lib-afr-api/lib_afr_api/microsite.py

124 lines
3.0 KiB
Python

"""
Microsite Class object
"""
import logging
import requests
class Microsite():
"""Microsite class"""
def __init__(self, base_url, jwt):
"""
__init__ Init Microsite
:param base_url: Base URL for Microsite API
:param jwt: JWT Authentication for Microsite API
"""
self.base_url = base_url
self.jwt = jwt
def get_all(self, args = None):
"""
get_all Get the microsites
: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}'
}
if not isinstance(args, dict):
args = {}
# Run the GET
response = requests.get(
f'{self.base_url}/api/v1/microsites',
params=args,
headers=headers,
timeout=300
)
# Good response. Return Microsites
if response.ok:
return response.json()
# Log error and retunr null
logging.error("[AFMICROSITE GET ALL] [%s]", response.text)
return None
def get(self, microsite_key):
"""
get Get the microsite
:param microsite_key: The Microsite 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/microsites/{microsite_key}',
headers=headers,
timeout=300
)
# Good response. Return Microsite
if response.ok:
return response.json()
# Log error and retunr null
logging.error("[AFMICROSITE GET] [%s]", response.text)
return None
def patch(self, microsite_key, patch_data):
"""
patch Path the Microsite
:param microsite_key: The Microsite key to update with API
:param patch_data: The data to update the Microsite
"""
# 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'] = microsite_key
# Run the GET
response = requests.patch(
f'{self.base_url}/api/v1/microsites/{microsite_key}',
headers=headers,
timeout=300,
json=patch_data
)
# Good response. Return Microsite
if response.ok:
return response.json()
# Log error and retunr null
logging.error("[AFMICROSITE UPDATE] [%s]", response.text)
try:
return response.json()
except: # pylint: disable=bare-except
return None