File size: 2,272 Bytes
7203369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from public_api.public_api_auth import login

def get_inventory_holds(session_data, warehouse_id, **kwargs):
    url = "http://euwdsw202em01:4660/api/inventory/v1/inventoryHolds"
    session = session_data["wms_auth"]
    
    # Build dynamic params from kwargs
    params = {}
    if kwargs.get("lodnum"): params["lodnum"] = kwargs.get("lodnum")
    if kwargs.get("subnum"): params["subnum"] = kwargs.get("subnum")
    if kwargs.get("dtlnum"): params["dtlnum"] = kwargs.get("dtlnum")
        
    headers = {
        "accept": "application/json",
        "Content-Type": "application/json"
    }

    try:
        response = session.get(url, params=params, headers=headers, verify=False)
        
        # 1. Logic for first attempt 401 and retry
        if response.status_code == 401:
            print("Session expired. Re-logging in...")
            new_session, new_wh, success = login()
            if success:
                session_data["wms_auth"] = new_session
                session_data["user_warehouse_id"] = new_wh
                # Retry immediately with new session
                response = new_session.get(url, params=params, headers=headers, verify=False)

        # 2. Handle Success (200)
        if response.status_code == 200:
            try:
                res_json = response.json()
                all_data = res_json.get("data", [])
                
                # Dynamic Filtering: Only include records matching the session warehouse
                # Using 'wh_id' as per your existing logic
                filtered_data = [
                    attr for attr in all_data 
                    if attr.get("wh_id") == warehouse_id
                ]
                
                return {"data": filtered_data, "error": None}
            except ValueError:
                return {"data": [], "error": "Invalid JSON format from Holds API."}
        
        # 3. Handle all other error codes (404, 500, etc.)
        print(f"Holds API response code: {response.status_code}\n{response.text}")
        return {"data": [], "error": f"Holds API Error: {response.text}"}

    except Exception as e:
        print(f"Holds API exception: {e}")
        return {"data": [], "error": f"Holds API Connection failed: {str(e)}"}