Chatbot-Backend / public_api /public_api_location.py
srilakshu012456's picture
Create public_api_location.py
e73c9c1 verified
import requests
from public_api.public_api_auth import login
def get_location_status(session_data, warehouse_id, stoloc):
url = "http://euwdsw202em01:4660/api/inventory/v1/locationStatus"
session = session_data["wms_auth"]
# Pass everything from kwargs directly to the API
params = {
"stoloc":stoloc
}
headers = {
"accept": "application/json",
"Content-Type": "application/json"
}
try:
response = session.get(url, params=params, headers=headers, verify=False)
# Standardized 401 Session Recovery
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
response = new_session.get(url, params=params, headers=headers, verify=False)
# SUCCESS: Return data and the status code for main.py to inspect
if response.status_code == 200:
try:
res_json = response.json()
all_data = res_json.get("data", [])
return {"data": all_data, "error": None, "status_code": 200}
except ValueError:
return {"data": [], "error": "Invalid JSON format", "status_code": 200}
print(f"Location API response code: {response.status_code}\n{response.text}")
return {"data": [], "error": f"Location API Error: {response.text}"}
except Exception as e:
print(f"Location API exception: {e}")
return {"data": [], "error": f"Location API Connection failed: {str(e)}"}