Spaces:
Sleeping
Sleeping
Create public_api_inventory.py
Browse files
public_api/public_api_inventory.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from public_api.public_api_auth import login
|
| 3 |
+
|
| 4 |
+
def get_inventory_holds(session_data, warehouse_id, **kwargs):
|
| 5 |
+
url = "http://euwdsw202em01:4660/api/inventory/v1/inventoryHolds"
|
| 6 |
+
session = session_data["wms_auth"]
|
| 7 |
+
|
| 8 |
+
# Build dynamic params from kwargs
|
| 9 |
+
params = {}
|
| 10 |
+
if kwargs.get("lodnum"): params["lodnum"] = kwargs.get("lodnum")
|
| 11 |
+
if kwargs.get("subnum"): params["subnum"] = kwargs.get("subnum")
|
| 12 |
+
if kwargs.get("dtlnum"): params["dtlnum"] = kwargs.get("dtlnum")
|
| 13 |
+
|
| 14 |
+
headers = {
|
| 15 |
+
"accept": "application/json",
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
response = session.get(url, params=params, headers=headers, verify=False)
|
| 21 |
+
|
| 22 |
+
# 1. Logic for first attempt 401 and retry
|
| 23 |
+
if response.status_code == 401:
|
| 24 |
+
print("Session expired. Re-logging in...")
|
| 25 |
+
new_session, new_wh, success = login()
|
| 26 |
+
if success:
|
| 27 |
+
session_data["wms_auth"] = new_session
|
| 28 |
+
session_data["user_warehouse_id"] = new_wh
|
| 29 |
+
# Retry immediately with new session
|
| 30 |
+
response = new_session.get(url, params=params, headers=headers, verify=False)
|
| 31 |
+
|
| 32 |
+
# 2. Handle Success (200)
|
| 33 |
+
if response.status_code == 200:
|
| 34 |
+
try:
|
| 35 |
+
res_json = response.json()
|
| 36 |
+
all_data = res_json.get("data", [])
|
| 37 |
+
|
| 38 |
+
# Dynamic Filtering: Only include records matching the session warehouse
|
| 39 |
+
# Using 'wh_id' as per your existing logic
|
| 40 |
+
filtered_data = [
|
| 41 |
+
attr for attr in all_data
|
| 42 |
+
if attr.get("wh_id") == warehouse_id
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
return {"data": filtered_data, "error": None}
|
| 46 |
+
except ValueError:
|
| 47 |
+
return {"data": [], "error": "Invalid JSON format from Holds API."}
|
| 48 |
+
|
| 49 |
+
# 3. Handle all other error codes (404, 500, etc.)
|
| 50 |
+
print(f"Holds API response code: {response.status_code}\n{response.text}")
|
| 51 |
+
return {"data": [], "error": f"Holds API Error: {response.text}"}
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Holds API exception: {e}")
|
| 55 |
+
return {"data": [], "error": f"Holds API Connection failed: {str(e)}"}
|