-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
57 lines (48 loc) · 1.8 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
from flask import Request
import re
from logic.bookmarks import (
get_bookmarks,
get_bookmarks_last_updated,
get_bookmarks_version,
post_bookmarks,
put_bookmarks,
)
from logic.info import get_info
from common.exceptions import BadRequestException, NotFoundException
from common.utils import bad_request, method_not_allowed, not_found, parse_request
from models.bookmarks import BookmarksPatch, Version
def info(request: Request):
if request.method != "GET":
return method_not_allowed(["GET"])
return get_info()
def bookmarks(request: Request):
path = request.path.rstrip("/")
try:
if not path:
if request.method != "POST":
return method_not_allowed(["POST"])
return post_bookmarks(parse_request(request, Version))
else:
match = re.match(r"/(\w+)$", path)
if match:
if request.method == "GET":
return get_bookmarks(match[1])
elif request.method == "PUT":
return put_bookmarks(match[1], parse_request(request, BookmarksPatch))
else:
return method_not_allowed(["GET", "PUT"])
match = re.match(r"/(\w+)/lastUpdated$", path)
if match:
if request.method != "GET":
return method_not_allowed(["GET"])
return get_bookmarks_last_updated(match[1])
match = re.match(r"/(\w+)/version$", path)
if match:
if request.method != "GET":
return method_not_allowed(["GET"])
return get_bookmarks_version(match[1])
except BadRequestException as e:
return bad_request(str(e))
except NotFoundException as e:
return not_found()
return not_found()