$ pip install eve
from eve import Eve app = Eve() if __name__ == '__main__': app.run()
# user, password, ds049945.mongolab.com, example . MONGO_URI = "mongodb://user:password@ds049945.mongolab.com:49945/example" # Eve API "read-only" (.. GET ), # POST, PUT, PATCH, DELETE. RESOURCE_METHODS = ['GET', 'POST', 'DELETE'] ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE'] DOMAIN = { # `/users` 'users': { # . Cerberus Eve. # http://docs.python-cerberus.org/en/stable/. # EVE http://python-eve.org/validation.html#validation. 'schema': { 'username': { 'type': 'string', 'minlength': 5, 'maxlength': 32, 'required': True, # ( , ) 'unique': True, }, 'firstname': { 'type': 'string', 'minlength': 1, 'maxlength': 10, 'required': True, }, 'lastname': { 'type': 'string', 'minlength': 1, 'maxlength': 15, 'required': True, }, 'role': { 'type': 'list', # : 'allowed': ["author", "contributor"], # : "author", "contributor" }, 'location': { 'type': 'dict', # : # "" 'schema': { 'address': {'type': 'string'}, 'city': {'type': 'string'} }, }, 'born': { 'type': 'datetime', }, 'active': { 'type': 'boolean', 'default': True } } }, # `/groups` 'groups': { # (. ). 'schema': { 'title': { 'type': 'string', 'minlength': 5, 'maxlength': 32, 'required': True, 'unique': True }, 'users': { 'type': 'list', # : 'default': [], # : # "" 'schema': { 'type': 'objectid', # : objectid # 'data_relation': { 'resource': 'users', # `users` ( ) 'field': '_id', # `_id` 'embeddable': True } } } } } }
$ python3.5 run.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
$ pip install httpie
$ http http://0.0.0.0:5000/ HTTP/1.0 200 OK Content-Length: 99 Content-Type: application/json Date: Sun, 07 Feb 2016 18:13:33 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_links": { "child": [ { "href": "users", "title": "users" }, { "href": "groups", "title": "groups" } ] } }
» http http://0.0.0.0:5000/users HTTP/1.0 200 OK Content-Length: 166 Content-Type: application/json Date: Sun, 07 Feb 2016 18:20:41 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 X-Total-Count: 0 { "_items": [], "_links": { "parent": { "href": "/", "title": "home" }, "self": { "href": "users", "title": "users" } }, "_meta": { "max_results": 25, "page": 1, "total": 0 } }
$ http http://0.0.0.0:5000/users username=johndoe HTTP/1.0 422 UNPROCESSABLE ENTITY Content-Length: 184 Content-Type: application/json Date: Sun, 07 Feb 2016 18:22:44 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_error": { "code": 422, "message": "Insertion failure: 1 document(s) contain(s) error(s)" }, "_issues": { "firstname": "required field", "lastname": "required field" }, "_status": "ERR" }
$ http http://0.0.0.0:5000/users username=johndoe
{ "username": "johndoe" }
"_issues": { "firstname": "required field", "lastname": "required field" }
$ http http://0.0.0.0:5000/users username=johndoe firstname=John lastname=Doe HTTP/1.0 201 CREATED Content-Length: 276 Content-Type: application/json Date: Sun, 07 Feb 2016 18:34:42 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "24509359443095dd05dece6d0eb7d98cce70b076", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 18:34:41 GMT" }
$ http http://0.0.0.0:5000/users HTTP/1.0 200 OK Content-Length: 504 Content-Type: application/json Date: Sun, 07 Feb 2016 18:36:00 GMT Last-Modified: Sun, 07 Feb 2016 18:34:41 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 X-Total-Count: 1 { "_items": [ { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "24509359443095dd05dece6d0eb7d98cce70b076", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 18:34:41 GMT", "active": true, "firstname": "John", "lastname": "Doe", "username": "johndoe" } ], "_links": { "parent": { "href": "/", "title": "home" }, "self": { "href": "users", "title": "users" } }, "_meta": { "max_results": 25, "page": 1, "total": 1 } }
$ http put http://0.0.0.0:5000/users/56b78e41cf7b35255aa5a1e6 username=janedoe firstname="Jane" lastname="Doe" HTTP/1.0 403 FORBIDDEN Content-Length: 101 Content-Type: application/json Date: Sun, 07 Feb 2016 18:43:04 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_error": { "code": 403, "message": "An etag must be provided to edit a document" }, "_status": "ERR" }
$ http put http://0.0.0.0:5000/users/56b78e41cf7b35255aa5a1e6 "If-Match":"24509359443095dd05dece6d0eb7d98cce70b076" username=janedoe firstname="Jane" lastname="Doe" HTTP/1.0 200 OK Content-Length: 276 Content-Type: application/json Date: Sun, 07 Feb 2016 18:46:56 GMT ETag: 0138d193174528c205827ba9af25b7b8fb93940e Last-Modified: Sun, 07 Feb 2016 18:46:56 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "0138d193174528c205827ba9af25b7b8fb93940e", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 18:46:56 GMT" }
$ http http://0.0.0.0:5000/groups title="Friends" users:='["56b77466cf7b352414deb451"]' HTTP/1.0 422 UNPROCESSABLE ENTITY Content-Length: 220 Content-Type: application/json Date: Sun, 07 Feb 2016 19:14:31 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_error": { "code": 422, "message": "Insertion failure: 1 document(s) contain(s) error(s)" }, "_issues": { "users": { "0": "value '56b77466cf7b352414deb451' must exist in resource 'users', field '_id'." } }, "_status": "ERR" }
users:='["56b77466cf7b352414deb451"]'
» http http://0.0.0.0:5000/groups title="Friends" users:='["56b78e41cf7b35255aa5a1e6"]' HTTP/1.0 201 CREATED Content-Length: 278 Content-Type: application/json Date: Sun, 07 Feb 2016 19:21:42 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 19:21:41 GMT", "_etag": "c6fc02a0bd4bae92a1310be0748ff8bc971ff209", "_id": "56b79945cf7b35255aa5a1e7", "_links": { "self": { "href": "groups/56b79945cf7b35255aa5a1e7", "title": "Group" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 19:21:41 GMT" }
$ http http://0.0.0.0:5000/groups HTTP/1.0 200 OK Content-Length: 488 Content-Type: application/json Date: Sun, 07 Feb 2016 19:24:50 GMT Last-Modified: Sun, 07 Feb 2016 19:21:41 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 X-Total-Count: 1 { "_items": [ { "_created": "Sun, 07 Feb 2016 19:21:41 GMT", "_etag": "c6fc02a0bd4bae92a1310be0748ff8bc971ff209", "_id": "56b79945cf7b35255aa5a1e7", "_links": { "self": { "href": "groups/56b79945cf7b35255aa5a1e7", "title": "Group" } }, "_updated": "Sun, 07 Feb 2016 19:21:41 GMT", "title": "Friends", "users": [ "56b78e41cf7b35255aa5a1e6" ] } ], "_links": { "parent": { "href": "/", "title": "home" }, "self": { "href": "groups", "title": "groups" } }, "_meta": { "max_results": 25, "page": 1, "total": 1 } }
$ http http://0.0.0.0:5000/groups/56b79945cf7b35255aa5a1e7/\?embedded='{"users":1}' HTTP/1.0 200 OK Content-Length: 646 Content-Type: application/json Date: Sun, 07 Feb 2016 19:38:27 GMT ETag: c6fc02a0bd4bae92a1310be0748ff8bc971ff209 Last-Modified: Sun, 07 Feb 2016 19:21:41 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 19:21:41 GMT", "_etag": "c6fc02a0bd4bae92a1310be0748ff8bc971ff209", "_id": "56b79945cf7b35255aa5a1e7", "_links": { "collection": { "href": "groups", "title": "groups" }, "parent": { "href": "/", "title": "home" }, "self": { "href": "groups/56b79945cf7b35255aa5a1e7", "title": "Group" } }, "_updated": "Sun, 07 Feb 2016 19:21:41 GMT", "title": "Friends", "users": [ { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "0138d193174528c205827ba9af25b7b8fb93940e", "_id": "56b78e41cf7b35255aa5a1e6", "_updated": "Sun, 07 Feb 2016 18:46:56 GMT", "active": true, "firstname": "Jane", "lastname": "Doe", "username": "janedoe" } ] }
$ http http://0.0.0.0:5000/groups/56b79945cf7b35255aa5a1e7/\?embedded='{"users":1}' "Accept":"application/xml" HTTP/1.0 200 OK Content-Length: 690 Content-Type: application/xml; charset=utf-8 Date: Sun, 07 Feb 2016 19:43:36 GMT ETag: c6fc02a0bd4bae92a1310be0748ff8bc971ff209 Last-Modified: Sun, 07 Feb 2016 19:21:41 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 <resource href="groups/56b79945cf7b35255aa5a1e7" title="Group"> <link href="groups" rel="collection" title="groups" /> <link href="/" rel="parent" title="home" /> <_created>Sun, 07 Feb 2016 19:21:41 GMT</_created> <_etag>c6fc02a0bd4bae92a1310be0748ff8bc971ff209</_etag> <_id>56b79945cf7b35255aa5a1e7</_id> <_updated>Sun, 07 Feb 2016 19:21:41 GMT</_updated> <title>Friends</title> <users> <_created>Sun, 07 Feb 2016 18:34:41 GMT</_created> <_etag>0138d193174528c205827ba9af25b7b8fb93940e</_etag> <_id>56b78e41cf7b35255aa5a1e6</_id> <_updated>Sun, 07 Feb 2016 18:46:56 GMT</_updated> <active>True</active> <firstname>Jane</firstname> <lastname>Doe</lastname> <username>janedoe</username> </users> </resource>
$ http patch http://0.0.0.0:5000/users/56b78e41cf7b35255aa5a1e6 firstname=John "If-Match":"0138d193174528c205827ba9af25b7b8fb93940e" HTTP/1.0 200 OK Content-Length: 276 Content-Type: application/json Date: Sun, 07 Feb 2016 19:46:48 GMT ETag: 86f3495cf1d6edf301e25563099844bd816c5a3c Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "86f3495cf1d6edf301e25563099844bd816c5a3c", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 19:46:47 GMT" }
» http http://0.0.0.0:5000/users/56b78e41cf7b35255aa5a1e6 HTTP/1.0 200 OK Content-Length: 431 Content-Type: application/json Date: Sun, 07 Feb 2016 19:50:06 GMT ETag: 86f3495cf1d6edf301e25563099844bd816c5a3c Last-Modified: Sun, 07 Feb 2016 19:46:47 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "86f3495cf1d6edf301e25563099844bd816c5a3c", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "collection": { "href": "users", "title": "users" }, "parent": { "href": "/", "title": "home" }, "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 19:46:47 GMT", "active": true, "firstname": "John", "lastname": "Doe", "username": "janedoe" }
$ echo '[{"username": "userone", "firstname": "First", "lastname":"Last"},{"username":"usertwo", "firstname":"First", "lastname":"Last"}]' | http http://0.0.0.0:5000/users HTTP/1.0 201 CREATED Content-Length: 585 Content-Type: application/json Date: Sun, 07 Feb 2016 20:01:33 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 { "_items": [ { "_created": "Sun, 07 Feb 2016 20:01:33 GMT", "_etag": "6f397b570ef12769d372c902fa6149bb7e9eaf89", "_id": "56b7a29dcf7b35255aa5a1e8", "_links": { "self": { "href": "users/56b7a29dcf7b35255aa5a1e8", "title": "User" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 20:01:33 GMT" }, { "_created": "Sun, 07 Feb 2016 20:01:33 GMT", "_etag": "378f30b37724139c213a85079185226ab2b209f3", "_id": "56b7a29dcf7b35255aa5a1e9", "_links": { "self": { "href": "users/56b7a29dcf7b35255aa5a1e9", "title": "User" } }, "_status": "OK", "_updated": "Sun, 07 Feb 2016 20:01:33 GMT" } ], "_status": "OK" }
$ http http://0.0.0.0:5000/users\?where='{"firstname":"John"}' HTTP/1.0 200 OK Content-Length: 535 Content-Type: application/json Date: Sun, 07 Feb 2016 20:05:28 GMT Last-Modified: Sun, 07 Feb 2016 19:46:47 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 X-Total-Count: 1 { "_items": [ { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "86f3495cf1d6edf301e25563099844bd816c5a3c", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 19:46:47 GMT", "active": true, "firstname": "John", "lastname": "Doe", "username": "janedoe" } ], "_links": { "parent": { "href": "/", "title": "home" }, "self": { "href": "users?where={\"firstname\":\"John\"}", "title": "users" } }, "_meta": { "max_results": 25, "page": 1, "total": 1 } }
$ http http://0.0.0.0:5000/users\?sort\=-username HTTP/1.0 200 OK Content-Length: 1203 Content-Type: application/json Date: Sun, 07 Feb 2016 20:08:08 GMT Last-Modified: Sun, 07 Feb 2016 20:01:33 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0 X-Total-Count: 3 { "_items": [ { "_created": "Sun, 07 Feb 2016 20:01:33 GMT", "_etag": "378f30b37724139c213a85079185226ab2b209f3", "_id": "56b7a29dcf7b35255aa5a1e9", "_links": { "self": { "href": "users/56b7a29dcf7b35255aa5a1e9", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 20:01:33 GMT", "active": true, "firstname": "First", "lastname": "Last", "username": "usertwo" }, { "_created": "Sun, 07 Feb 2016 20:01:33 GMT", "_etag": "6f397b570ef12769d372c902fa6149bb7e9eaf89", "_id": "56b7a29dcf7b35255aa5a1e8", "_links": { "self": { "href": "users/56b7a29dcf7b35255aa5a1e8", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 20:01:33 GMT", "active": true, "firstname": "First", "lastname": "Last", "username": "userone" }, { "_created": "Sun, 07 Feb 2016 18:34:41 GMT", "_etag": "86f3495cf1d6edf301e25563099844bd816c5a3c", "_id": "56b78e41cf7b35255aa5a1e6", "_links": { "self": { "href": "users/56b78e41cf7b35255aa5a1e6", "title": "User" } }, "_updated": "Sun, 07 Feb 2016 19:46:47 GMT", "active": true, "firstname": "John", "lastname": "Doe", "username": "janedoe" } ], "_links": { "parent": { "href": "/", "title": "home" }, "self": { "href": "users?sort=-username", "title": "users" } }, "_meta": { "max_results": 25, "page": 1, "total": 3 } }
$ http delete http://0.0.0.0:5000/users HTTP/1.0 204 NO CONTENT Content-Length: 0 Content-Type: application/json Date: Sun, 07 Feb 2016 20:10:06 GMT Server: Eve/0.6.1 Werkzeug/0.10.4 Python/3.5.0
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
Source: https://habr.com/ru/post/276731/
All Articles