Humaans.ResponseHandler (Humaans v0.4.0)

View Source

This module provides helper functions for handling API responses consistently across all Humaans API resource modules.

Summary

Functions

Handles responses for delete operations, returning a map with deleted status and ID.

Handles responses for list operations, converting raw API response data into a list of resource structs.

Handles responses for single resource operations, converting raw API response data into a resource struct.

Generic response handler for all API operations.

Functions

handle_delete_response(response)

@spec handle_delete_response({:ok, map()} | {:error, any()}) ::
  {:ok, %{deleted: boolean(), id: String.t()}} | {:error, any()}

Handles responses for delete operations, returning a map with deleted status and ID.

Parameters

  • response - The response tuple from the HTTP client

Examples

response = Client.delete(client, "/endpoint/id")
ResponseHandler.handle_delete_response(response)

handle_list_response(response, resource_module)

@spec handle_list_response({:ok, map()} | {:error, any()}, module()) ::
  {:ok, [struct()]} | {:error, any()}

Handles responses for list operations, converting raw API response data into a list of resource structs.

Parameters

  • response - The response tuple from the HTTP client
  • resource_module - The module to use for converting the raw data to structs

Examples

response = Client.get(client, "/endpoint", params)
ResponseHandler.handle_list_response(response, Resources.Person)

handle_resource_response(response, resource_module)

@spec handle_resource_response({:ok, map()} | {:error, any()}, module()) ::
  {:ok, struct()} | {:error, any()}

Handles responses for single resource operations, converting raw API response data into a resource struct.

Parameters

  • response - The response tuple from the HTTP client
  • resource_module - The module to use for converting the raw data to a struct

Examples

response = Client.get(client, "/endpoint/id")
ResponseHandler.handle_resource_response(response, Resources.Person)

handle_response(response, success_handler)

@spec handle_response({:ok, map()} | {:error, any()}, (any() -> any())) ::
  {:ok, any()} | {:error, any()}

Generic response handler for all API operations.

Parameters

  • response - The response tuple from the HTTP client
  • success_handler - A function to process successful response bodies