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 DSpace.API.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:
{: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. 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 :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.
Summary
Functions
Verifies if the passed client is authenticated with the DSpace API.
Authenticates with a DSpace API and returns an updated client structure.
Authenticates the client with a DSpace API and raises on errors.
Creates a new API client.
Updates the Access token.
Updates the API version.
Updates the CSRF token.
Updates the API endpoint.
Updates the HTTP adapter implementation.
Updates the session hook function.
Updates the user agent.
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.
Types
Functions
Verifies if the passed client is authenticated with the 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
@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.
Creates a new API client.
Parameters
This function takes either
- a keyword list of attributes or
- a single argument that represents the DSpace REST endpoint; either as
Attributes
endpoint- The DSpace REST 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 DSpace API version as a string, defaults to9.2.0:user_agent- Optional User agent string, defaults todspace-ex/0.1.0-alpha2: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- DSpace API version as a string
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 REST 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
@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- Options for the request
Options
:transform- Whether to transform the response. If set to false, returns aDSpace.API.HTTP.Response.t/0structure (defaults to true)- request option overrides passed to the HTTP adapter
@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.
For parameters and options, see request/3.