From c8dbcd9b258b880f426fa8ddfbbe48767bfb508f Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 28 Oct 2024 14:12:20 +0000 Subject: [PATCH] Begin implementation of OwlBoard API Class --- src/main.py | 5 ----- src/owlboard.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/owlboard.py diff --git a/src/main.py b/src/main.py index e0587c9..3d414b6 100644 --- a/src/main.py +++ b/src/main.py @@ -77,11 +77,6 @@ def main(): out.write(json.dumps(schedule_cards, indent=4, default=str)) out.close() - ## For each file in list, run get_schedule_card_data(filepath) - ## if returned value is not None, append to a list. Once - ## complete, pass to a validation function, then into Git handling - ## function. - if __name__ == "__main__": main() \ No newline at end of file diff --git a/src/owlboard.py b/src/owlboard.py new file mode 100644 index 0000000..d481d14 --- /dev/null +++ b/src/owlboard.py @@ -0,0 +1,37 @@ +import requests +from urllib.parse import urljoin + +class APIConnectionError(Exception): + # API Connection has failed + pass + +## CONTANTS + +API_TEST_PATH = "" +API_HEADCODE_PATH = "" +API_TRAIN_UID_PATH = "" + +class OwlBoard_API_Connection: + + def __init__(self, api_key, base_url): + self.api_key = api_key + self.base_url = base_url + + self.request_headers = { + 'Content-Type': 'application/json', + 'uuid': self.api_key + } + + if not self.test_connection(): + raise APIConnectionError(f"failed to connect to API at {self.base_url} with supplied API key") + + + def test_connection(self): + try: + request_url = urljoin(self.base_url, API_TEST_PATH) + response = requests.get(request_url, headers=self.request_headers) + response.raise_for_status() + return True + except requests.RequestException as e: + print(f"Connection rest failed: {e}") + return False \ No newline at end of file