-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_material.py
More file actions
129 lines (112 loc) · 4.03 KB
/
fetch_material.py
File metadata and controls
129 lines (112 loc) · 4.03 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import requests
import json
import os
# Configuration from user provided values (hardcoded for this script to avoid .env issues if ignored)
BASE_URL = "https://orders.optimumgroup.nl/OrderServiceTest"
TOKEN_URL = "https://orders.optimumgroup.nl/OrderServiceTest/api/token"
USERNAME = "helloprint"
PASSWORD = "7D9ACE2E-FA66-48AC-A142-7B3F68EB9F8C"
LOCATIONS = ["L02", "L03"]
ENDPOINTS = [
"api/material/Locations",
"api/material/Material",
"api/material/Adhesives",
"api/material/coresizes",
"api/material/pouches",
"api/material/pouchclosures",
"api/material/producttypes",
"api/material/shapes",
"api/material/shiptocountries",
"api/material/shippingmethods"
]
def get_token():
print("Getting access token...")
payload = {
"grant_type": "password",
"username": USERNAME,
"password": PASSWORD
}
try:
response = requests.post(TOKEN_URL, data=payload, timeout=10)
if response.status_code == 200:
token_data = response.json()
return token_data.get("access_token")
else:
print(f"Failed to get token: {response.status_code} - {response.text}")
return None
except Exception as e:
print(f"Exception getting token: {e}")
return None
def fetch_data(token):
results = {}
# Add Key header (GUID) as discovered
headers = {
"Authorization": f"Bearer {token}",
"Key": "7D9ACE2E-FA66-48AC-A142-7B3F68EB9F8C"
}
# First fetch Locations (global)
print(f"Fetching global Locations...")
try:
url = f"{BASE_URL}/api/material/Locations"
response = requests.get(url, headers=headers, timeout=10)
print(f"Status: {response.status_code}")
if response.status_code == 200:
try:
results["Locations"] = response.json()
except:
results["Locations"] = response.text
else:
results["Locations"] = f"Error: {response.status_code} - {response.text}"
except Exception as e:
results["Locations"] = f"Exception: {str(e)}"
# Fetch per-location endpoints
for loc in LOCATIONS:
results[loc] = {}
print(f"\nFetching data for Location: {loc}")
for endpoint in ENDPOINTS:
if endpoint == "api/material/Locations":
continue
name = endpoint.split('/')[-1]
url = f"{BASE_URL}/{endpoint}?LocationCode={loc}"
print(f" Fetching {name}...")
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
try:
data = response.json()
results[loc][name] = data
except:
results[loc][name] = response.text
else:
# Capture text for debugging
results[loc][name] = f"Error: {response.status_code} - {response.text}"
except Exception as e:
results[loc][name] = f"Exception: {str(e)}"
return results
def generate_markdown(data):
md = "# Material API Collection\n\n"
md += f"Base URL: {BASE_URL}\n\n"
# Locations
md += "## Global Locations\n"
md += "```json\n"
md += json.dumps(data.get("Locations"), indent=2)
md += "\n```\n\n"
for loc in LOCATIONS:
md += f"## Location: {loc}\n"
loc_data = data.get(loc, {})
for name, content in loc_data.items():
md += f"### {name}\n"
md += "```json\n"
md += json.dumps(content, indent=2)
md += "\n```\n\n"
return md
if __name__ == "__main__":
token = get_token()
if token:
data = fetch_data(token)
md_content = generate_markdown(data)
with open("material.md", "w") as f:
f.write(md_content)
print("\nDone. Saved to material.md")
else:
print("Aborting due to auth failure.")