apistar
apistar copied to clipboard
API Mocking
Once we've got the schemas fully in place we'd like to be able to run a mock API purely based on the function annotations, so that users can start up a mock API even before they've implemented any functionality.
We'd want to use randomised output that fits the given schema constraints, as well as validating any input, and returning those values accordingly.
eg.
schemas.py:
class KittenName(schema.String):
max_length = 100
class KittenColor(schema.Enum):
enum = [
'black',
'brown',
'white',
'grey',
'tabby'
]
class Kitten(schema.Object):
properties = {
'name': KittenName,
'color': KittenColor,
'cuteness': schema.Number(
minimum=0.0,
maximum=10.0,
multiple_of=0.1
)
}
views.py:
def list_favorite_kittens(color: KittenColor=None) -> List[Kitten]:
"""
List your favorite kittens, optionally filtered by color.
"""
pass
def add_favorite_kitten(name: KittenName) -> Kitten:
"""
Add a kitten to your favorites list.
"""
pass
We should be able to do this sort of thing...
$ apistar mock
mock api running on 127.0.0.1:5000
$ curl http://127.0.0.1:5000/kittens/fav/
[
{
"name": "congue",
"color": "tabby",
"cuteness": 5.9
},
{
"name": "aenean",
"color": "white",
"cuteness": 9.3
},
{
"name": "etiam",
"color": "tabby",
"cuteness": 8.8
}
]
How could this be implemented?
If we could generate an API blueprint document we can have this done for us. Since we can convert a Swagger to blueprint using swagger2blueprint it's fairly easy to achieve. It will also allow us to easily integrate with Apiary if we want to.
I'm not sure if it's a good idea to introduce another language runtime to the mix just to do it but it's an idea or at least a workaround until this issue is resolved.