DocumentAPI.py

import requests
import xml.etree.ElementTree as ET
from RMISLibrary import RMISCredentials, Document
from TruckStopDBHelper import *

def GetDocument(rmisCreds: RMISCredentials, document: Document):
    
    # Open a connection to the database and check if this document already exists
    dateBaseObject = GetDBConnection("RMISCarriers") 
    documentCheck = dateBaseObject.sqlCursor.execute(f'SELECT * FROM Documents where ClientID=\'{rmisCreds.clientID}\' AND DocumentID = \'{document.documentID}\'')
    documentCheck = dateBaseObject.sqlCursor.fetchall()

    if (len(documentCheck) == 0): # Document does not exist in the database
        # Base URL for the Document API
        base_url = "https://api.rmissecure.com/_c/std/api/DocumentAPI.aspx"
        
        # Parameters for the API request
        params = {
            "clientID": rmisCreds.clientID,  # Client ID for authentication
            "pwd": rmisCreds.clientPassword,  # Client password for authentication
            "documentType": document.documentType,  # Type of document being requested
            "documentID": document.documentID,  # Unique identifier for the document
            "insdID": document.CarrierRMISID,  # Insured ID (carrier ID) related to the document
            "version": "1"  # Version of the API being called
        }

        # Headers for the API request
        headers = {"accept": "application/xml"}  # Indicate the response format expected

        # Make the API request with the specified parameters
        response = requests.get(base_url, headers=headers, params=params)

        # Check if the response was successful (HTTP status code 200)
        if response.status_code == 200:
            newDocument = __ProcessResponse(response)  # Process the successful response
            newDocument.CarrierRMISID = document.CarrierRMISID
            newDocument.clientID = rmisCreds.clientID
            InsertIntoTable(table="Documents", dataBaseObject=dateBaseObject, object=newDocument)

            #__InsertIntoDatabase(dbo, newDocument)
        else:
            # Log the error details if the request was not successful
            print(f"Failed to retrieve document: {response.status_code} - {response.text}")
            return False
    # Close the database connection
    dateBaseObject.sqlConn.close()

def __ProcessResponse(response: requests.Response):
    # Parse the XML response content to extract relevant fields
    root = ET.fromstring(response.content)

    try:
        # Extract relevant fields from the XML
        document_data = root.find('.//DocumentData').text  # Base64-encoded document data
        document_file_type = root.find('.//DocumentFileType').text  # Type of the document (e.g., PDF, JPG)
        document_title = root.find('.//DocumentTitle').text  # Title of the document
        document_size_in_bytes = root.find('.//DocumentFileSizeInBytes').text # Size of the document
        document_type = root.find('.//DocumentType').text # Type of document (client agreement/etc)
        document_date = root.find('.//DocumentDate').text # Date the document was uploaded
        document_id = root.find('.//DocumentID').text # Specific ID for this document

        document = Document(documentType=document_type, documentID=document_id, carrierID=None)
        document.documentData = document_data
        document.documentFileType = document_file_type
        document.documentTitle = document_title
        document.documentSizeInBytes = document_size_in_bytes
        document.documentDate = document_date

        return document        
    except Exception as e:
        # Handle exceptions and extract any errors from the response
        errors = root.find('.//Errors')  # Check for errors in the response
        for error in errors:
            print(error.text)  # Log each error message
        return False