Handle a successful sales system API response
If the API call is successful, you want to release the current work item as done (this indicates the work item was processed successfully):
@task
def consume_traffic_data():
"""
Inhuman Insurance, Inc. Artificial Intelligence System robot.
Consumes traffic data work items.
"""
process_traffic_data()
def process_traffic_data():
for item in workitems.inputs:
traffic_data = item.payload["traffic_data"]
valid = validate_traffic_data(traffic_data)
if valid:
status = post_traffic_data_to_sales_system(traffic_data)
if status == 200:
item.done()
def validate_traffic_data(traffic_data):
return len(traffic_data["country"]) == 3
def post_traffic_data_to_sales_system(traffic_data):
url = "https://robocorp.com/inhuman-insurance-inc/sales-system-api"
response = requests.post(url, json=traffic_data)
return response.status_code
In case the status returned by our POST request, is 200
(a successful API request) we set our item
as done, releasing it.
You want to release all the work items your robot processes - both the successful and the unsuccessful.
You again notice that validate_traffic_data(traffic_data)
does only one minor thing - a check.
You decide to do some quick refactoring and replace the function call with the explicit check:
def process_traffic_data():
for item in workitems.inputs:
traffic_data = item.payload["traffic_data"]
if len(traffic_data["country"]) == 3:
status = post_traffic_data_to_sales_system(traffic_data)
if status == 200:
item.done()