Spaces:
Sleeping
Sleeping
Create public_api_auth.py
Browse files
public_api/public_api_auth.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#inside .env file
|
| 2 |
+
#service_url="http://euwdsw202em01:4660/service"
|
| 3 |
+
#console_url="http://euwdsw202em01:4660/console"
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load variables from .env file
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
usr_id = os.getenv("usr_id")
|
| 13 |
+
password = os.getenv("password")
|
| 14 |
+
|
| 15 |
+
def login():
|
| 16 |
+
url = "http://euwdsw202em01:4660/ws/auth/login"
|
| 17 |
+
|
| 18 |
+
# Query parameters
|
| 19 |
+
params = {
|
| 20 |
+
"usr_id": usr_id,
|
| 21 |
+
"password": password
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
headers = {
|
| 25 |
+
#"Accept": "*/*",
|
| 26 |
+
"Cache-Control": "no-cache",
|
| 27 |
+
"Accept": "application/json"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
session = requests.Session()
|
| 32 |
+
response = session.get(url, params=params, headers=headers,verify=False)
|
| 33 |
+
#print("Content-Type:", response.headers.get("Content-Type"))
|
| 34 |
+
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
wh_url = f"http://euwdsw202em01:4660/api/user/v1/users/{usr_id}/warehouses"
|
| 37 |
+
wh_response = session.get(wh_url, verify=False)
|
| 38 |
+
|
| 39 |
+
authorized_wh = "WH1" # Fallback default
|
| 40 |
+
if wh_response.status_code == 200:
|
| 41 |
+
wh_data = wh_response.json().get("data", [])
|
| 42 |
+
if wh_data:
|
| 43 |
+
# Logic: Take the first warehouse ID they are assigned to
|
| 44 |
+
# Or find the one marked as 'primary' if the API provides it
|
| 45 |
+
authorized_wh = wh_data[0].get("wh_id", "WH1")
|
| 46 |
+
|
| 47 |
+
print(f"Login Success. User {usr_id} assigned to {authorized_wh}")
|
| 48 |
+
|
| 49 |
+
# Return the session AND the locked warehouse ID
|
| 50 |
+
return session, authorized_wh, True
|
| 51 |
+
|
| 52 |
+
else:
|
| 53 |
+
print({"login": "failed", "status_code": response.status_code})
|
| 54 |
+
return None, None, False
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print({"login": "error", "error": str(e)})
|
| 58 |
+
return None, None, False
|
| 59 |
+
|
| 60 |
+
#if __name__ == "__main__":
|
| 61 |
+
# login()
|
| 62 |
+
|