-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathutils.py
More file actions
155 lines (127 loc) · 4.62 KB
/
utils.py
File metadata and controls
155 lines (127 loc) · 4.62 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
import logging
import random
import string
import sys
from .versioning import LATEST_API_VERSION, versioned
from flask import jsonify
err_map = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
422: "Unprocessable Entity",
429: "Rate Limit Exceeded",
500: "Server Error"
}
msg_map = {
400: "Bad Request. Did you provide valid JSON?",
401: "You must provide a valid API token in the x-apikey header.",
403: "This endpoint is forbidden.",
404: "The resource you requested does not exist.",
405: "This method is not allowed.",
422: "This request failed validation",
429: "You have exceeded your rate limit. Try again later.",
500: "Something went wrong"
}
class Paginator:
def __init__(self, configuration, request):
self.configuration = configuration
self.page = request.args.get('page', 1, type=int)
self.page_size = request.args.get('page_size', configuration.per_page, type=int)
if self.page_size > configuration.max_page_size:
self.page_size = configuration.max_page_size
def paginated_data(self, query):
data = query.paginate(self.page, self.page_size, False)
if self.page > data.pages:
return None
setattr(data, "per_page", self.page_size)
return data
def details(self, paginated_data):
return {
"details": {
"page": paginated_data.page,
"number_of_pages": paginated_data.pages,
"records_per_page": paginated_data.per_page,
"total_count": paginated_data.total,
"has_next": paginated_data.has_next,
"has_prev": paginated_data.has_prev
}
}
def format_resource_search(hit):
formatted = {
'id': hit['id'],
'name': hit['name'],
'url': hit['url'],
'category': hit['category'],
'languages': hit['languages'],
'free': hit['free'],
'notes': hit['notes'],
'upvotes': hit['upvotes'],
'downvotes': hit['downvotes'],
'times_clicked': hit['times_clicked'],
'created_at': hit['created_at'],
'last_updated': hit['last_updated'],
}
return formatted
@versioned(throw_on_invalid=False)
def standardize_response(
payload={},
status_code=200,
version=LATEST_API_VERSION,
datatype="data"):
"""Response helper
This simplifies the response creation process by providing an internally
defined mapping of status codes to messages for errors. It also knows when
to respond with a server error versus when to be 'ok' based on the keys
present in the supplied payload.
If the payload has falsey data and no error key defined, it responds with
a 500.
Arguments:
payload -- None or a dict with 'data' or 'error' keys, 'data' should be
json serializable
status_code -- a valid HTTP status code. For errors it defaults to 500,
for 'ok' it defaults to 200
"""
data = payload.get("data")
errors = payload.get("errors")
details = payload.get("details")
resp = dict(
apiVersion=version,
status="ok",
status_code=status_code
)
if status_code >= 400 and err_map.get(status_code):
resp["status"] = err_map.get(status_code)
resp["status_code"] = status_code
if errors:
resp["errors"] = errors
else:
code = get_error_code_from_status(status_code)
message = msg_map.get(status_code)
resp["errors"] = {}
resp["errors"][code] = {"message": message}
elif not data:
# 500 Error case -- Something went wrong.
message = msg_map.get(500)
resp["errors"] = {"server-error": {"message": message}}
resp["status_code"] = 500
resp["status"] = err_map.get(500)
else:
resp[datatype] = data
if details:
resp.update(details)
return jsonify(resp), resp["status_code"], {'Content-Type': 'application/json'}
def setup_logger(name, level=logging.INFO):
"""Function setup as many loggers as you want"""
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
return logger
def get_error_code_from_status(status_code):
return '-'.join(err_map.get(status_code).split(' ')).lower()
def random_string(n=10):
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=n))