Quickstart Guide for Python Eve: Setting up

Python REST API framework designed for human beings

Problems I faced

Setting-up

A Minimal Application

from eve import Eveapp = Eve()if __name__ == '__main__':
app.run()
DOMAIN = {'users': {}}
$ python run.py
* Running on http://127.0.0.1:5000/

Database Inclusion

# Let's just use the local mongod instance. Edit as needed.# Please note that MONGO_HOST and MONGO_PORT could very well be left
# out as they already default to a bare bones local 'mongod' instance.
MONGO_HOST = 'localhost'
MONGO_PORT = 27017# Skip this block if your db has no auth. But it really should.
MONGO_USERNAME = '<your username>'
MONGO_PASSWORD = '<your password>'
# Name of the database on which the user can be authenticated,
# needed if --auth mode is enabled.
MONGO_AUTH_SOURCE = '<dbname>'. MONGO_DBNAME = 'users_ms'

Real-life Application

users_schema = {
'user_name': {
'type': 'string',
'minlength': 1,
'maxlength': 50,
'required': True,
# talk about hard constraints! For the purpose of the demo
# 'lastname' is an API entry-point, so we need it to be unique.
'unique': True,
},
'user_education': {
'type': 'integer',
'minlength': 1,
'maxlength': 4,
'required': True,
# talk about hard constraints! For the purpose of the demo
},
# An embedded 'strongly-typed' dictionary.
'question_json': {
'schema': {
'question': {'type': 'string'},
'answer': {'type': 'string'}
},'required': True,
},# 'role' is a list, and can only contain values from 'allowed'.
'role': {
'type': 'list',
'allowed': ["author", "contributor", "copy"],
}
}
users = {
'item_title': 'users',
# Enable GET, POST, DELETE for resources/collections.
# (if we omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
'resource_methods': ['GET', 'POST'],
# Enable GET, PATCH, PUT and deletes #of individual items'item_methods': ['GET', 'PATCH', 'DELETE'], 'schema': users_schema
}
DOMAIN = {
'users': users,
}
from schema import users_schema
# Let's just use the local mongod instance. Edit as needed.# Please note that MONGO_HOST and MONGO_PORT could very well be left

# out as they already default to a bare bones local 'mongod' instance.
MONGO_HOST = 'localhost'
MONGO_PORT = 27017# Skip this block if your db has no auth. But it really should.
MONGO_USERNAME = '<your username>'
MONGO_PASSWORD = '<your password>'
# Name of the database on which the user can be authenticated,
# needed if --auth mode is enabled.
MONGO_AUTH_SOURCE = '<dbname>'. MONGO_DBNAME = 'users_ms'users = {
'item_title': 'users',
# Enable GET, POST, DELETE for resources/collections.
# (if we omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
'resource_methods': ['GET', 'POST'],
# Enable GET, PATCH, PUT and deletes #of individual items'item_methods': ['GET', 'PATCH', 'DELETE'], 'schema': users_schema
}
DOMAIN = {
'users': users,
}
$ curl -d '[{"user_name":"Sagar Malik","user_education":12,"question_json":{"Question":"What is photosynthesis","answer":"It is the process by which plants make food"},"role":"author"},{"user_name":"Vartul Mittal","user_education":12,"question_json":{"Question":"What is photosynthesis","answer":"It is the process by which plants make food"},"role":"author"}]' -H 'Content-Type: application/json'  http://127.0.0.1:5000/users
HTTP/1.0 201 OK
{
"_status": "OK",
"_items": [
{
"_updated": "Fri, 05 Jun 2020 02:30:56 GMT",
"_created": "Fri, 05 Jun 2020 02:30:56 GMT",
"_etag": "fbd77aca5e59b9a0751c6f549f890692aa0d10c3",
"_id": "5ed9ae60dbdf8c083f0ca30f",
"_links": {
"self": {
"title": "users",
"href": "users/5ed9ae60dbdf8c083f0ca30f"
}
},
"_status": "OK"
},
{
"_updated": "Fri, 05 Jun 2020 02:30:56 GMT",
"_created": "Fri, 05 Jun 2020 02:30:56 GMT",
"_etag": "364e0a5574f29e7fab9a84b293c0ffc1d02c2587",
"_id": "5ed9ae60dbdf8c083f0ca310",
"_links": {
"self": {
"title": "users",
"href": "users/5ed9ae60dbdf8c083f0ca310"
}
},
"_status": "OK"
}
]
}

--

--

--

Graduate from The National Institute of Engineering. Currently, I am working as a backend developer in a FinTech firm.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Easy PSR-7 with Zend Diactoros

CI/CD with Drone, Kubernetes and Helm — Part 2

Dictionaries in Python

Top 10 Useful Websites for Java Developers

Hitting dead ends, a tale of transcribeR.

High Quality at Optimum Cost — Salesforce Test Automation

Fluttering Dart: Null Safety

A splash of coffee.

Animations in Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
anurag kumar

anurag kumar

Graduate from The National Institute of Engineering. Currently, I am working as a backend developer in a FinTech firm.

More from Medium

Spreedsheats API — Python

Build your own QRCode using Python

An Overview on Tenso

Build the Forest in Python Series: Make the Forest Thread-Safe