Skip to content

Commit 8cd8d06

Browse files
committed
fast-json-api
1 parent b6c090f commit 8cd8d06

15 files changed

+1230
-1
lines changed

LICENSE

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
MIT License
1+
The MIT License (MIT)
22

33
Copyright (c) 2021 Sai Kumar Yava
44

@@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include README.md
2+
include LICENSE
3+
include json_server/*.py

README.md

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
[fast-json-server v0.0.1](https://pypi.org/project/fast-json-server/)
2+
3+
**fast-json-server** provides a full **REST API / GraphQL** Server with zero
4+
coding in few seconds.
5+
6+
fast-json-server provides a simple and quick back-end for development.
7+
8+
fast-json-server only requires json data.
9+
10+
## Installation
11+
12+
```shell
13+
$ pip install fast-json-server
14+
```
15+
16+
## How to use
17+
18+
Create individual json files with some data in a folder
19+
20+
**Note:** json objects must contain **id** key.
21+
22+
#### /sample_data/users.json
23+
24+
```json
25+
26+
[
27+
{
28+
"id": 1,
29+
"first_name": "Sampath",
30+
"last_name": "Varma"
31+
},
32+
{
33+
"id": 2,
34+
"first_name": "Virat",
35+
"last_name": "Ranbhor"
36+
},
37+
{
38+
"id": 3,
39+
"first_name": "Rakesh",
40+
"last_name": "Chopra"
41+
}
42+
]
43+
44+
```
45+
46+
#### /sample_data/articles.json
47+
48+
```json
49+
50+
[
51+
{
52+
"id": 1,
53+
"title": "Article1",
54+
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore, et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
55+
"likes": 20,
56+
"created_date": "11/30/2021",
57+
"author_id": 3
58+
},
59+
{
60+
"id": 2,
61+
"title": "Article2",
62+
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
63+
"likes": 20,
64+
"created_date": "11/30/2021",
65+
"author_id": 2
66+
},
67+
{
68+
"id": 3,
69+
"title": "Article3",
70+
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
71+
"likes": 20,
72+
"created_date": "11/30/2021",
73+
"author_id": 2
74+
}
75+
]
76+
77+
```
78+
79+
### Start Fast-JSON-Server
80+
81+
fast-json-server supports REST API and GraphQL
82+
83+
```shell
84+
python -m fast_json_server.json_server --data_path="/sample_data" --host="0.0.0.0" --port=3000 --log_level="debug" --server_type="rest_api"
85+
# or
86+
python -m fast_json_server.json_server --data_path="/sample_data" --host="0.0.0.0" --port=3001 --log_level="debug" --server_type="graph_ql"
87+
```
88+
89+
or
90+
91+
```python
92+
from fast_json_server.json_server import start_server
93+
94+
start_server(data_path="/sample_data", host="0.0.0.0", port=3000,
95+
log_level='debug', server_type="rest_api")
96+
# or
97+
start_server(data_path="/sample_data", host="0.0.0.0", port=3000,
98+
log_level='debug', server_type="graph_ql")
99+
```
100+
101+
## REST API Server
102+
103+
fast-json-server creates GET,POST,PUT,DELETE Routes from json files content.
104+
105+
### User routes
106+
107+
```
108+
GET /api/v1/users?page_num=1&page_size=5
109+
GET /api/v1/users?id=&first_name=&last_name=
110+
POST /users
111+
PUT /users/1
112+
DELETE /users/1
113+
```
114+
115+
### Article routes
116+
117+
```
118+
GET /api/v1/articles?page_num=1&page_size=5
119+
GET /api/v1/articles?id=&title=&content=&likes&author_id=
120+
POST /articles
121+
PUT /articles/1
122+
DELETE /articles/1
123+
```
124+
125+
Example GET Request
126+
127+
```shell
128+
curl --location --request GET 'http://localhost:3000/api/v1/users?page_num=1&page_size=5'
129+
```
130+
131+
Example GET Response
132+
133+
```json
134+
{
135+
"total_pages": 1,
136+
"total_items": 9,
137+
"page_data": {
138+
"page_num": 1,
139+
"item_count": 5,
140+
"items": [
141+
{
142+
"id": 2,
143+
"first_name": "Sampath",
144+
"last_name": "Varma"
145+
},
146+
{
147+
"id": 3,
148+
"first_name": "Virat",
149+
"last_name": "Ranbhor"
150+
},
151+
{
152+
"id": 4,
153+
"first_name": "Rakesh",
154+
"last_name": "Chopra"
155+
},
156+
{
157+
"id": 5,
158+
"first_name": "Jimmy",
159+
"last_name": "Kapoor"
160+
},
161+
{
162+
"id": 6,
163+
"first_name": "Satya",
164+
"last_name": "Ellapragada"
165+
}
166+
]
167+
}
168+
}
169+
```
170+
171+
fast-json-api provides interactive swagger api docs
172+
173+
```
174+
/api/v1/docs
175+
```
176+
177+
## GraphQL
178+
179+
fast-json-server creates the Query and Mutations from json files content.
180+
181+
GraphQL endpoint **/api/v1/graphql**
182+
183+
### User Query
184+
185+
```shell
186+
# User data pagination
187+
{
188+
users(pageSize:3,pageNum:1){
189+
totalPages,
190+
totalItems,
191+
pageData{
192+
pageNum,
193+
itemCount,
194+
items{
195+
firstName,
196+
lastName
197+
}
198+
},
199+
}
200+
}
201+
# User data with filter
202+
{
203+
users(pageSize:3,pageNum:1,firstName:"Virat"){
204+
totalPages,
205+
totalItems,
206+
pageData{
207+
pageNum,
208+
itemCount,
209+
items{
210+
firstName,
211+
lastName
212+
}
213+
},
214+
}
215+
}
216+
```
217+
218+
### User Mutations
219+
220+
```shell
221+
# create a record
222+
mutation {
223+
createUsers(createRecord:{firstName:"Mohan",lastName:"Kumar"}) {
224+
message
225+
id
226+
}
227+
}
228+
# update a record
229+
mutation {
230+
updateUsers(id:10,updateRecord:{firstName:"Mohan",lastName:"Chandra"}) {
231+
message
232+
}
233+
}
234+
# delete a record
235+
mutation {
236+
deleteUsers(id:10) {
237+
message
238+
}
239+
}
240+
```
241+
242+
### Article Query
243+
244+
```shell
245+
# Articles data pagination
246+
{
247+
articles(pageSize:3,pageNum:1){
248+
totalPages,
249+
totalItems,
250+
pageData{
251+
pageNum,
252+
itemCount,
253+
items{
254+
firstName,
255+
lastName
256+
}
257+
},
258+
}
259+
}
260+
# Articles data with filter
261+
{
262+
articles(pageSize:3,pageNum:1,title:"Article3"){
263+
totalPages,
264+
totalItems,
265+
pageData{
266+
pageNum,
267+
itemCount,
268+
items{
269+
title,
270+
content
271+
likes
272+
}
273+
},
274+
}
275+
}
276+
```
277+
278+
### Article Mutations
279+
280+
```shell
281+
# create a record
282+
mutation {
283+
createArticles(createRecord:{title:"Article10",content:"Kumar","likes":0,"author_id":1}) {
284+
message
285+
id
286+
}
287+
}
288+
# update a record
289+
mutation {
290+
updateArticles(id:10,updateRecord:{title:"Article10",content:"Kumar","likes":0,"author_id":1}) {
291+
message
292+
}
293+
}
294+
# delete a record
295+
mutation {
296+
deleteArticles(id:10) {
297+
message
298+
}
299+
}
300+
```
301+
302+
**Note**: All the data changes will be automatically saved to the json files.
303+
304+
## License
305+
306+
MIT

fast_json_server/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .json_server import start_server
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)