Skip to content

Instantly share code, notes, and snippets.

View timothycrosley's full-sized avatar
🤔
Next experiment incoming

Timothy Edmund Crosley timothycrosley

🤔
Next experiment incoming
View GitHub Profile
@timothycrosley
timothycrosley / simple_todo.py
Created September 9, 2015 22:59
Simple todo for puppy meetup
'''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():
@timothycrosley
timothycrosley / versioning.py
Created March 11, 2016 07:00
A simple example of a hug API call with versioning
"""A simple example of a hug API call with versioning"""
import hug
@hug.get('/echo', versions=1)
def echo(text):
return text
@timothycrosley
timothycrosley / documentation.json
Created March 11, 2016 07:05
Auto generated documentation
{"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"
],
@timothycrosley
timothycrosley / write_once.py
Last active March 31, 2021 20:37
Write once run every where
"""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"""
@timothycrosley
timothycrosley / install.sh
Last active March 14, 2016 04:14
hug install script
pip3 install hug -U
# Or if pip is set to Python3 pip
pip install hug -U
@timothycrosley
timothycrosley / first_step_1.py
Last active March 15, 2016 01:37
First hug API step #1
"""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)}
@timothycrosley
timothycrosley / first_step_2.py
Last active March 15, 2016 01:37
Expand first API to include HTTP support
"""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)}
@timothycrosley
timothycrosley / first_step_3.py
Last active March 15, 2016 01:37
Adding CLI access to our service
"""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),
@timothycrosley
timothycrosley / run_on_wsgi.sh
Created March 14, 2016 07:16
Run using WSGI server
# 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__
@timothycrosley
timothycrosley / seamless.py
Last active June 27, 2017 09:46
Seamlessly switch how communication happens
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)