You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.8 KiB
60 lines
2.8 KiB
import requests as rq
|
|
import re
|
|
from logging import debug, error, basicConfig, DEBUG, WARNING, ERROR, INFO
|
|
import json
|
|
|
|
with open("config.json") as configFile:
|
|
config = json.load(configFile)
|
|
|
|
basicConfig(filename=config["logLocation"], encoding='utf-8', level=config["logging"])
|
|
|
|
RETURNCENTER_BASE_RL = config["OnePakUrl"]
|
|
|
|
def retrieve_return_data(lease_number: str, apiKey: str, details: bool = False) -> dict:
|
|
"""
|
|
Reaches out to OnePak API and searches for the given lease number.
|
|
Returns only summary data by default, use details = True to get full return object
|
|
"""
|
|
debug(f"[I] retrieve_return_data: {lease_number} | deatils: {details}")
|
|
if re.match("\d{3}-\d{7}-\d{3}", lease_number) == None:
|
|
error(f"[E] Invalid lease number: {lease_number}")
|
|
return {"error": f"Invalid lease number: {lease_number}"}
|
|
resp = rq.post(RETURNCENTER_BASE_RL,
|
|
json={"action": "get", "lease_number": lease_number, "details": "yes" if details else "no"},
|
|
headers={"Content-Type": "application/json"},
|
|
auth=(apiKey, ''))
|
|
if not resp.ok:
|
|
error(f"[E] retrieve_return_data: response was unsuccessful: {resp.text}")
|
|
return {"error": f"{resp.status_code} | {resp.text}"}
|
|
debug(f"[I] retrieve_return_data: resp: {resp}\n{resp.json()}")
|
|
return dict(resp.json())
|
|
|
|
|
|
def cancel_return(ra_number: str, apiKey: str, cancel_reason: str = "No longer needed") -> dict:
|
|
"""
|
|
Cancels a return authorization based on the ra_number
|
|
"""
|
|
debug(f"[I] cancel_return: {ra_number} | cancel reason: {cancel_reason}")
|
|
if re.match("^RCTR.*", ra_number) == None:
|
|
error(f"[!E] Invalid RA Number: {ra_number}")
|
|
return {"error": f"Invalid RA number: {ra_number}"}
|
|
resp = rq.post(RETURNCENTER_BASE_RL,
|
|
json={"action": "cancel", "ra_number": ra_number, "reason": cancel_reason},
|
|
headers={"Content-Type": "application/json"},
|
|
auth=(apiKey, ''))
|
|
if not resp.ok:
|
|
error(f"[E] cancel_return: response was unsuccessful: {resp.text}")
|
|
return {"error": f"{resp.status_code} | {resp.text}"}
|
|
debug(f"[I] cancel_return: resp: {resp}\n{resp.json()}")
|
|
return dict(resp.json())
|
|
|
|
def search_and_cancel(lease_number: str, apiKey: str, details: bool = False, cancel_reason: str = "No longer needed") -> dict:
|
|
searchResult = retrieve_return_data(lease_number, apiKey, details, cancel_reason)
|
|
if "error" in searchResult.keys():
|
|
error(f"[E] search_and_cancel: Failed to find lease number: {lease_number}\n{searchResult}")
|
|
return searchResult
|
|
cancelresult = cancel_return(searchResult["ra_number"], apiKey, cancel_return)
|
|
if "error" in cancelresult.keys():
|
|
error(f"[E] search_and_cancel: Failed to cancel RA: {lease_number} | {searchResult['ra_number']}\n{cancelresult}")
|
|
return searchResult
|
|
return {"success": "RA has been canceled"} |