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
%cpaste
import time
class Test(object):
__slots__ = ('a', 'b', 'c')
def __init__(self):
self.a = 1
self.b = 1
self.c = 1
@timothycrosley
timothycrosley / exception.py
Last active April 1, 2016 09:09
Custom Exception handling
import hug
from project import exceptions
@hug.exception((exceptions.CustomExceptionTypeOne, exceptions.CustomExceptionTypeTwo))
def handle_custom_exceptions(exception):
# handles the passed in custom exception, able to return a different response here:
return {'error': 'No error here!'}
@timothycrosley
timothycrosley / class_based.py
Created March 18, 2016 08:01
Class based routing
import hug
# As a pattern for reuse
@hug.object.urls('/endpoint', requires=())
class MyClass(object):
@hug.object.get()
def my_method(self):
return 'hi there!'
@timothycrosley
timothycrosley / chainable.py
Created March 18, 2016 07:35
Chainable Routing
import hug
api = hug.get(on_invalid=hug.redirect.not_found)
@api.urls('/do-math', examples='number_1=1&number_2=2')
def math(number_1: hug.types.number, number_2: hug.types.number):
return number_1 + number_2
@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)
@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 / 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 / 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_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 / 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