Represents a DSpace API client configuration.
Basic Usage / Configuration
dspace_ex doesn't prescribe the configuration strategy of consuming applications. For API
interactions, declare a t/0 structure with the necessary configuration when you need it:
client = %DSpace.API{
endpoint: "https://example.com/server",
access_token: "my-access-token",
csrf_token: "my-csrf-token"
}Then, use the client struct to perform an operation with one of the request functions:
{:ok, item} =
"a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
|> DSpace.API.Item.fetch()
|> DSpace.API.request(client)See new/1 for all client configuration options.
Session Management
Per DSpace API contract, all responses need to be monitored for CSRF token updates, regardless of client or use context. In practice, the actual implementation of the DSpace backend does not currently refresh CSRF tokens with every request, only with login/logout, when the token refresh endpoints are called, or the client sends an invalid token.
The optional :on_response_hook field allows applications to handle CSRF token updates. When
set, the function will be invoked whenever a response header contains a CSRF token (which should
be every response).
client = %DSpace.API{
endpoint: "https://example.com/server",
on_response_hook: &MyApp.Session.update_csrf/1
}The hook will receive a map with a :csrf_token key. Invocation is synchronous and will block
until the hook returns. It's probably best to think about using a separate process or a task to
invoke the function for asynchronous handling, depending on your session management strategy.
API Compatibility
The DSpace API is not versioned. Some endpoints and behaviours differ across DSpace versions and distributions, including "patch" or "minor" version bumps. Per default, operations in dspace_ex assume the latest DSpace release supported and may not work correctly with instances running older versions of DSpace.
However, dspace_ex includes version-specific overrides where the API differences are known. To enable them, include the DSpace version of the instance you are connecting to in the client struct:
DSpace
client = %DSpace.API{
endpoint: "https://example.com/server",
api_version: "7.6.2"
}CRIS fork
If your connect to an instance running the DSpace-CRIS fork, set the cris_version field
instead of api_version in the client struct:
client = %DSpace.API{
endpoint: "https://example.com/server",
cris_version: "2023.02.07"
}If you target a version prior to the 2023.01.01 release, please review the
DSpace.API.Version module documentation.
Doing so will
- adjust operation behaviour to the targeted DSpace version
- return a
DSpace.API.Operation.Error.t/0when you try to perform an operation that is not supported on the targeted instance instead of making a request to the server
Configuring version information at runtime
There might be scenarios where the API version of the DSpace instance you are interacting with
is not available at compile time of your application. To initialise or update a client
configuration from a live API directly, see load_version/1. To source the API version for
other configuration strategies, use the operation DSpace.API.Version.fetch/0.
Summary
Client helpers
Verifies if the passed client is authenticated with a DSpace API.
Fetches version information from a DSpace API and returns an updated client structure.
Authenticates with a DSpace API and returns an updated client structure.
Authenticates the client with a DSpace API and raises on errors.
Requests
Updates the path of an operation with a continuation URL.
Makes a request to the API and returns a result or an error.
Makes a request to the API and returns a result or raises an error.
Makes a request to the API and returns a stream.
Struct API
Creates a new API client structure.
Updates the Access token.
Updates the API version.
Updates the CRIS fork release version.
Updates the CSRF token.
Updates the API endpoint.
Updates the HTTP adapter implementation.
Updates the session hook function.
Updates the user agent.
Types
@type t() :: %DSpace.API{ access_token: nil | binary(), api_version: nil | binary(), cris_version: nil | binary(), csrf_token: nil | binary(), endpoint: URI.t() | binary() | (-> term()), http_impl: {module(), keyword()}, on_response_hook: nil | (map() -> :ok), user_agent: binary() }
A DSpace API client structure.
Client helpers
Verifies if the passed client is authenticated with a DSpace API.
Returns false if the server indicates the client is not authenticated, or if the server is
unreachable. Callers who want to separate transport errors from authentication status should
request DSpace.API.Auth.status/0 directly.
Parameters
api- ADSpace.API.t/0structure
Fetches version information from a DSpace API and returns an updated client structure.
Returns the passed DSpace.API.t/0 client structure unchanged if the version information
cannot be retrieved.
Parameters
api- ADSpace.API.t/0structure
Example
client =
[endpoint: "https://example.com/server"]
|> DSpace.API.new()
|> DSpace.API.load_version()
@spec login(t(), username, password) :: {:ok, t()} | {:error, Exception.t()} when username: binary(), password: binary()
Authenticates with a DSpace API and returns an updated client structure.
Returns returns a DSpace.API.t/0 client structure with updated access- and CSRF tokens.
Managing token lifecycle (checking expiry, deciding when to refresh) is the responsibility of
the consuming application. The token is a JWT and contains an exp claim. See
DSpace.API.Auth.refresh_access_token/0.
Executing this operation will fetch a CSRF token from the API first if none is configured in the client struct, since that is a prerequisite for hitting the login endpoint.
Parameters
api- ADSpace.API.t/0structureusername- Username as a stringpassword- Password as a string
Usage
The login operation is executed directly when calling this function. The returned client can then immediately be used for follow-up operations:
client =
[endpoint: "https://example.com/server"]
|> DSpace.API.new()
|> DSpace.API.login("username", "password")
items =
Item.list()
|> DSpace.API.stream!(client)
Authenticates the client with a DSpace API and raises on errors.
Similar to login/3, but returns the updated client structure directly or raises on errors.
Requests
@spec next_page(DSpace.API.Operation.JSON.t(), binary()) :: DSpace.API.Operation.JSON.t()
Updates the path of an operation with a continuation URL.
This function is intended to be used with paginated responses, where the continuation URL
is returned as the third element of the response tuple {items, meta, next_url}.
Most users will prefer to stream operations that return paginated results, as stream!/3 wraps
pagination automatically and returns a lazy Stream of resources.
Parameters
operation- ADSpace.API.Operation.t/0next_url- The continuation URL returned as the third element of a paginated response tuple{items, meta, next_url}
Example
client = DSpace.API.new("https://example.com/server")
operation = Collection.list()
{:ok, {collections, _meta, next_url}} = DSpace.API.request(operation, client)
{:ok, {more_collections, _meta, _next_url}} =
operation
|> DSpace.API.next_page(next_url)
|> DSpace.API.request(client)
@spec request(DSpace.API.Operation.t(), t(), keyword()) :: {:ok, term()} | {:error, Exception.t()}
Makes a request to the API and returns a result or an error.
Parameters
operation- ADSpace.API.Operation.t/0api- ADSpace.API.t/0structureoptions- Keyword list of options for the request
Options
:transform- How to transform the API response. Can bei either- a 1-arity function that takes a
DSpace.API.HTTP.Response.t/0struct and returns a transformed value false- passes the rawDSpace.API.HTTP.Response.t/0struct
- a 1-arity function that takes a
- request option overrides passed to the HTTP adapter
Examples
client = DSpace.API.new("https://example.com/server")
{:ok, item} =
"a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
|> DSpace.API.Item.fetch()
|> DSpace.API.request(client)
@spec request!(DSpace.API.Operation.t(), t(), keyword()) :: term()
Makes a request to the API and returns a result or raises an error.
For parameters and options, see request/3.
@spec stream!(DSpace.API.Operation.t(), t(), keyword()) :: Enumerable.t()
Makes a request to the API and returns a stream.
Parameters
operation- ADSpace.API.Operation.t/0api- ADSpace.API.t/0structureoptions- Keyword list of options for the request
Options
- request option overrides passed to the HTTP adapter
Examples
client = DSpace.API.new("https://example.com/server")
stream =
[query: "software errors"]
|> DSpace.API.Item.find()
|> DSpace.API.stream!(client)
Enum.take(stream, 5)
Struct API
Creates a new API client structure.
Parameters
This function takes either
- a keyword list of attributes or
- a single argument that represents the DSpace API endpoint; either as
Attributes
endpoint- The DSpace API endpoint, e.g. https://example.com/server. Can be either:access_token- Optional login token or API key used for authentication:csrf_token- Optional CSRF token. Needed for all modifying requests:api_version- Optional base DSpace API version as a string, e.g.10.0.0:cris_version- Optional CRIS fork release version, e.g.2025.02.00orcris-2025.02.00:user_agent- Optional User agent string, defaults todspace-ex/0.1.1:http_impl- Optional HTTP adapter implementation and options as{module, options}:on_response_hook- Optional callback function invoked when CSRF tokens are updated
Updates the Access token.
Parameters
api- ADSpace.API.t/0structureaccess_token- Login token or API key as a string
Updates the API version.
Parameters
api- ADSpace.API.t/0structureversion- Base DSpace API version as a string, e.g.10.0.0
Updates the CRIS fork release version.
Parameters
api- ADSpace.API.t/0structureversion- DSpace-CRIS release version as a string, with or without thecris-prefix, e.g.2025.02.00
Updates the CSRF token.
Parameters
api- ADSpace.API.t/0structurecsrf_token- CSRF token as a string
Updates the API endpoint.
Parameters
api- ADSpace.API.t/0structureendpoint- The DSpace API endpoint, e.g. https://example.com/server. Can be either
Updates the HTTP adapter implementation.
Parameters
api- ADSpace.API.t/0structurehttp_impl- A tuple of{module, options}where module implementsDSpace.API.HTTPbehaviour.
Updates the session hook function.
Parameters
api- ADSpace.API.t/0structureon_response_hook- A 1-arity function invoked when CSRF tokens are updated
Updates the user agent.
Parameters
api- ADSpace.API.t/0structureuser_agent- User agent as a string