|
1
|
|
|
# ==================================================================================== # |
|
2
|
|
|
# worker.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 .validators import validate_response |
|
29
|
|
|
from .exceptions import BadRequestError |
|
30
|
|
|
from .session import SessionSingleton |
|
31
|
|
|
from . import paths |
|
32
|
|
|
from aiohttp import ClientError |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
# ==================================================================================== # |
|
36
|
|
|
async def request(endpoint: str, kwargs: dict) -> tuple: |
|
37
|
|
|
""" |
|
38
|
|
|
The main function responsible for making the |
|
39
|
|
|
web requests to the Yahoo Finance API servers. |
|
40
|
|
|
""" |
|
41
|
|
|
path = get_path(endpoint, kwargs) |
|
42
|
|
|
sanitize_booleans_to_strings(kwargs) |
|
43
|
|
|
try: |
|
44
|
|
|
ss = SessionSingleton() |
|
45
|
|
|
async with ss.session.get(url=path, params=kwargs) as resp: |
|
46
|
|
|
data = await resp.json() |
|
47
|
|
|
await validate_response(data) |
|
48
|
|
|
error = None |
|
49
|
|
|
except (ClientError, BadRequestError) as ex: |
|
50
|
|
|
error = build_error(ex) |
|
51
|
|
|
data = None |
|
52
|
|
|
return data, error |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
# ------------------------------------------------------------------------------------ # |
|
56
|
|
|
def get_path(endpoint: str, params: dict) -> str: |
|
57
|
|
|
path = paths.get(endpoint) |
|
58
|
|
|
if '{symbol}' in path: |
|
59
|
|
|
sym = params.pop('symbol', '') |
|
60
|
|
|
path = path.format(symbol=sym) |
|
61
|
|
|
return path |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
# ------------------------------------------------------------------------------------ # |
|
65
|
|
|
def sanitize_booleans_to_strings(params: dict) -> None: |
|
66
|
|
|
for key, value in params.items(): |
|
67
|
|
|
if isinstance(value, bool): |
|
68
|
|
|
params[key] = str(value).lower() |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
# ------------------------------------------------------------------------------------ # |
|
72
|
|
|
def build_error(ex=None) -> dict: |
|
73
|
|
|
default_message = 'Internal server error' |
|
74
|
|
|
default_status = 500 |
|
75
|
|
|
return dict( |
|
76
|
|
|
name='HTTPError', |
|
77
|
|
|
status=getattr(ex, 'status', default_status), |
|
78
|
|
|
message=getattr(ex, 'message', default_message) |
|
79
|
|
|
) |
|
80
|
|
|
|