87 lines
2.0 KiB
Python
87 lines
2.0 KiB
Python
|
|
"""
|
||
|
|
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
|