ruạṛ
import requests # API Base URL BASE_URL = "https://thecomputerdoctor.repairshopr.com/api/v1" # Your RepairShopr API key API_KEY = "Tec5f801c3cd6fd7f8-209c71eba217e2f611bd4355646226f0" # Set the headers for the API request headers_get = { "accept": "application/json", "Authorization": API_KEY, } headers_put = { "accept": "application/json", "Authorization": API_KEY, "Content-Type": "application/json", } # Function to check if the ticket is an ID or a number and return the ID if it is a number def check_if_id(ticket_id): if ticket_id.startswith("RST"): return ticket_id else: id_url = BASE_URL + "/tickets?number=" + ticket_id response = requests.get(id_url, headers=headers_get) data = response.json() ticket_id = data["tickets"][0] ticket_id = "RST-" + str(ticket_id["id"]) return ticket_id # Function to make the api call and store the data in a dictionary def make_api_call(ticket_id): response = requests.get(BASE_URL + "/tickets/" + ticket_id[4:], headers=headers_get) data = response.json() return data # Function to handle the received API data and extract the useful data from it. def handle_data(api_data): ticket_properties = {} api_ticket_properties = api_data["ticket"]["properties"] for key, value in api_ticket_properties.items(): ticket_properties[key] = value old_bin_location = ticket_properties["Bin"] return ticket_properties, old_bin_location def bin_update(): extracted_data[0].update({"Bin": bin_location}) bin_updated = {"properties": extracted_data[0]} response = requests.put(BASE_URL + "/tickets/" + ticket_id[4:], json=bin_updated, headers=headers_put) data = response.json() if data["ticket"]["properties"]["Bin"] == bin_updated["properties"]["Bin"]: print("The Ticket has updated successfully!") else: print("An error occurred while updating the ticket:", response.text) def ticket_comment(): update_ticket_data = { "subject": "Auto update", "tech": "Auto", "body": f"This job has been moved from shelf: {extracted_data[1]} to the new shelf of: {bin_location}", "hidden": "true", "do_not_email": "true" } response = requests.post(BASE_URL + "/tickets/" + ticket_id[4:] + "/comment", json=update_ticket_data, headers=headers_put) # Saves the data from the post, but I'm not doing anything with it yet. data = response.json() if data["comment"]["body"] == update_ticket_data["body"]: print("The Ticket has been commented on successfully!") else: print("An error occurred while updating the ticket:", response.txt) def ticket_status(): status_map = { "C": "Resolved", "I": "Waiting for Insurance", "N": "New", "W": "Waiting on Customer", "D": "In Progress" } status = {"status": status_map.get(bin_location[0], "Error")} if status["status"] != "Error": response = requests.put(BASE_URL + "/tickets/" + ticket_id[4:], json=status, headers=headers_put) data = response.json() if data["ticket"]["status"] == status["status"]: print("The Ticket has had it's status successfully updated!") else: print("An error occurred while changing the status of the ticket", response.txt) else: print("Looks like the location doesn't correspond to a ticket status - skipping updating ticket status....") while True: # The ticket ID and bin location to update ticket_id = input("Enter the ticket ID or Number: ") bin_location = input("Enter the bin location: ") try: ticket_id = check_if_id(ticket_id) except: print("An error occurred while retrieving the ticket ID - Either number entered incorrect, repairshopr is down or the barcode was scanned incorrectrly.") continue try: api_data = make_api_call(ticket_id) except: print("An error occurred while making the call to repairshopr, the ticket may not have scanned correctly?") continue try: extracted_data = handle_data(api_data) except: print("An error occurred while manipulating the data from repairshopr - This job may not have a bin location? Or the barcode was scanned incorrectly and it's pulled down the wrong ticket information.") continue try: bin_update() ticket_comment() ticket_status() except: print("An error occurred while sending data to RepairShopr.") continue
cải xoăn