|
1
|
|
|
# ==================================================================================== # |
|
2
|
|
|
# handler.py - This file is part of the YFrake package. # |
|
3
|
|
|
# ------------------------------------------------------------------------------------ # |
|
4
|
|
|
# # |
|
5
|
|
|
# MIT License # |
|
6
|
|
|
# # |
|
7
|
|
|
# Copyright (c) 2022 Mattias Aabmets # |
|
8
|
|
|
# # |
|
9
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy # |
|
10
|
|
|
# of this software and associated documentation files (the "Software"), to deal # |
|
11
|
|
|
# in the Software without restriction, including without limitation the rights # |
|
12
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # |
|
13
|
|
|
# copies of the Software, and to permit persons to whom the Software is # |
|
14
|
|
|
# furnished to do so, subject to the following conditions: # |
|
15
|
|
|
# # |
|
16
|
|
|
# The above copyright notice and this permission notice shall be included in all # |
|
17
|
|
|
# copies or substantial portions of the Software. # |
|
18
|
|
|
# # |
|
19
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # |
|
20
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # |
|
21
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # |
|
22
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # |
|
23
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # |
|
24
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # |
|
25
|
|
|
# SOFTWARE. # |
|
26
|
|
|
# # |
|
27
|
|
|
# ==================================================================================== # |
|
28
|
|
|
from .. import client |
|
29
|
|
|
from ..client.worker import build_error |
|
30
|
|
|
from ..client.exceptions import BadRequestError |
|
31
|
|
|
from multidict import MultiDictProxy |
|
32
|
|
|
from aiohttp import web |
|
33
|
|
|
import json |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
# ==================================================================================== # |
|
37
|
|
|
async def handler(request: web.Request) -> web.Response: |
|
38
|
|
|
""" |
|
39
|
|
|
This func receives all the incoming requests to the server |
|
40
|
|
|
and forwards them to the correct endpoint handlers. |
|
41
|
|
|
""" |
|
42
|
|
|
|
|
43
|
|
|
params = convert_multidict(request.query) |
|
44
|
|
|
params = convert_datatypes(params) |
|
45
|
|
|
endpoint = request.path.strip('/') |
|
46
|
|
|
try: |
|
47
|
|
|
resp = client.get(endpoint, **params) |
|
48
|
|
|
await resp.wait() |
|
49
|
|
|
error = resp.error |
|
50
|
|
|
data = resp.data |
|
51
|
|
|
|
|
52
|
|
|
except (NameError, TypeError, KeyError, BadRequestError): |
|
53
|
|
|
error = build_error(BadRequestError) |
|
54
|
|
|
data = None |
|
55
|
|
|
|
|
56
|
|
|
result = dict( |
|
57
|
|
|
endpoint=endpoint, |
|
58
|
|
|
error=error, |
|
59
|
|
|
data=data |
|
60
|
|
|
) |
|
61
|
|
|
text = json.dumps(result, indent=3) |
|
62
|
|
|
return web.Response(text=text) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
# ------------------------------------------------------------------------------------ # |
|
66
|
|
|
def convert_multidict(multidict: MultiDictProxy) -> dict: |
|
67
|
|
|
out = dict() |
|
68
|
|
|
for key in multidict.keys(): |
|
69
|
|
|
if key not in out: |
|
70
|
|
|
out[key] = multidict[key] |
|
71
|
|
|
return out |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
# ------------------------------------------------------------------------------------ # |
|
75
|
|
|
def convert_datatypes(params: dict) -> dict: |
|
76
|
|
|
for key, value in params.items(): |
|
77
|
|
|
try: |
|
78
|
|
|
params[key] = int(value) |
|
79
|
|
|
except ValueError: |
|
80
|
|
|
pass |
|
81
|
|
|
|
|
82
|
|
|
if value.lower() == 'true': |
|
83
|
|
|
params[key] = True |
|
84
|
|
|
if value.lower() == 'false': |
|
85
|
|
|
params[key] = False |
|
86
|
|
|
|
|
87
|
|
|
return params |
|
88
|
|
|
|