This repository has been archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
55 lines (44 loc) · 1.83 KB
/
util.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
import json
import logging
from typing import Any, Dict, Optional, Union
import httpx
log: logging.Logger = logging.getLogger(__name__)
class Utility:
"""Class containing utilitarian functions intended to reduce duplicate code."""
def GET(
self: Any,
url: str,
headers: Dict[str, str],
parameters: Dict[str, str] = {"language": "en"},
) -> Optional[dict]:
"""
Return the response of a successful HTTP GET request to the specified
URL with the optionally provided header values.
"""
res: httpx.Response = httpx.get(url, headers=headers, params=parameters)
# HTTP 200 (OK)
if res.status_code == 200:
if res.headers["Content-Type"].lower() == "application/json; charset=utf-8":
return res.json()
else:
log.error(f"Failed to GET {url} (HTTP {res.status_code})")
def ReadFile(self: Any, filename: str, extension: str) -> Optional[dict]:
"""Read and return the contents of the specified file."""
try:
with open(f"{filename}.{extension}", "r", encoding="utf-8") as file:
if extension == "json":
return json.loads(file.read())
except Exception as e:
log.error(f"Failed to read {filename}.{extension}, {e}")
def WriteFile(
self: Any, filename: str, extension: str, data: Union[str, dict, list]
) -> None:
"""Write the provided data to the specified file."""
try:
with open(f"{filename}.{extension}", "w", encoding="utf-8") as file:
if extension == "json":
file.write(json.dumps(data, indent=4))
else:
file.write(data)
except Exception as e:
log.error(f"Failed to write {filename}.{extension}, {e}")