-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_var.py
72 lines (54 loc) · 2.25 KB
/
env_var.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests, json, base64
from dotenv import dotenv_values
from liongardAPI import LiongardAPI
config = dotenv_values(".env")
# CW instance info
cw_id = config["CW_ID"]
cw_url = config["CW_URL"]
# Linking current connectwise codebase to the module
codebase_req = requests.get(f"https://{cw_url}/login/companyinfo/{cw_id}")
codebase_obj = json.loads(codebase_req.text)
accept_codebase = codebase_obj['VersionCode'].strip('v')
cw_base_url = f"https://{cw_url}/{codebase_obj['Codebase']}apis/3.0"
# CW credentials
clientId = config["CW_CLIENT_ID"]
cw_public = config["CW_PUBLIC"]
cw_private = config["CW_PRIVATE"]
# Packaging authentication headers {cw_id}
authorization_key = base64.b64encode(bytes(f"{cw_id}+{cw_public}:{cw_private}", 'utf-8'))
cw_headers = {
"clientId": clientId,
"Authorization":f"Basic {authorization_key.decode()}",
"Accept": f"application/vnd.connectwise.com+json; version={accept_codebase}"
}
# Liongard credentials
lg_public = config["LG_PUBLIC"]
lg_private = config["LG_PRIVATE"]
lg_instance = config["LG_INSTANCE"]
# Liongard controller
liongard_controller = LiongardAPI(lg_instance, lg_private, lg_public)
# Huntress API credentials
huntress_public = config['HUNTRESS_PUBLIC']
huntress_private = config['HUNTRESS_PRIVATE']
def get_msp_additions(companies_ver=False):
'''
returns: list of tuples [('company_name', [all_msp_additions])]
'''
a_url = cw_base_url + "/finance/agreements?pageSize=1000"
agreements = json.loads(requests.get(a_url, headers=cw_headers).text)
msp_agreements = [x for x in agreements if x['type']['name'] == "Managed Services"]
# Pulling all additions for each agreement and storing them in a tuple with the company name
additions = []
companies = {}
for agreement in msp_agreements:
url = cw_base_url + f"/finance/agreements/{agreement['id']}/additions"
req = requests.get(url, headers=cw_headers)
all = json.loads(req.text)
additions.append((agreement['company']['name'], all))
if agreement['company']['name'] not in companies:
companies[agreement['company']['name']] = [all]
else:
companies[agreement['company']['name']].append(all)
if companies_ver:
return companies
return additions