Skip to content

Commit

Permalink
Merge pull request #576 from JJ/develop
Browse files Browse the repository at this point in the history
Adds URL-encoded feature to *getting started* example
  • Loading branch information
timothycrosley committed Oct 23, 2017
2 parents 4d26316 + 70eda78 commit aec8a6f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
31 changes: 29 additions & 2 deletions README.md
Expand Up @@ -60,9 +60,35 @@ To run, from the command line type:
hug -f happy_birthday.py
```

You can access the example in your browser at: `localhost:8000/happy_birthday?name=hug&age=1`. Then check out the documentation for your API at `localhost:8000/documentation`
You can access the example in your browser at:
`localhost:8000/happy_birthday?name=hug&age=1`. Then check out the
documentation for your API at `localhost:8000/documentation`

Parameters can also be encoded in the URL (check
out [`happy_birthday.py`](examples/happy_birthday.py) for the whole
example).

```py
@hug.get('/greet/{event}')
def greet(event: str):
"""Greets appropriately (from http://blog.ketchum.com/how-to-write-10-common-holiday-greetings/) """
greetings = "Happy"
if event == "Christmas":
greetings = "Merry"
if event == "Kwanzaa":
greetings = "Joyous"
if event == "wishes":
greetings = "Warm"

return "{greetings} {event}!".format(**locals())
```

Which, once you are running the server as above, you can use this way:

```
curl http://localhost:8000/greet/wishes
"Warm wishes!"
```

Versioning with hug
===================
Expand Down Expand Up @@ -153,7 +179,8 @@ def math(number_1:int, number_2:int): #The :int after both arguments is the Type
return number_1 + number_2
```

Type annotations also feed into hug's automatic documentation generation to let users of your API know what data to supply.
Type annotations also feed into `hug`'s automatic documentation
generation to let users of your API know what data to supply.


**Directives** functions that get executed with the request / response data based on being requested as an argument in your api_function.
Expand Down
13 changes: 13 additions & 0 deletions examples/happy_birthday.py
Expand Up @@ -6,3 +6,16 @@
def happy_birthday(name, age: hug.types.number):
"""Says happy birthday to a user"""
return "Happy {age} Birthday {name}!".format(**locals())

@hug.get('/greet/{event}')
def greet(event: str):
"""Greets appropriately (from http://blog.ketchum.com/how-to-write-10-common-holiday-greetings/) """
greetings = "Happy"
if event == "Christmas":
greetings = "Merry"
if event == "Kwanzaa":
greetings = "Joyous"
if event == "wishes":
greetings = "Warm"

return "{greetings} {event}!".format(**locals())
8 changes: 8 additions & 0 deletions examples/test_happy_birthday.py
Expand Up @@ -6,3 +6,11 @@ def tests_happy_birthday():
response = hug.test.get(happy_birthday, 'happy_birthday', {'name': 'Timothy', 'age': 25})
assert response.status == HTTP_200
assert response.data is not None

def tests_season_greetings():
response = hug.test.get(happy_birthday, 'greet/Christmas')
assert response.status == HTTP_200
assert response.data is not None
assert str(response.data) == "Merry Christmas!"
response = hug.test.get(happy_birthday, 'greet/holidays')
assert str(response.data) == "Happy holidays!"

0 comments on commit aec8a6f

Please sign in to comment.