Created
April 3, 2019 19:02
-
-
Save shon/1cd9a46e53c22bdab16df40010f8422c to your computer and use it in GitHub Desktop.
FastAPI Python function -> API (sort of)
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
from fastapi import FastAPI | |
from pydantic import BaseModel | |
class Item(BaseModel): | |
name: str | |
class CreateSignature(BaseModel): | |
id: int | |
item: Item | |
def create(data: CreateSignature): | |
print(data) | |
print(data.item) | |
o = dict(id=data.id, name=data.item.name) | |
return o | |
app = FastAPI() | |
app.post('/items/')(create) | |
# | |
# uvicorn fa:app --host 0.0.0.0 --port 8888 | |
# | |
# ## Over HTTP ## | |
# import requests | |
# resp = requests.post('http://127.0.0.1:8888/test1/', json={'id': 1, 'item': {'name': 'Guido'}}) | |
# print(resp.json()) | |
# | |
# ## In Python ## | |
# class odict: | |
# def __init__(self, d): | |
# for k, v in d.items(): | |
# setattr(self, k, v) | |
# | |
# import fa | |
# fa.create(odict({'id': 2, 'item': odict({'name':'Guido'})})) # umm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment