Skip to content

Instantly share code, notes, and snippets.

@WTFox
Created May 22, 2017 11:13
Show Gist options
  • Save WTFox/d5086be21644bf2a3cc1c9aec211f3d6 to your computer and use it in GitHub Desktop.
Save WTFox/d5086be21644bf2a3cc1c9aec211f3d6 to your computer and use it in GitHub Desktop.
apistar-example
from apistar import App, Include, Route, schema
from apistar.docs import docs_routes
from apistar.http import Response
from apistar.statics import static_routes
_hotdogs = ['hotdog', 'hotdogs']
class Hotdog(schema.String):
min_length = 1
pattern = r'[hotdog]'
def is_hotdog(item: Hotdog) -> Response:
"""
Text based api endpoint to determine if item is a hotdog or not.
"""
return Response({
'status': f'{item}'
}, status=200)
def hotdogs() -> schema.List[Hotdog]:
return [Hotdog(item) for item in _hotdogs]
def get_hotdog(hotdog: Hotdog) -> Hotdog:
return Hotdog('hotdog')
def add_hotdog(new_hotdog: Hotdog) -> Hotdog:
_hotdogs.append(new_hotdog)
return Hotdog(new_hotdog)
routes = [
Route('/isHotdog', 'GET', is_hotdog),
Route('/hotdogs', 'GET', hotdogs),
Route('/hotdog', 'GET', get_hotdog),
Route('/hotdog', 'POST', add_hotdog),
Include('/docs', docs_routes),
Include('/static', static_routes)
]
app = App(routes=routes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment