Spaces:
Sleeping
Sleeping
Create public_api_item.py
Browse files
public_api/public_api_item.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from public_api.public_api_auth import login
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
def get_item(session_data, warehouse_id, item_number):
|
| 5 |
+
url = "http://euwdsw202em01:4660/api/item/v1/items"
|
| 6 |
+
session = session_data["wms_auth"]
|
| 7 |
+
|
| 8 |
+
# Using your specific required parameters
|
| 9 |
+
params = {
|
| 10 |
+
"itemNumber": item_number,
|
| 11 |
+
"warehouseId": warehouse_id
|
| 12 |
+
#"itemClientId": "----"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
headers = {"accept": "application/json", "Content-Type": "application/json"}
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
response = session.get(url, params=params, headers=headers, verify=False)
|
| 19 |
+
print("response",response.text)
|
| 20 |
+
# Logic for first attempt and retry
|
| 21 |
+
if response.status_code == 401:
|
| 22 |
+
print("Session expired. Re-logging in...")
|
| 23 |
+
new_session, new_wh, success = login()
|
| 24 |
+
if success:
|
| 25 |
+
session_data["wms_auth"] = new_session
|
| 26 |
+
session_data["user_warehouse_id"] = new_wh
|
| 27 |
+
# Retry immediately
|
| 28 |
+
response = new_session.get(url, params=params, headers=headers, verify=False)
|
| 29 |
+
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
try:
|
| 32 |
+
res_json = response.json()
|
| 33 |
+
print("res_json",res_json)
|
| 34 |
+
all_data = res_json.get("items", [])
|
| 35 |
+
print("all_data",all_data)
|
| 36 |
+
#print(f"DEBUG: Raw data from API: {response.json()}")
|
| 37 |
+
#print(f"DEBUG: Data from API: {all_data}")
|
| 38 |
+
# Local filtering for the specific warehouse
|
| 39 |
+
#filtered_data = [attr for attr in all_data if attr.get("warehouseId") == warehouse_id]
|
| 40 |
+
return {"data": all_data, "error": None}
|
| 41 |
+
except ValueError:
|
| 42 |
+
return {"data": [], "error": "Invalid JSON format from server."}
|
| 43 |
+
|
| 44 |
+
# Minimalist error handling for all other cases (404, 500, etc.)
|
| 45 |
+
print(f"Items API response code: {response.status_code}\n{response.text}")
|
| 46 |
+
return {"data": [], "error": f"Items API Error: {response.text}"}
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Items API exception: {e}")
|
| 50 |
+
return {"data": [], "error": f"Items API Connection failed: {str(e)}"}
|