-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpcodes_per_account.py
More file actions
executable file
·69 lines (53 loc) · 2.29 KB
/
Copy pathcpcodes_per_account.py
File metadata and controls
executable file
·69 lines (53 loc) · 2.29 KB
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
# report of all cpcodes and products in an account per group
# written by ralvarez on 2024-06-27
import requests, json, sys, os
from akamai.edgegrid import EdgeGridAuth,EdgeRc
import urllib
import argparse
# from https://github.com/akamai/AkamaiOPEN-edgegrid-python
edgerc_file_path = os.path.expanduser('~/.edgerc')
edgerc_object = EdgeRc(edgerc_file_path)
baseurl = 'https://' + edgerc_object.get('default', 'host')
s=requests.Session()
s.auth=EdgeGridAuth.from_edgerc(edgerc_object,'default')
groups_api='/papi/v1/groups'
cpcodes_api='/papi/v1/cpcodes'
headers = {
"accept": "application/json",
"PAPI-Use-Prefixes": "false"
}
# Grab the account switch key from command line execution
try:
skey=sys.argv[1]
print('Using Account Switch Key: ' + skey)
except IndexError:
print("Error: Please provide the accountSwitchKey as a command line argument.")
sys.exit(1)
params = {
"accountSwitchKey": skey
}
grp_req=s.get(baseurl+groups_api,headers=headers,params=params)
grp_json=grp_req.json()
if grp_req.status_code != 200:
print(f"\nAPI Error {grp_req.status_code}: {grp_json.get('title', 'Unknown Error')}")
print(f"Detail: {grp_json.get('detail', 'No details provided.')}")
sys.exit(1)
for grp in grp_json['groups']['items']:
print(grp['groupName']+' has the following contract(s) and cpcodes:')
for contract in grp['contractIds']:
print(contract)
# print()
# Add accountSwitchKey to the inner loop params so it carries over
cp_params={'groupId': grp['groupId'],'contractId': contract, 'accountSwitchKey': skey}
cpc=s.get(baseurl+cpcodes_api,headers=headers,params=cp_params)
cpc_json=cpc.json()
# print(cpc_json)
# Check if the contract/group combo actually has cpcodes to avoid KeyError
if 'cpcodes' in cpc_json and 'items' in cpc_json['cpcodes']:
for cp in cpc_json['cpcodes']['items']:
# Products are returned as a list, so we join them into a readable string
products = ", ".join(cp.get('productIds', ['No Product']))
print(' '+cp['cpcodeName']+' cpcodeId: '+cp['cpcodeId']+' | products: '+products)
else:
print(' No cpcodes found for this contract/group combo.')
print()