Functions for transforming API responses.
Summary
Functions
Extracts the access token from an API response.
Extracts the CSRF token from an API response.
Extracts the body as a map from an API response structure.
Extracts a value from the response body by key.
Returns a :not_found error when the response status is 204 (No Content).
Extracts a token from an API response.
Extracts the access token and CSRF token from an API response.
Extracts and transforms a collection of resources from a response.
Extracts resources and metadata from a paginated collection response.
Functions
@spec access_token_from_response(DSpace.API.HTTP.Response.t()) :: {:ok, binary()} | {:error, DSpace.API.Error.t()}
Extracts the access token from an API response.
Returns the token or an error.
@spec csrf_token_from_response(DSpace.API.HTTP.Response.t()) :: {:ok, binary()} | {:error, DSpace.API.Error.t()}
Extracts the CSRF token from an API response.
Returns the token or an error.
@spec from_response(DSpace.API.HTTP.Response.t() | term()) :: map()
Extracts the body as a map from an API response structure.
Extracts a value from the response body by key.
@spec not_found_on_no_content(DSpace.API.HTTP.Response.t(), binary()) :: map() | DSpace.API.Error.t()
Returns a :not_found error when the response status is 204 (No Content).
Some endpoints that conceptually fetch a single resource are implemented as searches
(e.g. /api/eperson/epersons/search/byEmail). When no match is found, the API returns 204
reflecting an "empty search result" rather than a missing resource.
From the caller's perspective, however, these are unique-key lookups: an email uniquely
identifies at most one user (this is enforced by DSpace), so an absent result is semantically
a missing resource, not an empty collection. This function can be used to normalize that
behaviour by converting 204 responses into a :not_found error, giving callers a consistent
signal.
Extracts a token from an API response.
Returns the token or an error.
@spec tokens_from_response(DSpace.API.HTTP.Response.t()) :: {:ok, {binary(), binary()}} | {:error, DSpace.API.Error.t()}
Extracts the access token and CSRF token from an API response.
Returns the tokens or an error.
@spec transform( DSpace.API.HTTP.Response.t() | map(), keyword() ) :: [term()]
Extracts and transforms a collection of resources from a response.
Parameters
map- A map orDSpace.API.HTTP.Response.t/0containing the resource(s)options- Keyword list of options
Options
:extract- A path to navigate to the resource(s) (defaults tonil):transform- A function to process each resource (defaults to identity)
Extracts resources and metadata from a paginated collection response.
Returns a three-element tuple {data, meta, next}:
data- List of transformed resourcesmeta- Meta information from the API responsenext- URL for next page or nil if no more pages
Parameters
map- A map orDSpace.API.HTTP.Response.t/0containing the resource collectionoptions- Keyword list of options
Options
:extract- A path to navigate to the resources (required):next- A path to navigate to the continuation token (defaults to["_links", "next", "href"]):transform- A function to process each resource (defaults to identity)