ConnectwiseFriends/reportguy.py

31 lines
1.4 KiB
Python
Raw Normal View History

import requests
from dotenv import load_dotenv
import os
load_dotenv()
def apiGetRequest(api_base, endpoint, params, headers):
url = api_base + endpoint
resp = requests.get(url, params=params, headers=headers)
if resp.status_code == 200:
return resp.json()
2024-07-27 14:17:14 +00:00
else:
resp.raise_for_status()
2024-07-27 05:17:37 +00:00
api_base = os.environ["API_BASE"]
authToken = os.environ["AUTH_TOKEN"]
clientId = os.environ["CLIENT_ID"]
endpoint = '/finance/agreementrecap' #endpoint for GET
headers = {'Authorization': authToken, 'clientId': clientId} # DO NOT fill this out and then commit it to the publicly-viewable git repository, please
params = {'pageSize': 50, 'conditions': 'agreementStatus!="CANCEL"', 'fields': 'id,companyName,name,lastInvoiceAmount,nextInvoiceAmount,agreementStatus'} # API call params--prepare to cry if your url encoding is sub-par
2024-07-27 05:17:37 +00:00
data = apiGetRequest(api_base, endpoint, params, headers) #Put it all together and whadya get
for item in data: #This is just iterating through whatever comes back and barfing the data in a readable way
print(f"{item['companyName']} - {item['name']} - {item['agreementStatus']}")
additionsEndpoint = '/finance/agreements/' + str(item['id']) + '/additions'
2024-07-27 14:17:14 +00:00
additions = apiGetRequest(api_base, additionsEndpoint, {'pagesize': 50}, headers)
for thing in additions: # Yo dawg I heard you like loops
print(' ' + thing['product']['identifier'])