-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-definition.py
More file actions
88 lines (75 loc) · 3.31 KB
/
api-definition.py
File metadata and controls
88 lines (75 loc) · 3.31 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from dotenv import load_dotenv
import os
import requests # For making HTTP requests
from akamai.edgegrid import EdgeGridAuth, EdgeRc # For Akamai API authentication
import urllib3
import json
############################## remove before production ##############################
# Disable SSL warnings (not recommended for production)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#######################################################################################
# Load Akamai API credentials from ~/.edgerc file
# The .edgerc file contains authentication details for the Akamai API
edgerc = EdgeRc('~/.edgerc')
section = 'default' # Using the 'default' section from .edgerc
# base_url = f"https://{edgerc.get(section, 'host')}" # Construct the base URL
host = edgerc.get(section, 'host') # e.g. "hostname"
url = f"https://{host}/api-definitions/v2/endpoints"
# Load environment variables from .env file
load_dotenv()
# Default parameters for API requests
params = {
'contractId': os.getenv('AKAMAI_CONTRACT_ID'), # Your Akamai contract ID from .env
'groupId': os.getenv('AKAMAI_GROUP_ID'), # Your Akamai group ID from .env
}
headers = {"accept": "application/json"}
# Create a requests Session and attach EdgeGrid authentication so Authorization header is sent
session = requests.Session()
# Use the helper to populate auth from ~/.edgerc; this ensures the Authorization header is included
session.auth = EdgeGridAuth.from_edgerc(edgerc, section)
# Use the constructed base_url as the request URL (adjust the path if you need a specific endpoint)
# url = base_url + "/api-definitions/v2/endpoints" # e.g. url = f"{base_url}/papi/v1/search" for a specific PAPI endpoint
params = {
'contains': 'homedepot.ca'
}
def summarize_api_endpoints(api_json):
"""
api_json: dict loaded from your posted JSON
returns: list of { apiEndPointName, apiEndPointHosts, resourcePaths }
"""
out = []
for ep in api_json.get("apiEndPoints", []):
out.append({
"apiEndPointName": ep.get("apiEndPointName", ""),
"apiEndPointHosts": ep.get("apiEndPointHosts", []) or [],
"resourcePaths": [
r.get("resourcePath")
for r in (ep.get("apiResourceBaseInfo", []) or [])
if isinstance(r, dict) and r.get("resourcePath")
]
})
return out
response = session.get(url, headers=headers, params=params, verify=False)
print(f"HTTP {response.status_code} {response.reason}")
content_type = response.headers.get('Content-Type', '')
parsed = None
if 'application/json' in content_type:
try:
parsed = response.json()
print(json.dumps(parsed, indent=2, ensure_ascii=False))
except ValueError:
print("Failed to parse JSON; raw body:")
print(response.text)
else:
# Try to pretty-print JSON even if Content-Type is missing/incorrect
try:
parsed = json.loads(response.text)
print(json.dumps(parsed, indent=2, ensure_ascii=False))
except Exception:
print("Response body:")
print(response.text)
# If we have a parsed JSON object, produce and print a summary of API endpoints
if isinstance(parsed, dict):
summary = summarize_api_endpoints(parsed)
print("\nAPI endpoints summary:")
print(json.dumps(summary, indent=2, ensure_ascii=False))