From 96019367e8f72eac26abd3b7a908c2b914bd1ae1 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Fri, 17 Nov 2023 13:24:42 -0800 Subject: v2: add initial Next JS files, remove static templates --- backend/webapi/web_api.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 backend/webapi/web_api.py (limited to 'backend/webapi/web_api.py') diff --git a/backend/webapi/web_api.py b/backend/webapi/web_api.py new file mode 100644 index 0000000..525994c --- /dev/null +++ b/backend/webapi/web_api.py @@ -0,0 +1,29 @@ +import urllib.request +import json + + +class WebAPI: + """ + General class for interacting with web APIs + """ + + def __init__(self, api_key: str, base_url: str) -> None: + self.api_key = api_key + self.base_url = base_url + + def _download_url(self, query: str, header = 'X-APIKEY') -> dict: + """ + Downloads the URL and returns the result as a string + param: + query: str - the query to be appended to the base URL + """ + if self.api_key is None: + raise Exception("API key not set") + opener = urllib.request.build_opener() + opener.addheaders = [(header, self.api_key)] + urllib.request.install_opener(opener) + response = urllib.request.urlopen(self.base_url + query) + json_results = response.read() + r_obj = json.loads(json_results) + response.close() + return r_obj -- cgit v1.2.3