Skip to content

requests

Dispatch provides a built-in integration with requests, offering proper error handling out of the box. It’s enabled by default and it takes no extra code to set it up.

import dispatch
import requests
@dispatch.function
def fetch_cat_fact():
response = requests.get("https://cat-facts.dispatch.run")
# Raise `requests.HTTPError` exception when request is not successful
response.raise_for_status()
return response.text
def main():
fetch_cat_fact.dispatch()
dispatch.run(main)

Exception handling

HTTPError

Depending on the status_code attribute, a different Dispatch behavior is applied.

HTTP status codeDescriptionDispatch status
1xxInformationalStatus.PERMANENT_ERROR
2xxSuccessfulStatus.OK
3xxRedirectionStatus.PERMANENT_ERROR
3xxRedirectionStatus.PERMANENT_ERROR
4xxClient errorStatus.PERMANENT_ERROR
400Bad requestStatus.INVALID_ARGUMENT
401UnauthorizedStatus.UNAUTHENTICATED
403ForbiddenStatus.PERMISSION_DENIED
404Not foundStatus.NOT_FOUND
408Request timeoutStatus.TIMEOUT
429Too many requestsStatus.THROTTLED
5xxServer errorStatus.TEMPORARY_ERROR
501Not implementedStatus.PERMANENT_ERROR

Timeout

Returns STATUS.TIMEOUT.

RequestException

Returns Status.TEMPORARY_ERROR, unless exception is HTTPError or Timeout, since RequestException is a parent class of these. Learn more in requests documentation.

Response handling

If a function returns requests.Response instance, Dispatch looks at the status_code attribute and determines behavior the same way as HTTPError exception.

import dispatch
@dispatch.function
def fetch_cat_fact():
response = httpx.get("https://cat-facts.dispatch.run")
return response