-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_product_fetch.py
More file actions
100 lines (85 loc) · 4.02 KB
/
debug_product_fetch.py
File metadata and controls
100 lines (85 loc) · 4.02 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
import requests
import json
import random
BASE_URL = "http://localhost:5000/api/v1"
def debug_product_fetch():
print("1. Listing Products (GetProductsView)...")
try:
# Assuming open endpoint or needs auth. Products usually public?
# Endpoints configs said MapGetProductsViewEndpoint.
# Let's try without auth first, then with auth if 401.
response = requests.get(f"{BASE_URL}/catalogs/products?Page=1&PageSize=10")
if response.status_code == 401:
print("Auth required. Logging in...")
# Login logic... (Reusing from debug_auth.py)
login_payload = {"userNameOrEmail": "mehdi@test.com", "password": "123456"}
auth_res = requests.post(f"{BASE_URL}/identity/login", json=login_payload)
auth_res.raise_for_status()
token = auth_res.json().get("accessToken")
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"{BASE_URL}/catalogs/products?Page=1&PageSize=10", headers=headers)
response.raise_for_status()
data = response.json()
print(f"List response: {json.dumps(data, indent=2)}")
print(f"List response keys: {data.keys()}")
# Handle different potential response structures
if "products" in data:
if isinstance(data["products"], dict) and "products" in data["products"]:
# Nested structure: { "products": { "products": [...] } }
products = data["products"]["products"]
print("Found nested product list.")
elif isinstance(data["products"], list):
products = data["products"]
elif isinstance(data["products"], dict) and "items" in data["products"]:
products = data["products"]["items"]
else:
# Last resort: try to find a list in values
products = []
for k, v in data["products"].items():
if isinstance(v, list):
products = v
print(f"Found products list in key: {k}")
break
elif "items" in data: # Common paged result key
products = data["items"]
else:
# Maybe it's just the list?
if isinstance(data, list):
products = data
else:
products = []
if not products:
print("No products found in list (or could not parse).")
return
target_product = products[0]
# target_id = target_product.get("id")
# Handle case sensitivity
target_id = target_product.get("id") or target_product.get("Id") or target_product.get("productId")
print(f"Targeting Product ID: {target_id}")
if not target_id:
print("Could not determine ID from product object.")
return
print(f"2. Fetching Product Details (GetProductById) for ID: {target_id}...")
detail_url = f"{BASE_URL}/catalogs/products/{target_id}"
# Check if auth needed
detail_res = requests.get(detail_url)
if detail_res.status_code == 401:
print("Auth required for details.")
detail_res = requests.get(detail_url, headers=headers)
print(f"Status Code: {detail_res.status_code}")
print(f"Response: {detail_res.text}")
if detail_res.status_code == 404:
print("FATAL: Product found in list but returned 404 on details.")
elif detail_res.status_code == 200:
# Validate product is in response
d_json = detail_res.json()
if "product" in d_json or "id" in d_json:
print("SUCCESS: Product details fetched.")
else:
print(f"WARNING: 200 OK but response structure unexpected: {d_json.keys()}")
except Exception as e:
import traceback
traceback.print_exc()
print(f"Error: {e}")
if __name__ == "__main__":
debug_product_fetch()