aws-solutions / enhanced-document-understanding-on-aws

Enhanced Document Understanding on AWS delivers an easy-to-use web application that ingests and analyzes documents, extracts content, identifies and redacts sensitive customer information, and creates search indexes from the analyzed data.
https://aws.amazon.com/solutions/implementations/enhanced-document-understanding-on-aws/
Apache License 2.0
29 stars 10 forks source link

API Schema information request #64

Closed Komal-99 closed 4 weeks ago

Komal-99 commented 1 month ago

Hi @mukitmomin @knihit , I am opening this issue to check if the Request body for RestAPI got updated. Not able to find it but my previous code throughs me an error of Invalid Request Body.

def create_case(case_id: str = None):
    if not case_id:
        raise ValueError("`case_id` is required")
    request_body = {
            "caseName": case_id,
        }
    get_case_api = f"{REST_API_ENDPOINT}/case"
    try:
        region = "us-east-1"  # Update this with your AWS region
        service = "execute-api"
        # Sign the request using AWS Signature Version 4
        aws_auth = AWS4Auth(ACCESS_KEY,SECRET_KEY, region, service, session_token=session_token)
        headers = {
        "Authorization": f"Bearer {session_token}",
        "User-Agent": USER_AGENT,
        "Content-Type": "application/json"
    }
        # Send the request with authentication
        response = requests.post(get_case_api,data=json.dumps(request_body), headers=headers)

        # Check if the response was successful
        if response.status_code == 200:
            # Parse the JSON data from the response body
            data = response.json()
            case_id = data.get('caseId')
            return case_id
        else:
            print(f"Error: {response.status_code} - {response.text}")

    except NoCredentialsError:
        print("AWS credentials not found or invalid.")
        return None

When Passing a String case_id due to which case not getting created and I am not able to use the service. image

Originally posted by @Komal-99 in https://github.com/aws-solutions/enhanced-document-understanding-on-aws/issues/31#issuecomment-2267525886

knihit commented 1 month ago

@Komal-99 have you looked at this guide here. https://docs.aws.amazon.com/solutions/latest/enhanced-document-understanding-on-aws/developer-guide.html#api-reference. Yes in this release the API schema has changed.

Komal-99 commented 1 month ago

Yes , but here in API reference no Schema is provided.

Can you guide me?

knihit commented 1 month ago

So you can export the swagger or Open API spec if you have deployed the solution from the API Gateway web console. Here is the API Gateway documentation on how you can do it - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-export-api.html

Komal-99 commented 1 month ago

Hi , Not able to resolve it till now, can you elaborate more, will exporting swagger gives me the JSON payload expected to pass to the API?

knihit commented 1 month ago

The Swagger will give you the JSON schema for the payload.

knihit commented 1 month ago

You can also capture the actual payload, if you have the solution deployed and while using the web inspector in browsers like chrome and firefox.

Komal-99 commented 1 month ago

Hi @knihit , Thanks, I downloaded Swagger files and got an idea of the payload. However, we don't have a deployed solution as we need to use only the API integrated into our solution.

While I am trying the New payload from the Python script it shows 403 error: forbidden on the Rest API request to create caseID Figure 1 and when calling it from postman it gives a CustomExecutionError: Cannot read properties of undefined (reading 'concat') which on my research is same as Undefined because Case is not getting Creating. figure 2 image

image

looking for guidance and help :)

Komal-99 commented 1 month ago

Hi @knihit @mukitmomin looking for an answers if you could help.

mukitmomin commented 4 weeks ago

The format of the Authorization header is incorrect i believe. You do not need Bearer, just the {"Authorization" : <token>} should be fine. You can try using this function as a sample

# create a function to parse a command line argument for case name
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--case_name", help="Name of the case to be created", required=False, default=DEFAULT_CASE_NAME)
    args = parser.parse_args()
    return args

def create_case(case_name: str = None):
    if not case_name:
        case_name = DEFAULT_CASE_NAME

    create_case_endpoint = f"{REST_API_ENDPOINT}/case"
    headers = {"Authorization": token, "User-Agent": USER_AGENT, "Content-Type": "application/json"}

    try:
        response = requests.post(
            create_case_endpoint, headers=headers, json={"caseName": case_name, "enableBackendUpload": False}
        )

        if response.status_code == 200:
            return response.json()

    except Exception as e:
        raise e

if __name__ == "__main__":
    args = parse_args()
    response = create_case(args.case_name)

    pprint(response)

I have tested that bit of code with the v2 release of EDU and it is working.

Komal-99 commented 4 weeks ago

How to get an Authorization token? I am using a session token. Because I removed the Authorization from all the APIs by editing the API from API gateway.

mukitmomin commented 4 weeks ago

The upload-document lambda function has a method to create cases. To ensure only valid users can create cases the Authorization jwt token is decoded to extract the cognito userId claim jwt token, using this function in the common-node-lib.

If you have removed the Authorization mechanism then you have to modify the logic for validating case creation, as implemented here. The error you are seeing is coming from this line because the userId in that function is undefined as you removed the Authorization mechanism.