""" Event Class object """ import logging import requests class Event(): """Event class""" def __init__(self, base_url, jwt): """ __init__ Init Event :param base_url: Base URL for Event API :param jwt: JWT Authentication for Event API """ self.base_url = base_url self.jwt = jwt def get_all(self, args = None): """ get_all Get the events :param args: Dict of args to add as URL params """ # Set the headers for the request headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {self.jwt}' } if not isinstance(args, dict): args = {} # Run the GET response = requests.get( f'{self.base_url}/api/v1/events', params=args, headers=headers, timeout=300 ) # Good response. Return Events if response.ok: return response.json() # Log error and retunr null logging.error("[AFEVENT GET ALL] [%s]", response.text) return None def get(self, event_key): """ get Get the event :param event_key: The Event key to get with API """ # Set the headers for the request headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {self.jwt}' } # Run the GET response = requests.get( f'{self.base_url}/api/v1/events/{event_key}', headers=headers, timeout=300 ) # Good response. Return Event if response.ok: return response.json() # Log error and retunr null logging.error("[AFEVENT GET] [%s]", response.text) return None def patch(self, event_key, patch_data): """ patch Path the Event :param event_key: The Event key to update with API :param patch_data: The data to update the Event """ # Set the headers for the request headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {self.jwt}' } # Set the key to the patch data patch_data['key'] = event_key # Run the GET response = requests.patch( f'{self.base_url}/api/v1/events/{event_key}', headers=headers, timeout=300, json=patch_data ) # Good response. Return Event if response.ok: return response.json() # Log error and retunr null logging.error("[AFEVENT UPDATE] [%s]", response.text) try: return response.json() except: return None