DSpace.API.Transform (dspace_ex v0.2.1)

Copy Markdown View Source

Functions for transforming API responses.

Summary

Functions

Extracts the access token from an API response.

Casts each value in a map through the given module's from_map/1 transformation.

Extracts the CSRF token from an API response.

Transforms a value into a Date struct.

Transforms a value into a DateTime struct.

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.

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.

Transforms a value into a URI struct.

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.

cast_map(map, module)

@spec cast_map(map() | nil, module()) :: map() | nil

Casts each value in a map through the given module's from_map/1 transformation.

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.

date(date)

@spec date(nil | Date.t() | binary()) :: nil | Date.t() | binary()

Transforms a value into a Date struct.

datetime(datetime)

@spec datetime(nil | DateTime.t() | binary()) :: nil | DateTime.t() | binary()

Transforms a value into a DateTime struct.

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.

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 "No Content" 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 for 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)

uri(uri)

@spec uri(nil | binary()) :: nil | URI.t() | binary()

Transforms a value into a URI struct.