-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (127 loc) · 6.05 KB
/
main.py
File metadata and controls
157 lines (127 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# main.py
from flask import Flask, jsonify, request
from flask_restx import Api, Resource, fields
import requests
import json
#######################Settings############################
flask_app = Flask(__name__)
app = Api(app=flask_app,
version="1.0",
title="Post Manager",
description="Manage posts associated to users from external API")
name_space = app.namespace("", description='User posts API')
###########################################################
#####################External API Data#####################
posts_api = requests.get('https://jsonplaceholder.typicode.com/posts')
posts_data = posts_api.text
external_posts = json.loads(posts_data)
users_api = requests.get('https://jsonplaceholder.typicode.com/users')
users_data = users_api.text
users = json.loads(users_data)
############################################################
###################Local Mock Data##########################
posts = [{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}]
############################################################
########################Models##############################
post_model = app.model('Post',
{'id': fields.Integer(Required=True),
'userId': fields.Integer(Required=True),
'title': fields.String(Required=True),
'body': fields.String})
############################################################
####################Adding a post###########################
@name_space.route("/posts")
class PostsCreateResource(Resource):
@app.expect(post_model)
@app.doc(responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'})
def post(self):
try:
data = request.get_json()
for i in range(0, users):
if users[i]['id'] == data['userId']:
for j in range(0, posts):
if users[i]['id'] == data['userId'] and posts[j]['id'] == data['id']:
name_space.abort(
500, e.__doc__, status="Could not retrieve information", statusCode="400")
posts.append(data)
return "Post added"
except Exception as e:
name_space.abort(
400, e.__doc__, status="Could not retrieve information", statusCode="400")
######Getting, updating and deleting a post by post id######
@name_space.route("/posts<int:id>")
class PostsResource(Resource):
@app.doc(responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'},
params={'id': 'Specify the Id associated with the person'})
def get(self, id):
try:
for i in range(0, len(posts)):
if posts[i]['id'] == id:
return jsonify(posts[i])
for i in range(0, len(external_posts)):
if external_posts[i]['id'] == id:
return jsonify(external_posts[i])
name_space.abort(
500, e.__doc__, status="Could not retrieve information", statusCode="500")
except Exception as e:
name_space.abort(
400, e.__doc__, status="Could not retrieve information", statusCode="400")
@app.expect(post_model)
@app.doc(responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'},
params={'id': 'Specify the Id associated with the person'})
def put(self, id):
try:
data = request.get_json()
for i in range(0,len(posts)):
if posts[i]['id']==id and data['userId'] == posts[i]['userId']:
posts[i]['title'] = data['title']
posts[i]['body'] = data['body']
return "Post updated"
name_space.abort(
500, e.__doc__, status="Could not retrieve information", statusCode="500")
except Exception as e:
name_space.abort(
400, e.__doc__, status="Could not retrieve information", statusCode="400")
@app.doc(responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'},
params={'id': 'Specify the Id associated with the person'})
def delete(self, id):
try:
for i in range(0, len(posts)):
if posts[i]['id'] == id:
posts.pop(i)
return "Post deleted"
name_space.abort(
500, e.__doc__, status="Could not retrieve information", statusCode="500")
except Exception as e:
name_space.abort(
400, e.__doc__, status="Could not retrieve information", statusCode="400")
####################Getting posts by userId#######################
@name_space.route("/posts/users<int:id>")
class UsersResouces(Resource):
@app.doc(responses={200: 'OK', 400: 'Invalid Argument', 500: 'Mapping Key Error'},
params={'id': 'Specify the Id associated with the person'})
def get(self, id):
user_posts = []
try:
for i in range(0, len(users)):
if users[i]['id'] == id:
for i in range(0, len(posts)):
if posts[i]['userId'] == id:
user_posts.append(posts[i])
return jsonify(user_posts)
name_space.abort(
500, e.__doc__, status="Could not retrieve information", statusCode="500")
except Exception as e:
name_space.abort(
400, e.__doc__, status="Could not retrieve information", statusCode="400")