From 4acb30b95b075ca32093b1a6a2e8e575cafdb09d Mon Sep 17 00:00:00 2001 From: nolan Date: Thu, 7 Dec 2023 20:34:34 -0800 Subject: [PATCH] initial commit --- .gitignore | 23 +++++++++++ README.md | 1 + lib_af_api/__init__.py | 10 +++++ lib_af_api/campaign.py | 86 ++++++++++++++++++++++++++++++++++++++++++ lib_af_api/event.py | 86 ++++++++++++++++++++++++++++++++++++++++++ lib_af_api/voterset.py | 86 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 25 ++++++++++++ 7 files changed, 317 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 lib_af_api/__init__.py create mode 100644 lib_af_api/campaign.py create mode 100644 lib_af_api/event.py create mode 100644 lib_af_api/voterset.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c078a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +venv/ +test-venv/ + +*.pyc +__pycache__/ + +instance/ + +.pytest_cache/ +.coverage +htmlcov/ + +dist/ +build/ +*.egg-info/ + +node_modules/ + +.envs +session_files/ +mongo_data/ +test_data/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..06b5202 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Library AF API diff --git a/lib_af_api/__init__.py b/lib_af_api/__init__.py new file mode 100644 index 0000000..b2e6b02 --- /dev/null +++ b/lib_af_api/__init__.py @@ -0,0 +1,10 @@ +""" +lib_af_api +--------------------------------------------- +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 + diff --git a/lib_af_api/campaign.py b/lib_af_api/campaign.py new file mode 100644 index 0000000..0738ce8 --- /dev/null +++ b/lib_af_api/campaign.py @@ -0,0 +1,86 @@ +""" +Campaign Class object +""" + +import logging +import requests + + +class Campaign(): + """Campaign class""" + + def __init__(self, base_url, jwt): + """ + __init__ Init Campaign + + :param base_url: Base URL for Campaign API + :param jwt: JWT Authentication for Campaign API + """ + + self.base_url = base_url + self.jwt = jwt + + + def get(self, campaign_key): + """ + get Get the campaign + + :param campaign_key: The Campaign 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/campaigns/{campaign_key}', + headers=headers, + timeout=300 + ) + + # Good response. Return Campaign + if response.ok: + return response.json() + + # Log error and retunr null + logging.error("[AFCAMPAIGN GET] [%s]", response.text) + return None + + + def patch(self, campaign_key, patch_data): + """ + patch Path the Campaign + + :param campaign_key: The Campaign key to update with API + :param patch_data: The data to update the Campaign + """ + + # 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'] = campaign_key + + # Run the GET + response = requests.patch( + f'{self.base_url}/api/v1/campaigns/{campaign_key}', + headers=headers, + timeout=300, + json=patch_data + ) + + # Good response. Return Campaign + if response.ok: + return response.json() + + # Log error and retunr null + logging.error("[AFCAMPAIGN UPDATE] [%s]", response.text) + return None diff --git a/lib_af_api/event.py b/lib_af_api/event.py new file mode 100644 index 0000000..745d9a7 --- /dev/null +++ b/lib_af_api/event.py @@ -0,0 +1,86 @@ +""" +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(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) + return None diff --git a/lib_af_api/voterset.py b/lib_af_api/voterset.py new file mode 100644 index 0000000..de99cc5 --- /dev/null +++ b/lib_af_api/voterset.py @@ -0,0 +1,86 @@ +""" +VoterSet Class object +""" + +import logging +import requests + + +class VoterSet(): + """VoterSet class""" + + def __init__(self, base_url, jwt): + """ + __init__ Init VoterSet + + :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(self, voterset_key): + """ + get Get the voterset + + :param voterset_key: The Voterset 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/votersets/{voterset_key}', + headers=headers, + timeout=300 + ) + + # Good response. Return VoterSet + if response.ok: + return response.json() + + # Log error and retunr null + logging.error("[AFVOTER GET] [%s]", response.text) + return None + + + def patch(self, voterset_key, patch_data): + """ + patch Path the Voterset + + :param voterset_key: The Voterset key to update with API + :param patch_data: The data to update the Voterset + """ + + # 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'] = voterset_key + + # Run the GET + response = requests.patch( + f'{self.base_url}/api/v1/votersets/{voterset_key}', + headers=headers, + timeout=300, + json=patch_data + ) + + # Good response. Return VoterSet + 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 new file mode 100644 index 0000000..71eac9d --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +""" +Project build definition file. +""" + +from setuptools import setup, find_packages + +with open('README.md', 'r') as f: + long_description = f.read() + +setup( + name='lib_af_api', + version='0.1.0', + author='', + author_email='', + description='', + long_description=long_description, + long_description_content_type='text/markdown', + zip_safe=False, + include_package_data=False, + packages=find_packages(), + python_requires='>=3.7', + install_requires=[ + 'requests==2.29.0', + ], +)