2023-12-08 07:16:43 +00:00
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2023-12-09 18:29:51 +00:00
|
|
|
def get_all(self, args = None):
|
2023-12-08 07:16:43 +00:00
|
|
|
"""
|
2023-12-10 07:04:39 +00:00
|
|
|
get_all Get the voter
|
2023-12-08 07:16:43 +00:00
|
|
|
|
|
|
|
|
: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}'
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-10 07:04:39 +00:00
|
|
|
if not isinstance(args, dict):
|
|
|
|
|
args = {}
|
2023-12-08 07:16:43 +00:00
|
|
|
|
|
|
|
|
# Run the GET
|
|
|
|
|
response = requests.get(
|
2023-12-10 07:04:39 +00:00
|
|
|
f'{self.base_url}/api/v1/voters',
|
|
|
|
|
params=args,
|
2023-12-08 07:16:43 +00:00
|
|
|
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
|
2023-12-09 07:27:19 +00:00
|
|
|
|
|
|
|
|
|
2023-12-09 16:59:09 +00:00
|
|
|
def post(self, post_data):
|
2023-12-09 07:27:19 +00:00
|
|
|
"""
|
|
|
|
|
post Post the Voter
|
|
|
|
|
|
|
|
|
|
: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
|
2023-12-09 18:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete(self, voter_key):
|
|
|
|
|
"""
|
|
|
|
|
delete Delete the Voter
|
|
|
|
|
|
|
|
|
|
:param voter_key: The Voter key to delete 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.delete(
|
|
|
|
|
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 DELETE] [%s]", response.text)
|
|
|
|
|
return None
|