0

I want to get the double of one number when I access to URL /double/<number> in hug server. Something like routing in flask. Is it possible to do that? In documentation of hug server, I did not found nothing:

My code would seem like:

@hug.get('/double', number)
def doubles(n):
    return 2*n
somenxavier
  • 1,206
  • 3
  • 20
  • 43

1 Answers1

1

Yup, you can do:

import hug

@hug.get("/double/{number}")
def doubles(response, number: hug.types.number):
    return 2 * number

Note, that if you don't define type it will default to string.

mislavcimpersak
  • 2,880
  • 1
  • 27
  • 30
  • How can I define the type of the variable of the method? How can I pass `{number}` to be a `path`, that is string which accepts slashes – somenxavier Dec 06 '19 at 19:37
  • Not sure you can do a string that accepts slashes that since slash `/` is a special delimiter for hug. You can define a preset number of multiple parameters instead: `@hug.get("/double/{number}/{not_number}/{also_not_number}")` `def doubles(response, number: hug.types.number, not_number: hug.types.text, also_not_number: hug.types.text):` `return 2 * number, not_number, also_not_number` – mislavcimpersak Dec 06 '19 at 19:55
  • I have a feeling that this would be easier if you explained with an example of an URL you are trying to get. – mislavcimpersak Dec 06 '19 at 19:58
  • Can you specify the path type like flask? For example returns the content of plain text file passing path of that: [in pseudocode] `def content(path): returns os.file(path).read` – somenxavier Dec 06 '19 at 20:13
  • 1
    Like a subpath? You want to catch `http://localhost:8000/foo` and `http://localhost:8000/foo/bar` and `http://localhost:8000/foo/bar/baz` all with one endpoint? Yeah, not sure that is possible with hug. Maybe by defining your own type that can take `/` as part of an argument. You would need to prevent hug's default mechanism of splitting the url by slashes. – mislavcimpersak Dec 06 '19 at 20:44
  • 1
    perhaps we could ask as independent question – somenxavier Dec 07 '19 at 22:18
  • asked: https://stackoverflow.com/questions/59234007/passing-path-like-string-in-hug-server – somenxavier Dec 08 '19 at 09:20