Python implementation of Node JSON Server (Flask as backend)
Project description
Python JSON Server
Python implementation of Node JSON Server (Flask as backend).
About
Make a full REST API without coding!
pyjserver is a python implementation of Node JSON Sever,
which creates a full REST api (with GET, POST, PUT and DELETE methods) based on json file.
What's Next
- PATCH HTTP method.
- Filter data with GET HTTP method.
- Pagination data with GET HTTP method.
Install
Python JSON Server library is on PyPi repository, so it can be installed with pip
.
>>> pip install pyjserver
Get stated
After install pyjserver, create db.json
file with the endpoints and data.
{
"python-libs": [
{
"id": 1,
"name": "pyjserver",
"version": "1.0.0"
},
{
"id": 2,
"name": "flask",
"version": "1.1.0"
},
{
"id": 3,
"name": "flask-restful",
"version": "0.3.8"
}
]
}
Run the pyjserver cli command.
>>> python -m pyjserver my-server ./db.json
OR
>>> python3 -m pyjserver my-server ./db.json
And the follow output will be displayed:
* Serving Flask app "my-server" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://localhost:5000/ (Press CTRL+C to quit)
Now to test the API, if you go to http://localhost:5000/python-libs you will get the same json in db.json.
[{"id":1,"name":"pyjserver","version":"1.0.0"},{"id":2,"name":"flask","version":"1.1.0"},{"id":3,"name":"flask-restful","version":"0.3.8"}]
With the same URL, you can send POST requests to add, PUT requests to edit and DELETE requests to remove from json file.
TIP
for each json key, it will be created an endpoint with GET, POST, PUT and DELETE HTTP methods.
You can also see all available endpoints in root url (http://localhost:5000/).
{
"endpoints" : [
{
"name" : "python-libs",
"route" : "/python-libs"
}
],
"methods" : [
"GET",
"POST",
"PUT",
"DELETE"
]
}
JSON file schema
Format: JSON object with a list of JSON objects.
Main JSON key: name of the endpoint
JSON list value: list with all records for the endpoint. To start with no data, just place an empty list []
JSONs inside the list: each record for the endpoint. DO NOT FORGET TO ADD SEQUENTIAL ID
KEY FOR EACH RECORD
{
"endpoint-1": [
{
"id": 1,
"name": "data 01"
},
{
"id": 2,
"name": "data 02"
}
],
"endpoint-2": [
{
"id": 1,
"name": "another data 01"
}
],
"endpoint-3": []
}
So the JSON above will create 3 endpoints /endpoint-1
(with 2 record data), endpoint-2
(with 1 record data) and endpoint-3
(with no record data).
The home page for this json file will display:
{
"endpoints" : [
{
"name" : "endpoint-1",
"route" : "/endpoint-1"
},
{
"name" : "endpoint-2",
"route" : "/endpoint-2"
},
{
"name" : "endpoint-3",
"route" : "/endpoint-3"
}
],
"methods" : [
"GET",
"POST",
"PUT",
"DELETE"
]
}
HTTP methods
For each endpoint created, is allowed GET
, POST
, PUT
and DELETE
HTTP method.
- GET
>>> curl http://localhost:5000/endpoint-1/
[{"id":1,"name":"data 01"},{"id":2,"name":"data 02"}]
- POST
>>> curl -d '{"name": "data 03"}' -H 'Content-type: application/json' -X POST http://localhost:5000/endpoint-1/
{"id":3,"name":"data 03"}
>>> curl http://localhost:5000/endpoint-1/
[{"id":1,"name":"data 01"},{"id":2,"name":"data 02"},{"id":3,"name":"data 03"}]
- PUT
>>> curl -d '{"name": "data 02 edited"}' -H 'Content-type: application/json' -X PUT http://localhost:5000/endpoint-1/2
{"id":2,"name":"data 02 edited"}
>>> curl http://localhost:5000/endpoint-1/
[{"id":1,"name":"data 01"},{"id":2,"name":"data 02 edited"},{"id":3,"name":"data 03"}]
- DELETE
>>> curl -X DELETE http://localhost:5000/endpoint-1/2
{}
>>> curl http://localhost:5000/endpoint-1/
[{"id":1,"name":"data 01"},{"id":3,"name":"data 03"}]
REMEMBER: all the data is inside the db.json
file, so any change with the HTTP method it will also change the file.
Consequently, if change the file, it will change the API.
NOTE: if you edit the json file and add an endpoint with pyjserver running, it will be necessary to restat the application to it load the new endpoint. But it is not necessary if you edit the file and add new records.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pyjserver-0.0.2.tar.gz
.
File metadata
- Download URL: pyjserver-0.0.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6057a55c2f0c2e9e56607198f87010a9731883410c9671a1befbeb685eb9c0f3 |
|
MD5 | 7a435c7f0a7b5f9926e165adbc8ae715 |
|
BLAKE2b-256 | 942b81973a69b55182ba4efa18d9d8703f9baafee84b2a342e7215abb130d747 |