Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

API Mocking #68

Open
tomchristie opened this issue Apr 20, 2017 · 2 comments
Open

API Mocking #68

tomchristie opened this issue Apr 20, 2017 · 2 comments

Comments

@tomchristie
Copy link
Member

tomchristie commented Apr 20, 2017

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
    }
]
@tomchristie tomchristie modified the milestone: 0.2 Release Apr 24, 2017
@aidiss
Copy link

aidiss commented Jul 2, 2017

How could this be implemented?

@tomchristie tomchristie modified the milestone: 0.2 Release Aug 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants
@tomchristie @aidiss and others