This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Quite possibly the most simplistic remotely available todo list ever created''' | |
import hot_redis as redis | |
import hug | |
import requests | |
todos = redis.List(key='my_todos') | |
authentication = hug.authentication.basic(hug.authentication.verify(*AUTH)) | |
@hug.get('/todos') | |
def get_todos(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A simple example of a hug API call with versioning""" | |
import hug | |
@hug.get('/echo', versions=1) | |
def echo(text): | |
return text | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"404": "The API call you tried to make was not defined. Here's a definition of the API to help you get going :)", | |
"documentation": { | |
"overview": "A basic (single function) API written using Hug", | |
"handlers": { | |
"/happy_birthday": { | |
"GET": { | |
"usage": "Says happy birthday to a user", | |
"examples": [ | |
"http://localhost:8000/happy_birthday?name=HUG&age=1" | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""An example of writing an API to scrape hacker news once, and then enabling usage everywhere""" | |
import hug | |
import requests | |
@hug.local() | |
@hug.cli() | |
@hug.get() | |
def top_post(section: hug.types.one_of(('news', 'newest', 'show'))='news'): | |
"""Returns the top post from the provided section""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip3 install hug -U | |
# Or if pip is set to Python3 pip | |
pip install hug -U |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""First API, local access only""" | |
import hug | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), | |
'took': float(hug_timer)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""First hug API (local and HTTP access)""" | |
import hug | |
@hug.get(examples='name=Timothy&age=26') | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), | |
'took': float(hug_timer)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""First hug API (local, command-line, and HTTP access)""" | |
import hug | |
@hug.cli() | |
@hug.get(examples='name=Timothy&age=26') | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using uwsgi | |
uwsgi --http 0.0.0.0:8000 --wsgi-file first_step_3.py --callable __hug_wsgi__ | |
# Or, using gunicorn | |
gunicorn first_step_3:__hug_wsgi__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import hug | |
import my_internal_hug_microservice | |
if os.environ.get('API_INTERFACE', None) == 'HTTP':: | |
internal_hug_microservice = hug.use.HTTP('http://localhost:8000/') | |
else: | |
internal_hug_microservice = hug.use.Local(my_internal_hug_microservice) |
OlderNewer