DCSA API Design Principles 1.0
Total Page:16
File Type:pdf, Size:1020Kb
DCSA API Design Principles 1.0 September 2020 Table of contents Change history ___________________________________________________ 4 Terms, acronyms, and abbreviations _____________________________________ 4 1 Introduction __________________________________________________ 5 1.1 Purpose and scope ________________________________________________ 5 1.2 API characteristics ________________________________________________ 5 1.3 Conventions ____________________________________________________ 6 2 Suitable _____________________________________________________ 6 3 Maintainable __________________________________________________ 6 3.1 JSON _________________________________________________________ 6 3.2 URLs __________________________________________________________ 6 3.3 Collections _____________________________________________________ 6 3.4 Sorting ________________________________________________________ 6 3.5 Pagination______________________________________________________ 7 3.6 Property names __________________________________________________ 8 3.7 Enum values ____________________________________________________ 9 3.8 Arrays _________________________________________________________ 9 3.9 Date and Time properties ___________________________________________ 9 3.10UTF-8 _________________________________________________________ 9 3.11 Query parameters _______________________________________________ 10 3.12 Custom headers ________________________________________________ 10 3.13 Binary data _____________________________________________________ 11 3.14Error handling ___________________________________________________ 11 3.15Subscription model_______________________________________________ 13 3.15.1 Subscription management ______________________________________ 13 3.15.2 Retry policy ________________________________________________ 14 3.15.3 Verification ________________________________________________ 14 2 DCSA API Design Principles 1.0 4 Stable ______________________________________________________ 15 4.1 Versioning _____________________________________________________ 15 4.2 Backward compatibility ____________________________________________ 15 4.3 Deprecation ___________________________________________________ 16 5 Secure ______________________________________________________ 16 5.1 Endpoints _____________________________________________________ 16 5.2 Signature used for the subscription model _______________________________ 16 6 Performant ___________________________________________________ 17 7 Well-documented ______________________________________________ 17 7.1 General documentation ____________________________________________17 7.2 HATEOAS ______________________________________________________ 18 Appendix A: Sequence diagrams of Subscription model _________________________ 18 Tables Table 1: API quality characteristics ___________________________________________ 5 Table 2: Examples of sorting _______________________________________________ 7 Table 3: Pagination _____________________________________________________ 8 Table 4: Examples of time format ___________________________________________ 9 Table 5: Query parameters naming conventions ________________________________ 10 Table 6: Custom headers Binary data ________________________________________ 11 Figures Figure 1: Creating a subscription ___________________________________________ 18 Figure 2: Publishing an event _____________________________________________ 19 Figure 3: Publisher changes the secret key ____________________________________ 20 Figure 4: Subscriber changes the secret key ___________________________________ 21 Figure 5: Tempered event _______________________________________________ 21 3 DCSA API Design Principles 1.0 Change history Rev Issue Contributors Description 1.0 September 2020 DCSA First release Terms, acronyms, and abbreviations Term Definition API Application programming interface camelCase In this document camelCase is defined as lower camel case. Definition of lower camel case is that initial letter is lower and every subsequent word is Capitalized. CRUD Create, Read, Update and Delete (equivalent to POST, GET, PUT/PATCH and DELETE rest operations) FK Foreign Key HATEOAS Hypermedia as the Engine of Application State kebab-case kebab-case combines words by replacing each space with a dash (-). All letters are lower case. For example, this-is-a-kebab-case-example. PK Primary Key UUID Universally unique identifier 4 DCSA API Design Principles 1.0 1 Introduction This chapter describes the purpose, structure and conventions used in this document. 1.1 Purpose and scope The purpose of this document is to establish guidelines on how APIs should be designed, developed and implemented in the digital container shipping context. The guidelines describe the requirements that DCSA-compliant APIs should fulfil. 1.2 API characteristics As described in the table below, a good API should have the following quality characteristics or have a balance of these quality characteristics. Index Characteristics Descriptions 1 Suitable APIs provide a well-defined service that optimizes value for consumers by having clearly defined responsibilities and an appropriate level of abstraction. 2 Maintainable It is easy to understand how to interact with the API and development of maintainable code is facilitated. 3 Stable The occurrence and impact of major changes are minimized. 4 Secure Measures and practices are implemented to prevent abuse. 5 Performant The performance consequences of API design are considered, monitored and optimized for common interactions. 6 Well-documented Up-to-date, complete and easy-to-read documentation is available to facilitate adoption. Table 1: API quality characteristics The structure of this documents follows the order of the API quality characteristics mentioned in the table above. 5 DCSA API Design Principles 1.0 1.3 Conventions The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 (https://www.ietf.org/rfc/rfc2119.txt). 2 Suitable Good APIs are suitable regarding their functionality and responsibilities. Suitable APIs provide a well-defined service that optimizes value for consumers by having clearly defined responsibilities and an appropriate level of abstraction. To achieve this objective, two requirements are identified below: - The API is constructed with the consumer in mind and SHOULD be based on User Stories. - The API is constructed according to a minimalistic approach less is more. 3 Maintainable APIs need to be maintainable. This means it should be easy to understand how to interact with the APIs, and development of maintainable code should be facilitated. The requirements to achieve good maintainability are described in this chapter. 3.1 JSON Requests and responses MUST be in The application/json Media Type for JavaScript Object Notation (JSON). Requests and responses MAY be in other formats as well e.g. XML Media Types. 3.2 URLs MUST point to a resource (entity, process). MUST consist of nouns and MUST NOT be actions. HTTP verbs (GET, PUT, PATCH, POST, DELETE) MUST be used for actions. MUST be used to indicate hierarchical relations. Kebab-case MUST be used in URLs. Path parameters MUST be consistent with property names. Path parameters referring to property names MUST use camelCase. Query parameters MUST use camelCase. 3.3 Collections Collection items MUST NOT consist of composite keys. A unique key MUST identify a collection element. Collections SHOULD be pluralized. 3.4 Sorting Sorting SHOULD be limited to specific fields. The parameter MUST be restricted to the values ASC (ascending order) and DESC (descending order) describing the sort direction. The direction MAY be omitted. If omitted ascending order MUST be used. A colon : is used to separate the field to sort by and the direction. Multiple fields can be used, in which case each field is separated by a comma,. The following table shows some examples: 6 DCSA API Design Principles 1.0 Query Parameter Explanation sort=name Sort by the name field in ascending (default) order. sort=name:desc Sort by the name field in descending order. sort=name:asc Sort by the name field in ascending order. sort=name,age:desc Sort first by the name field in ascending order. If two equal names exist, then sort those names by age in descending order. Table 2: Examples of sorting The sort parameter is optional and if not specified, the order is set by the implementer. It is up to the implementer to find a suitable Collator to use when sorting on String values. 3.5 Pagination GET requests on collection results SHOULD implement pagination. Links to current, previous, next, first and last page SHOULD be available in the response headers; more links MAY be present. Link Can Be Null Explanation Example First-Page Yes A link to the first page. First- <https://api.dcsa.org/events? Page header link MAY be limit=20&cursor=xxx>; omitted if current page is the rel="First-Page" first page. Previous-Page Yes A link to the previous page. <https://api.dcsa.org/events? Previous-Page header link MAY limit=20&cursor=xxx>; be omitted if the current page rel="Previous-Page" is the first page. 7 DCSA API Design Principles 1.0 Link Can Be Null Explanation Example Next-Page Yes A link to the next page. Next- <https://api.dcsa.org/events? Page header link MAY be limit=20&cursor=xxx>; omitted if the current page is rel="Next-Page" the last page. Last-Page Yes A link to the last page.