""" Voter Class object """ import logging import requests class Voter(): """Voter class""" def __init__(self, base_url, jwt): """ __init__ Init Voter :param base_url: Base URL for Voter API :param jwt: JWT Authentication for Voter API """ self.base_url = base_url self.jwt = jwt def get_all(self, args = {}): """ get Get the voter :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}' } url_args = "" prefix = "" for k in args: url_args += f"{prefix}{k}={args[k]}" prefix = "&" # Run the GET response = requests.get( f'{self.base_url}/api/v1/voters?{url_args}', headers=headers, timeout=300 ) # Good response. Return Voter if response.ok: return response.json() # Log error and retunr null logging.error("[AFVOTER GET ALL] [%s]", response.text) return None def get(self, voter_key): """ get Get the voter :param voter_key: The Voter 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/voters/{voter_key}', headers=headers, timeout=300 ) # Good response. Return Voter if response.ok: return response.json() # Log error and retunr null logging.error("[AFVOTER GET] [%s]", response.text) return None def patch(self, voter_key, patch_data): """ patch Path the Voter :param voter_key: The Voter key to update with API :param patch_data: The data to update the Voter """ # 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'] = voter_key # Run the GET response = requests.patch( f'{self.base_url}/api/v1/voters/{voter_key}', headers=headers, timeout=300, json=patch_data ) # Good response. Return Voter if response.ok: return response.json() # Log error and retunr null logging.error("[AFVOTER UPDATE] [%s]", response.text) return None def post(self, voter_key, post_data): """ post Post the Voter :param voter_key: The Voter key to update with API :param post_data: The data to create the Voter """ # Set the headers for the request headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {self.jwt}' } # Run the GET response = requests.post( f'{self.base_url}/api/v1/voters', headers=headers, timeout=300, json=post_data ) # Good response. Return Voter if response.ok: return response.json() # Log error and retunr null logging.error("[AFVOTER CREATE] [%s]", response.text) return None