First attempt at sales system API integration

Time to start the conversation with the sales system API. As an icebreaker, you implement a POST request:

consumer.robot:

*** Settings ***
Documentation     Inhuman Insurance, Inc. Artificial Intelligence System robot.
...               Consumes traffic data work items.
Resource          shared.robot

*** Tasks ***
Consume traffic data work items
    For Each Input Work Item    Process traffic data

*** Keywords ***
Process traffic data
    ${payload}=    Get Work Item Payload
    ${traffic_data}=    Set Variable    ${payload}[${WORK_ITEM_NAME}]
    ${valid}=    Validate traffic data    ${traffic_data}
    IF    ${valid}
        Post traffic data to sales system    ${traffic_data}
    END

Validate traffic data
    [Arguments]    ${traffic_data}
    ${country}=    Get Value From Json    ${traffic_data}    $.country
    ${valid}=    Evaluate    len("${country}") == 3
    [Return]    ${valid}

Post traffic data to sales system
    [Arguments]    ${traffic_data}
    POST    https://robocorp.com/inhuman-insurance-inc/sales-system-api
    ...    json=${traffic_data}

  • The IF condition checks if the data is valid. If yes, the Post traffic data to sales system keyword is called. If not, nothing is done.
  • The POST keyword posts the JSON payload to the sales system API endpoint URL.
  • You add the RPA.HTTP library to the shared.robot and remove the library import from the producer.robot (DRY!).

When you run the consumer task, eventually, one of the POST requests will fail with an exception:

HTTPError: 500 Server Error: Internal Server Error for url: https://robocorp.com/inhuman-insurance-inc/sales-system-api

This is probably the issue that the sales system API team mentioned. Sometimes the API call fails.

Currently, your robot gives up immediately after hitting an exception. You would like the robot to continue posting the traffic data as long as data is available, ignoring the failed attempts.

You decide to ignore the exceptions for now and improve the exception handling a bit later (small steps!):

consumer.robot:

*** Settings ***
Documentation     Inhuman Insurance, Inc. Artificial Intelligence System robot.
...               Consumes traffic data work items.
Resource          shared.robot

*** Tasks ***
Consume traffic data work items
    For Each Input Work Item    Process traffic data

*** Keywords ***
Process traffic data
    ${payload}=    Get Work Item Payload
    ${traffic_data}=    Set Variable    ${payload}[${WORK_ITEM_NAME}]
    ${valid}=    Validate traffic data    ${traffic_data}
    IF    ${valid}
        Post traffic data to sales system    ${traffic_data}
    END

Validate traffic data
    [Arguments]    ${traffic_data}
    ${country}=    Get Value From Json    ${traffic_data}    $.country
    ${valid}=    Evaluate    len("${country}") == 3
    [Return]    ${valid}

Post traffic data to sales system
    [Arguments]    ${traffic_data}
    ${status}    ${return}    Run Keyword And Ignore Error
    ...    POST
    ...    https://robocorp.com/inhuman-insurance-inc/sales-system-api
    ...    json=${traffic_data}

The Run Keyword And Ignore Error keyword ignores any errors and returns two values. The first one is either a string PASS or FAIL, depending on the status of the executed keyword. The second value is either the return value of the keyword or the received error message.

You run the consumer task. The log shows both the successful and failed requests:

Successful request:

${status} = PASS

{return} = <Response [200]>

Failed request:

${status} = FAIL

${return} = HTTPError: 500 Server Error: Internal Server Error for url: https://robocorp.com/inhuman-insurance-inc/sales-system-api