add voter to lib
This commit is contained in:
parent
4acb30b95b
commit
1d0fe2dced
|
|
@ -7,4 +7,5 @@ A Python library for managing AF API objects
|
||||||
from lib_af_api.campaign import Campaign
|
from lib_af_api.campaign import Campaign
|
||||||
from lib_af_api.event import Event
|
from lib_af_api.event import Event
|
||||||
from lib_af_api.voterset import VoterSet
|
from lib_af_api.voterset import VoterSet
|
||||||
|
from lib_af_api.voter import Voter
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue