-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (30 loc) · 996 Bytes
/
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
from exceptions import APIPaginationError
def build_link_header(query, base_url, per_page):
"""
Adds pagination link headers to an API response.
"""
links = [
'<{0}?page=1&per-page={1}>; rel="first"'.format(base_url, per_page),
'<{0}?page={1}&per-page={2}>; rel="last"'.format(
base_url, query.pages, per_page
),
]
if query.has_prev:
links.append(
'<{0}?page={1}&per-page={2}>; rel="prev"'.format(
base_url, query.prev_num, per_page
)
)
if query.has_next:
links.append(
'<{0}?page={1}&per-page={2}>; rel="next"'.format(
base_url, query.next_num, per_page
)
)
links = ",".join(links)
return dict(Link=links)
def validate_per_page(per_page):
if per_page and per_page > 100 or per_page < 1:
raise APIPaginationError(
"per-page parameter must be between 1 and 100")
return per_page