Begin implementation of OwlBoard API Class

This commit is contained in:
Fred Boniface 2024-10-28 14:12:20 +00:00
parent 11ec1329a5
commit c8dbcd9b25
2 changed files with 37 additions and 5 deletions

View File

@ -77,11 +77,6 @@ def main():
out.write(json.dumps(schedule_cards, indent=4, default=str)) out.write(json.dumps(schedule_cards, indent=4, default=str))
out.close() 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__": if __name__ == "__main__":
main() main()

37
src/owlboard.py Normal file
View File

@ -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