summaryrefslogtreecommitdiffstats
path: root/webapi/web_api.py
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2024-04-13 01:08:10 -0700
committerPinapelz <yukais@pinapelz.com>2024-04-13 01:08:10 -0700
commit163e07adb573da8497c9ccf0ae97a6c261e7b14a (patch)
tree72c1763fbb76b8fc24b318ffa7c4028273ebf01b /webapi/web_api.py
initial commit. Migration from GitLab
Diffstat (limited to 'webapi/web_api.py')
-rw-r--r--webapi/web_api.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/webapi/web_api.py b/webapi/web_api.py
new file mode 100644
index 0000000..525994c
--- /dev/null
+++ b/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
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage