Skip to main content

Mock Express server generated based on your views, that can come in handy when developing REST APIs with Django

Project description

Django REST Mock

Mock Express server generated based on your views, that can come in handy when developing REST APIs with Django.

Typically, the Django developer can first create all the views with docstrings and pass on the project to the front-end developer who can work with the mock server which outputs what is already expected. This allows frontend devs to work with a stable and predictable API.

Requirements

Requires Django 1.11 or later and Node.js.

Note that this requires views to be Class-based Views.

Installation

Using pip:

$ pip install django-rest-mock

Then include rest_mock_server into your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'rest_mock_server',
    ...
)

Usage

After including docstrings for the views you wish to generate endpoints for, you may run the following commands

Generates an ExpressJS file:

$ python manage.py genmockserver

Starts an ExpressJS server (it will generate an ExpressJS file if necessary):

$ python manage.py startmockserver [--file]
    --file (Optional): Specifies ExpressJS file to be used

Syntax

  1. Basic endpoint:

    class SomeView(APIView):
        """
        URL: /some-view/
        """
    
        def get(self, request, *args, **kwargs):
            """
            ```
            {
                "data": "Hello, world!"
            }
            ```
            """
            pass
  2. RESTful endpoint with list & instances:

    class ResourceListView(ListCreateAPIView):
        """
        URL: /resource/__key
        """
    
        def post(self, request, *args, **kwargs):
            """
            ```
            {
                "__options": {
                        "excludeKey": true
                },
            }
            ```
            """
            pass
    
    class ResourceView(RetrieveUpdateDestroyAPIView):
        """
        URL: /resource/__key
        """
    
        def get(self, request, *args, **kwargs):
            """
            ```
            {
                "__key": "<id:int>",
                "__key_position": "url",
                "__mockcount": 5,
                "__options": {
                    "modifiers": ["patch", "put", "delete"],
                    "excludeKey": false
                },
                "id": "<int>",
                "name": "<name>",
                "complexStructure": [
                    {
                        "link": "<int::10>",
                        "url": "<uri>",
                        "related_user": {
                            "id": "<int:1:5>",
                            "hash": "<sha256>"
                        }
                    }
                ]
            }
            ```
            """
            pass
    
        def put(self, request, *args, **kwargs):
            """We won't need to specify any response here"""
            pass
    
        def patch(self, request, *args, **kwargs):
            """We won't need to specify any response here"""
            pass
    
        def delete(self, request, *args, **kwargs):
            """We won't need to specify any response here"""
            pass

When creating fixtures for a resource (CRUD), you only need to work with the instance endpoint, in Django REST framework, it’s typically the endpoint that requires a unique ID - e.g. /some-resource/<pk>

You need to specify __key in the url and also in the response as above. The value follows the following syntax <name-of-unique-key:data-type>. You will also need to specify the position of the key: either url or query. If it is url, it exists as a URL param, and query means that the key should be found in query string.

  • __mockcount: (defaults to 1) Represents the number of instances of this fixture to create

  • __options: Possible options related to this endpoint:

  • modifiers: a list of modifier methods allowed for this resource. If you don’t specify a method, that method won’t be allowed for that endpoint

  • excludeKey: this can be specified to exclude a method from matching __key in the url. E.g. for the POST method for /resource/, you might want to exclude it

The syntax for fake data is as follows: <fakedatatype:min:max>

  • fakedatatype is any attribute that can be generated by Faker

  • min: for numbers, it will only generated random numbers that are at least min or greater. For strings, this will be the first index it will slice from

  • max: for numbers, it will only generated random numbers that are at most max or smaller. For strings, this will be the last index

POST requests will not create new instances, but PUT, PATCH and DELETE will work as expected on the resources. The resources are reset everytime the server is restarted.

Example

Refer to the example app for a detailed example.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-rest-mock-0.2.2.tar.gz (15.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page