-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
78 lines (61 loc) · 2.14 KB
/
utils.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
from typing import Dict
from xml.etree import ElementTree as ET
from flask import make_response, jsonify, current_app
JSON_MIME_TYPE = "application/json"
def get_supported_protocols() -> Dict[str, str]:
return current_app.config["AUTH_PROTOCOLS"]
def json_response(data="", status=200, headers=None):
headers = headers or {}
if "Content-Type" not in headers:
headers["Content-Type"] = JSON_MIME_TYPE
json_data = jsonify({"data": data})
return make_response(json_data, status, headers)
def get_request_data(request):
"""
Gets the data from the request
"""
# https://stackoverflow.com/questions/10434599/how-to-get-data-received-in-flask-request/25268170
data = request.form.to_dict() if request.form else request.get_json()
if not data:
return {}
return data
def is_xml(data):
"""
Check if string is XML or not by trying to parse it
Empty strings also raise Parse error
"""
try:
ET.fromstring(data)
return True
except ET.ParseError:
return False
def validate_protocol(protocol, supported_protocols: Dict[str, str]):
"""
Checks if the protocol is contained in the supported methods
:param protocol: the protocol
:param supported_protocols: a dict
"""
if protocol not in supported_protocols:
return json_response(
"The protocol is invalid. Accepted protocols: {}".format(
str(supported_protocols)
),
400,
)
def validate_protocol_data(data, supported_protocols: Dict[str, str]):
"""
Checks if the protocol in the passed dictionary is correct
"""
if "protocol" not in data:
return json_response(
"The request is missing 'protocol'. It must be passed in the form data", 400
)
return validate_protocol(data["protocol"], supported_protocols)
class ResourceNotFoundError(Exception):
pass
class KeycloakAPIError(Exception):
def __init__(self, status_code, message):
obj = {"status_code": status_code, "message": message}
super().__init__(obj)
self.status_code = status_code
self.message = message