DSpace / RestContract

REST Contract for DSpace 7-8
https://wiki.lyrasis.org/display/DSDOC8x/
37 stars 46 forks source link
dspace rest rest-api

DSpace 7 REST Contract

This repository documents new DSpace REST API Contract beginning with version 7.0.

Table of Contents

REST Endpoints

At the ROOT of the API a HAL document lists all the primary endpoints allowing full discovery of the API.

Use of HTTP Verbs and Response Codes

Please note that within this section, all terms used are meant to reference RESTful terminology and/or terminology borrowed from Spring Data REST.

On Collection of Resources Endpoints

This type of endpoint interacts with a group (or collection) of resources (objects). For example: /api/core/items references all Items in the system.

On Single Resource Endpoints

This type of endpoint interacts with a single, existing resource (object). For example: /api/core/items/<:uuid> references a single Item resource.

On Sub-Path of a Single Resource Endpoint (Associations)

This type of endpoint interacts with (one or more) resources that are associated with a single resource. For that reason, it is sometimes called an "association" endpoint. For example: /api/core/items/<:uuid>/mappedCollections references all Collections that are mapped to a single Item resource.

Error Codes

Pagination

Each endpoint that exposes a collection of resources, including sub-paths for embedded or linked collections (aka list of items of a collection, etc.), MUST implement the pagination with the following common behavior

Pagination Request Parameters

Pagination Response

The HAL document always includes a page object with the following attributes

An example

"page": {
    "size": 5,
    "totalElements": 14,
    "totalPages": 3,
    "number": 0
}

When applicable, the following links may also appear:

An example

"_links": {
    "first": {
        "href": "http://localhost:8080/server/api/core/bitstreams?page=0&size=5"
    },
    "self": {
        "href": "http://localhost:8080/server/api/core/bitstreams"
    },
    "next": {
        "href": "http://localhost:8080/server/api/core/bitstreams?page=1&size=5"
    },
    "last": {
        "href": "http://localhost:8080/server/api/core/bitstreams?page=2&size=5"
    }
}

Out of Bound Pages

If the request parameters lead to a page outside the result set, then an empty page should be returned with the links needed to go to the first and last page of the result set (if the results set is not empty), and the total number of resources in the collection.

Pagination Error Codes

400 Bad Request - If an unknown sort criteria is requested, or a not valid ordering keyword is specified

REST Design Principles

In the creation of the REST API, we've tried to follow a few specific design principles listed below

On the Naming of Endpoints

We strive to align with these rules for all REST API endpoint names:

HATEOAS & HAL

The REST API supports the HATEOAS paradigm and adopt the HAL format to express links and embedded resources. Links are always absolute to allow for easier implementation of "follow link" methods on the REST client side.

Because the API responds using the HAL Format, we distribute it with an embedded HAL Browser provided by Spring.

Statelessness

The REST API is stateless, meaning the client is responsible for keeping state information and sending it to the server when it is necessary. Because the server stores no state (or session) information internally, it will return a "token" (which contains any state information) to the client. The client must return that token in each subsequent request, if the client wants that state information preserved.

JSON Web Tokens

JSON Web Tokens (JWT) are used to store state (and authentication) information between requests. This is the format of token the REST API returns to the client. The client should return the JWT to the server in subsequent requests.

ALPS - Application Level Profile Semantics

While not yet implemented, we expect future support for the ALPS metadata (http://alps.io/), so a profile link MUST exist from the root of API. A profile link as defined in RFC 6906, is a place to include application level details. See the ALPS draft spec (http://tools.ietf.org/html/draft-amundsen-richardson-foster-alps-00)

Spring Technology Alignment

While it's an implementation detail, the new REST API uses many Spring REST (Java) libraries, including:

Each of these libraries were chosen based on design principles above, and based on the fact that much of our underlying Java APIs use or align with Spring technologies.

Content Negotiation

The DSpace REST API only supports the application/json response format at this time. So, there is no real content negotiation according to the broad meaning of the term.

Nevertheless, some concepts of content negotiation still apply.

Language Support

While most i18n (internationalization) settings exist in the UI layer, there are some backend features which also require i18n, such as Submission forms, Controlled Vocabularies, and Authorities.

For such features the Accept-Language header may be used by the REST client to force the use of a specific locale, if supported by the DSpace instance and endpoint in the response. (Please note that not all the DSpace instances support multiple locales. This can be discovered by querying the configuration endpoint for the webui.supported.locales key.)

If the support for multiple locales is enabled in the DSpace instance, a request with an Accept-Language header is expected to be processed as follows:

The REST client is expected to use the Content-Language Response Header as part of their caching strategy.

Please note that a REST client MUST always send the Accept-Language header for all the subsequent requests after a user has chosen a preferred language/locale.

If no explicit locale is requested by the client, or the requested locale is not supported, then DSpace will check if the current user is authenticated and has a preferred language stored (if any). If so, DSpace will respond in the preferred locale. If not (or if the user is not authenticated), DSpace will respond with the configured default locale (configuration key default.locale) as a fallback.

Proxies

The DSpace REST API supports the X-Forwarded-For header by default for all requests coming from localhost (127.0.0.1). This allows any clients running on the same server to immediately proxy requests for other IP addresses.

If your client (or User Interface) is on a different server, you may add additional "trusted" IP addresses (or ranges) which are allowed to use the X-Forwarded-For header. To do so, modify the proxies.trusted.ipranges setting in your local.cfg. This configuration accepts a comma-separated list of IPs and/or you may specify a range by listing the first three blocks of the IP range (e.g. 123.45.67)

Keep in mind, the X-Forwarded-For header is ONLY read if useProxies=true (default value) is also set in your DSpace configuration (either dspace.cfg or local.cfg).

ETags & conditional headers

The ETag header (http://tools.ietf.org/html/rfc7232#section-2.3) provides a way to tag resources. This can prevent clients from overriding each other while also making it possible to reduce unnecessary calls. It is expected that all the returned document have an ETag

The ETag value can be used with in GET request with the If-None-Match conditional header. If the header MATCHES the ETag, the API will conclude nothing has changed, and instead of sending a copy of the resource, an HTTP 304 Not Modified status code is returned.

The ETag value can be also used with DELETE, POST, PUT and PATCH with the If-Match conditional header to avoid performing an action on changed resources (concurrency issues, optimistic lock approach).

Finally, when possible the If-Modified-Since header in GET request should be respected. If the resource has been not modified since the value of the Header the API should return an HTTP 304 Not Modified status code. Resources that support the If-Modified-Since header MUST return the Last-Modified Header in the GET response. That header MUST BE NOT returned by resources not able to manage the If-Modified-Since header.

API Versioning

At this time, DSpace REST API is versioned alongside DSpace software (e.g. DSpace v7.x comes with REST API v7). Please check the Release Notes for information around any deprecations / breaking changes to the API between major versions of DSpace.

Community Resources