lib-afr-api/lib_afr_api/token.py

176 lines
4.7 KiB
Python

"""
Token Class object
"""
import logging
import requests
class Token():
"""Token class"""
def __init__(self, base_url, jwt):
"""
__init__ Init Token
:param base_url: Base URL for Token API
:param jwt: JWT Authentication for Token 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
def password_reset_trigger(self, username=None, email=None):
"""
password_reset_trigger Send a password reset email to the username account provided
:param username: The username of the account to be affected
:param email: The email of the account to be affected
"""
# Set the headers for the request
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
#'Authorization': f'Bearer {self.jwt}'
}
print(f"token::password_reset_trigger(): username = {username} email = {email}")
# Set the patch data
patch_data = {}
if username not in (None, ""):
patch_data['user_name'] = username
patch_data['email'] = None
elif email not in (None, ""):
patch_data['user_name'] = None
patch_data['email'] = email
if patch_data['user_name'] in (None, '') and patch_data['email'] in (None, ''):
print(f"token::password_reset_trigger(): Error: 'user_name' and 'email' values are both empty. patch_data = {patch_data}")
return None
print(f'{self.base_url}/api/v1/token/generate')
print(f'patch_data = {patch_data}')
# Run the PATCH
response = requests.patch(
f'{self.base_url}/api/v1/token/generate',
headers=headers,
timeout=300,
json=patch_data
)
if response.ok:
return response.json()
logging.error("[PASSWORD RESET TRIGGER] [%s]", response.text)
return None
def password_reset(self, token_value=None, password=None, verify=None):
"""
password_reset Reset password with token value
:param username: The username of the account to be affected
"""
if token_value in (None, ""):
return None
# 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 = {
'user_token': token_value,
'password': password,
'password_verification': verify
}
# Run the PATCH
response = requests.patch(
f'{self.base_url}/api/v1/token/password/{token_value}',
headers=headers,
timeout=300,
json=patch_data
)
if response.ok:
return response.json()
logging.error("[PASSWORD RESET] [%s]", response.text)
return None