-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
173 lines (133 loc) · 5.04 KB
/
main.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import flask
import postgresql
import json
import re
app = flask.Flask(__name__)
def db_connection():
return postgresql.open("pq://postgres:postgres@localhost:5432/birds_db")
def to_json(data):
return json.dumps(data)
def response(code, data):
return flask.Response(
status=code,
mimetype="application/json",
response=to_json(data)
)
def check_limit_offset_params():
limit = flask.request.args.get('limit')
offset = flask.request.args.get('offset')
errors = []
params = ''
if (limit is None or limit == '') or (offset is None or offset == ''):
errors.append("Param limit/offset is missing")
else:
incorrect_limit_param = re.sub(r'\d+', '', limit)
if incorrect_limit_param:
errors.append("Param limit should be positive number")
else:
incorrect_offset_param = re.sub(r'\d+', '', offset)
if incorrect_offset_param:
errors.append("Param offset should be positive number")
else:
params = params + " LIMIT " + limit + " OFFSET " + offset
return params, errors
def params_validate():
attribute = flask.request.args.get('attribute')
order = flask.request.args.get('order')
error = []
errors_attr_order_params = []
params = ''
if (attribute is None or attribute == '') or (order is None or order == ''):
errors_attr_order_params.append("Param attribute/order is missing")
else:
incorrect_attr_param = re.sub(r'species|name|color|body_length|wingspan', '', attribute)
if incorrect_attr_param:
errors_attr_order_params.append("Param attribute not found")
else:
order = str(order).upper()
incorrect_order_param = re.sub(r'ASC|DESC', '', order)
if incorrect_order_param:
errors_attr_order_params.append("Param order should be ASC or DESC")
else:
params = "ORDER BY " + str(attribute) + " " + order
(limit_offset_params, errors_limit_offset_params) = check_limit_offset_params()
if errors_attr_order_params:
if errors_limit_offset_params:
error.append(errors_attr_order_params[0] + " OR " + errors_limit_offset_params[0])
else:
params = params + limit_offset_params
else:
if not errors_limit_offset_params:
params = params + limit_offset_params
return params, error
def bird_validate():
errors = []
json_obj = flask.request.get_json()
if json_obj is None:
errors.append(
"Incorrect JSON")
return None, errors
for field_name in ['species', 'name', 'color']:
if type(json_obj.get(field_name)) is not str:
errors.append(
"Field '{}' is missing or is not a string".format(field_name))
for field_name in ['body_length', 'wingspan']:
if type(json_obj.get(field_name)) is not int:
errors.append(
"Field '{}' is missing or is not a int ".format(field_name))
if json_obj.get(field_name) <= 0:
errors.append(
"Value of field '{}' should be more than 0 ".format(field_name))
return json_obj, errors
@app.route('/')
def root():
return flask.redirect('/birds')
@app.route('/version', methods=['GET'])
def get_version():
return "Birds Service. Version 0.1"
@app.route('/birds', methods=['GET'])
def get_birds():
query = "SELECT species, name, color, body_length, wingspan FROM birds "
params = ''
if len(flask.request.args) != 0:
(params, errors) = params_validate()
if errors:
return response(400, {"errors": errors})
query = query + params
with db_connection() as db:
tuples = db.query(query)
birds = []
for (species, name, color, body_length, wingspan) in tuples:
birds.append({
"species": species,
"name": name,
"color": color,
"body_length": body_length,
"wingspan": wingspan
})
resp = birds
if len(flask.request.args) != 0:
resp = {"query": query, "results": resp}
return response(200, resp)
@app.route('/birds', methods=['POST'])
def add_bird():
(json_obj, errors) = bird_validate()
if errors:
return response(400, {"errors": errors})
with db_connection() as db:
try:
insert = db.prepare(
"INSERT INTO birds (species, name, color, body_length, wingspan) VALUES ($1, $2, $3, $4, $5) " +
"RETURNING name")
[(name,)] = insert(
json_obj['species'],
json_obj['name'],
json_obj['color'],
json_obj['body_length'],
json_obj['wingspan'])
return response(200, {"bird_name": name})
except Exception:
errors.append("This entry cannot be added to the base")
return response(400, {"errors": errors})
if __name__ == "__main__":
app.run(port=8080, debug=True)