Route Functions and Python Function Style Guide
What is a Route Function?
A route function is a Python function designed to handle API calls through Domo endpoints. Key points include:
- Decorator: Every route function must be decorated with
@gd.route_function
which helps standardize the behavior and error handling across the codebase. - Asynchronous Execution: Route functions are defined as
async def
to allow non-blocking HTTP requests viahttpx.AsyncClient
. - API Communication: They rely on the helper function
gd.get_data
to send requests. The response fromgd.get_data
is always an instance ofrgd.ResponseGetData
. - Response Validation: After making an API call, each function must verify the response by testing
res.is_success
. If the request fails, a custom exception (typically a subclass ofdmde.RouteError
) must be raised. - Parameters: Route functions expect at least the following:
auth
: admda.DomoAuth
(ordmda.DomoFullAuth
) object for authentication.session
: an optionalhttpx.AsyncClient
for managing requests.debug_api
: a boolean flag to enable extra logging or debugging.parent_class
: an optional parameter for passing context in error tracebacks.
Python Function Style Guide
General Guidelines:
Function Naming:
- Use
snake_case
for names. - Choose descriptive and concise names (e.g.,
get_application_by_id
).
- Use
Type Annotations:
Always include type hints for parameters and the return type.
Example:
async def get_application_by_id(auth: dmda.DomoAuth, application_id: str) -> rgd.ResponseGetData:
Docstrings:
Every function should have a docstring that describes its purpose, input arguments, and return value.
Example:
""" Retrieves an application by its ID. Args: auth (dmda.DomoAuth): Authentication object. application_id (str): The ID of the application to retrieve. Returns: rgd.ResponseGetData: The response object containing application data. """
Error Handling:
Always validate the response from
gd.get_data
by checkingres.is_success
.On failure, raise a custom exception (typically an instance of a class derived from
dmde.RouteError
).Example:
if not res.is_success: raise dmde.RouteError( =auth.domo_instance, domo_instance=res.traceback_details.function_name, function_name="API call failed", message )
Parameter Defaults and Optional Arguments:
- Provide default values for optional parameters (e.g.,
debug_api: bool = False
,session: httpx.AsyncClient = None
). - Ensure defaults do not hide required information for API requests.
- Provide default values for optional parameters (e.g.,
API Request Pattern:
- Route functions should always make API calls using
gd.get_data
(or similar helper functions such asgd.looper
when pagination is needed). - Consistently map the response to a structured format or raise errors if the response indicates failure.
- Route functions should always make API calls using
Code Readability:
- Use 4 spaces per indentation level.
- Limit line lengths to 79 characters for readability.
- Keep functions focused and avoid overly long implementations.
Return Values:
- Return only instances of
rgd.ResponseGetData
or restructured responses. - Ensure that a function either returns a valid response or raises an exception without returning ambiguous results.
- Return only instances of
By following these guidelines, route functions and other Python functions in the project will be consistent, maintainable, and convey clear intent. This style is based on the patterns exhibited in various modules (e.g., instance_config.py, appdb.py, group.py, etc.) where standardized error handling, response checking, and usage of the gd.get_data
helper are consistently applied.