diff --git a/lib_af_api/__init__.py b/lib_af_api/__init__.py index b2e6b02..a3ddf85 100644 --- a/lib_af_api/__init__.py +++ b/lib_af_api/__init__.py @@ -7,4 +7,5 @@ A Python library for managing AF API objects from lib_af_api.campaign import Campaign from lib_af_api.event import Event from lib_af_api.voterset import VoterSet +from lib_af_api.voter import Voter diff --git a/lib_af_api/voter.py b/lib_af_api/voter.py new file mode 100644 index 0000000..c3087de --- /dev/null +++ b/lib_af_api/voter.py @@ -0,0 +1,122 @@ +""" +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 diff --git a/setup.py b/setup.py index 71eac9d..3925f3f 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ with open('README.md', 'r') as f: setup( name='lib_af_api', - version='0.1.0', + version='0.1.1', author='', author_email='', description='',