forked from igbasly/BC_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
286 lines (235 loc) · 8.05 KB
/
app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from endpoints_functions import check_arguments, manage_requirements, manage_vacancies
from Requests import (request_buscacursos, request_vacancy,
request_requirements, request_parameters)
from flask import Flask, request, send_from_directory, redirect
from flask_cors import CORS
import json
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
def response(code: int, data: dict = None):
""" Create the response for any request based on the status code.
Args:
code (int): Status code of the response
data (dict): Information to be returned if required.
Returns:
dict: Response in dictionary format with all the information about the\
request.
int: Status code of response.
Codes:
200: "Ok"
204: "No Content"
400: "Bad Request"
403: "Forbidden"
404: "Not Found"
405: "Method Not Allowed"
500: "Internal Server Error"
503: "Service Unavailable"
"""
codes = {
200: "Ok",
202: "Accepted",
204: "No Content",
400: "Bad Request",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
500: "Internal Server Error",
503: "Service Unavailable",
}
response_template = {"code": code, "status": codes[code]}
if code // 400 >= 1:
response_template["error"] = data
else:
response_template["data"] = data
return response_template, code
@app.route("/favicon.ico", methods=["GET"])
def icon():
""" Return the favicon for explorers
Returns:
'Favicon' file
"""
return send_from_directory("Files", "favicon.png")
@app.route("/", methods=["GET"])
def index():
return redirect("https://igbasly.github.io/BC_API")
@app.route("/api/v1", methods=["GET"])
def BC_API_get(vacantes=False, formato=False):
""" HTTP GET method for v1
Args:
vancante (bool): Allow the use of 'vacantes' parameters in the request.
Returns:
dict: Response in dictionary format with all information about the\
request.
int: Status code of response.
"""
arguments = request.args
parameters, bad_arguments = check_arguments(
arguments, vacantes, formato)
if not arguments:
return response(400, {"message": "(#400) Requests with no arguments."})
if bad_arguments:
return response(
400,
{
"message": "(#400) Some arguments are not accepted.",
"invalid_arguments": bad_arguments,
},
)
try:
data_classes = request_buscacursos(parameters)
except Exception as exc:
print(exc)
return response(
500, {
"message": "(#500) An internal error ocurred, we are working on it."}
)
if len(data_classes) > 0:
return response(200, data_classes)
return response(202, {"message": "(#202) No data found with those parameters."})
@app.route("/api/v1", methods=["POST", "PUT", "PATCH", "DELETE"])
@app.route("/api/v2", methods=["POST", "PUT", "PATCH", "DELETE"])
@app.route("/api/v3", methods=["POST", "PUT", "PATCH", "DELETE"])
def BC_API_post():
return response(
405, {
"message": "(#405) This API does not accept PUT or POST methods."}
)
@app.route("/api/v2", methods=["GET"])
def BC_API_v2_get():
""" HTTP GET method for v2
This methods allows use of 'vacantes' parameter in the request.
Returns:
dict: Response in dictionary format with all information about the\
request.
int: Status code of response.
"""
resp, code = BC_API_get(True)
if "vacantes" in request.args and code == 200:
if request.args["vacantes"] == "true":
for cla in resp["data"].values():
for sec in cla.values():
vacancy = request_vacancy(sec["NRC"], sec["Semestre"])
sec["Vacantes"] = vacancy
total = sec.pop("Vacantes totales")
sec["Vacantes"]["Totales"] = total
available = sec.pop("Vacantes disponibles")
sec["Vacantes"]["Disponibles"] = available
elif request.args["vacantes"] != "false":
return response(
400,
{
"message": "(#400) Parameter 'vacantes' "
+ "only accepts boolean values."
},
)
return resp, code
@app.route("/api/v3", methods=["GET"])
def BC_API_v3_get():
""" HTTP GET method for v3
This method allows the use of 'requisitos' parameter in the request.
Return:
dict: Response in dictionary format with all information about the\
request.
int: Status code of response.
"""
vac = False
form = False
if "vacantes" in request.args and request.args["vacantes"] not in ["true", "false"]:
return response(
400,
{
"message": "(#400) Parameter 'vacantes' "
"only accepts boolean values."
}
)
elif "vacantes" in request.args and request.args["vacantes"] == "true":
vac = True
if "formato" in request.args and request.args["formato"] not in ["true", "false"]:
return response(
400,
{
"message": "(#400) Parameter 'formato' "
"only accepts boolean values."
}
)
elif "formato" in request.args and request.args["formato"] == "true":
form = True
if "requisitos" in request.args and request.args["requisitos"] not in [
"true",
"false",
]:
return response(
400,
{
"message": "(#400) Parameter 'requisitos' "
"only accepts boolean values."
}
)
resp, code = BC_API_get(vac, form)
if "vacantes" in request.args and code == 200 and vac:
resp["data"] = manage_vacancies(resp["data"])
if (
"requisitos" in request.args
and code == 200
and request.args["requisitos"] == "true"
):
resp["data"] = manage_requirements(resp["data"])
return resp, code
@app.route("/api/v3/requisitos", methods=["GET"])
def BC_API_v3_req_get():
""" HTTP GET method for v3 with 'requisitos' scope.
This method returns course requisities associated with an identifier.
Return:
dict: Response in dictionary format with all information about the\
request.
int: Status code of response.
"""
denied = []
sigla = ""
for a in request.args:
if a == "sigla":
sigla = request.args[a]
else:
denied.append(a)
if denied:
return response(
400,
{
"message": "(#400) Some arguments are not accepted.",
"invalid_arguments": denied,
},
)
if not sigla:
return response(400, {"message": "(#400) No value for the 'sigla' parameter."})
info = request_requirements(sigla)
i = 0
for value in info.values():
if not value:
i += 1
break
if not i:
return response(202, {"message": "(#202) No data found with these parameters."})
return response(200, {sigla: info})
@app.route("/api/v3/parametros", methods=["GET"])
def BC_API_v3_params_get():
""" HTTP GET method for v3 with 'requisitos' scope.
This method returns course requisities associated with an identifier.
Return:
dict: Response in dictionary format with all information about the\
request.
int: Status code of response.
"""
if len(request.args) > 0:
return response(
400,
{"message": "(#400) Arguments not accepted with this endpoint."}
)
params = request_parameters()
return response(200, {"parametros": params})
if __name__ == "__main__":
params = {
# "debug": True,
# "host": "192.168.1.8"
}
app.run(**params)