DSpace.API.Transform (dspace_ex v0.2.0)

Copy Markdown View Source

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

access_token_from_response(response)

@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.

csrf_token_from_response(response)

@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.

from_response(arg1)

@spec from_response(DSpace.API.HTTP.Response.t() | term()) :: map()

Extracts the body as a map from an API response structure.

get(response, key, default \\ nil)

Extracts a value from the response body by key.

not_found_on_no_content(response, message \\ "Resource not found")

@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.

token_from_response(response)

Extracts a token from an API response.

Returns the token or an error.

tokens_from_response(response)

@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.

transform(map, options)

@spec transform(
  DSpace.API.HTTP.Response.t() | map(),
  keyword()
) :: [term()]

Extracts and transforms a collection of resources from a response.

Parameters

Options

  • :extract - A path to navigate to the resource(s) (defaults to nil)
  • :transform - A function to process each resource (defaults to identity)

transform_collection(map, options)

@spec transform_collection(
  struct() | map(),
  keyword()
) :: {[term()], map(), term()}

Extracts resources and metadata from a paginated collection response.

Returns a three-element tuple {data, meta, next}:

  • data - List of transformed resources
  • meta - Meta information from the API response
  • next - URL for next page or nil if no more pages

Parameters

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)