Skip to content

Latest commit

 

History

History
162 lines (133 loc) · 4.13 KB

README.md

File metadata and controls

162 lines (133 loc) · 4.13 KB

Eve Genie

last_commit

Evegenie is a tool for blazing fast REST API creation. By providing JSON file with data and executing single command it can generate whole settings.py file for your Python Eve-based REST API application.

Originally developed by @drud

Latest Eve version tested: 2.0

Requirements

Python Eve (by default) uses MongoDB as a database. The easiest way for quick development with MongoDB is to spin up a MongoDB container by executing:

docker run --name mongo_eve -p 27017:27017 -d mongo

Then install dependencies

pip install -r requirements.txt

Then install Python Eve yourself (not included in requirements)

pip install eve

Example Usage

Create a json file, sample.json. Or you can use your own existing json file.

{
  "sample-resource": {
    "sample-string": "asdf",
    "sample-integer": 42,
    "sample-float": 1.0,
    "sample-list": ["a", "b", "c"],
    "sample-dict": {
      "sample-embedded-list": ["a", "b", "c"],
      "sample-embedded-dict": {"sample-integer2": 20}
    }
  },
  "sample-resource2": {
    "sample-object-id": "objectid:sample-resource",
    "sample-intrange": "1-100",
    "sample-floatrange": "0.0-1.0",
    "sample-unknown": {
      "allow_unknown": true
    }
  }
}

Then generate your eve schemas using:

python3 geneve.py sample.json

This will create a sample.settings.py file. Change it's name to settings.py and you can simply run the API with:

python3 run.py

That's it! You have a fully functional REST API working now based on your data!

Under the hood

Geneve created file with the following contents which is used by Python Eve as a settings file:

import os

MONGO_URI = os.environ.get('MONGODB_URI', 'mongodb://localhost:27017/evegenie')

RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
ITEM_METHODS = ['GET', 'PATCH', 'DELETE']


sample_resource = {
    'schema': {
        'sample-string': {
            'type': 'string'
        },
        'sample-integer': {
            'type': 'integer'
        },
        'sample-float': {
            'type': 'float'
        },
        'sample-list': {
            'type': 'list',
            'schema': {
                'type': 'string'
            }
        },
        'sample-dict': {
            'type': 'dict',
            'schema': {
                'sample-embedded-list': {
                    'type': 'list',
                    'schema': {
                        'type': 'string'
                    }
                },
                'sample-embedded-dict': {
                    'type': 'dict',
                    'schema': {
                        'sample-integer2': {
                            'type': 'integer'
                        }
                    }
                }
            }
        }
    }
}

sample_resource2 = {
    'schema': {
        'sample-object-id': {
            'type': 'objectid',
            'data_relation': {
                'resource': 'sample-resource',
                'field': '_id',
                'embeddable': True
            }
        },
        'sample-intrange': {
            'type': 'integer',
            'min': 1,
            'max': 100
        },
        'sample-floatrange': {
            'type': 'float',
            'min': 0.0,
            'max': 1.0
        },
        'sample-unknown': {
            'allow_unknown': True
        }
    }
}



DOMAIN = {
        'sample-resource': sample_resource,
        'sample-resource2': sample_resource2,
}

Special evegenie strings

Certain strings passed in via the source json will be converted to eve schema types with sane defaults.

  • "fieldname": "objectid:sample-entity" will add data_relation to sample-entity to the schema
  • "fieldname": "0-100" will create an integer with a min of 0 and a max of 100
  • "fieldname": "0.0-1.0" will create a float with a min of 0 and a max of 1
  • "fieldname": {"allow_unknown": true} will translate directly to fieldname that allows the unknown