Expand | ||
---|---|---|
| ||
|
...
Introduction
...
Expand | ||
---|---|---|
| ||
|
Section | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Introduction
|
Section | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
URLs
|
Section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Resource representation
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Panel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Info |
---|
The type of case are defined as follows: Camel case camelCase Snake case snake_case |
- URI MUST use snake case
- Resources MUST use snake case
- Query Parameter Keys MUST use camel case.
- Using camel case will reduce mapping effort between query parameter and backend variables
- Query Parameters Values are application specific
- Request Body MUST use camelCase
- Follow Google’s JSON guidelines
- Response Body MUST camelCase
- Follow Google’s JSON guidelines
- Resource names SHOULD be plural
- e.g. dogs instead of dog
- Resources SHOULD be nouns and not verbs
Identification
A resource is the entity on which an operation is performed. For instance - Create, Read, Update, Delete
- Concrete resources are better than abstract
- e.g. /dogs better than /animal
- Two URLs per resource are needed
- The first one is for a collection. Use plural to refer to collection
- e.g. /applications, /dogs etc.
- The second one is for an instance of a resource
- e.g. /applications/a1b2, /dogs/{id}
- The first one is for a collection. Use plural to refer to collection
- If more than one identifier is needed to identify a resource, they MUST be concatenated with commas
- e.g. /classes/CSE,100,SP18 instead of /classes/CSE/100/SP18
- When there are multiple types of resource identifiers, it is a good practice to qualify the identifier.
- e.g. /employees/employee_id=123 or /employees/racfid=AAAZZZ
title | WSO2 API Manager @ UCSD Specifics |
---|
The API Version is used to determine which iteration of an API an incoming request is attempting to access. It is used in conjunction with the API Context. For example api.ucsd.edu/echo/2.3/test is attempting to use version 2.3 of the echo API. The WSO2 manager will by default add the version to the end of your chosen context. You can specify where the version should appear in the URL by using "{version}" in the context but this is NOT RECOMMENDED and in almost all cases be avoided. The API Version is commonly numbers and dots (e.g. 1.2) following the major/minor convention used elsewhere in computer science, although it is also common to only use a single number as APIs change very rarely. Often a "v" is includes at the start of a version number ( e.g. "v1.2" ).
Final URL
(calculated)
Although supported by the WSO2 API Manager, this is NOT RECOMMENDED and often causes the API to become corrupted.
We recommend sticking to numbers, dots and an optional leading "v"
LINKING
borderColor | #ffab00 |
---|---|
bgColor | white |
titleColor | white |
borderWidth | 1 |
titleBGColor | #ffab00 |
borderStyle | solid |
title | Guidelines |
Linking
Linking is fundamental to scalability and flexibility of the system
MANY to MANY Relationship (example to be added later)
Represent the links separately
e.g. users and groups have many to many relations
Information about users/1 will return,
users/1
users/1/groups/1
users/1/groups/2
groups/1
Dealing with relations
If a relationship can only exist within another resource, RESTful principles provide useful guidance. e.g. A Class consists of a number of sections. These sections can be logically mapped to the /classes endpoint as follows:
- GET /classes/12/sections - Retrieves list of sections for class 12
Limiting which fields are returned by the API
The API consumer does not always need the full representation of a resource. The ability to select and choose returned fields goes a long way in letting the API consumer minimize network traffic and speed up their own usage of the API.
Using a comma-separated list of fields to include in the fields query parameter allows you to restrict what data is returned. For example, the following request would retrieve just enough information to display a sorted listing of open tickets:
GET /tickets?fields=id,subject,customer_name,updated_at&state=open&sort=-updated_at
Search and other Algorithms
Use a combination of collection and query parameters to filter out information appropriately.
Efficiency
- Query parameter use ?expand=xyz to expand the xyz resource
- Alternately, return fields=givenName, surname,directory(name)
- Should support parameters offset and limit
you can also add nested partial fields
in the following format
fields?=id,customer(id,name)
in this case, return object will in the following format
Code Block | ||
---|---|---|
| ||
[{
"id":"12",
"customer":{
"id": "232",
"name" :"abc corp"
}
},
...
] |
HTTP
Panel | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
RequestThe API MUST use proper HTTP methods, and operation idempotency* MUST be respected. Below is a list of methods that APIs SHOULD support. Not all resources will support all methods, but all resources using the methods below MUST conform to their usage. | ||||||||||||||
Method | Description | Is Idempotent* | Is | Example | Response | |||||||||
GET | Retrieve a collection of objects | True | True | GET /dogs |
| |||||||||
GET | Retrieve a specific object | True | True | GET /dogs/10 |
| |||||||||
POST | Create a new object | False | False | POST /dogs |
| |||||||||
PUT | Replace a specific object | True | False | PUT /dogs/10 |
| |||||||||
PATCH | Partially update a specific object | False | False | PATCH /dogs/10 |
| |||||||||
DELETE | Delete a specific object | True | False | DELETE /dogs/10 |
| |||||||||
OPTIONS | Get information about a request, notably which other methods are supported | True | True | OPTIONS /dogs |
| |||||||||
HEAD | Identical to GET, except MUST NOT return a body Response headers SHOULD be the same as equivalent GET request | True | True | HEAD /dogs/10 |
| |||||||||
Code | Error code (over and above HTTP code) to describe the error message | |||||||||||||
Message | User-friendly message | |||||||||||||
Developer message | Message to debug the problem | |||||||||||||
More info | Link to additional information |
Code Block | ||
---|---|---|
| ||
{
"message": string,
"developerMessage": string,
"moreInformation": string,
"errors": [
{
"message": string
}
],
"code": string
} |
Note:
- Either message or message array needs to exist.
- All other elements are optional and if the value is null, the element should be omitted. An element should not be an empty string.
- Use an array of messages to capture multiple validation errors.
- 400 The request is malformed, such as if the body does not parse
- 401 Unauthenticated. When no or invalid authentication details are provided. Also useful to trigger an auth popup if the API is used from a browser
- 403 The request is a legal request but user is unauthorized
- 404 Not Found - When a non-existent resource is requested
- 405 When an HTTP method is being requested that isn't allowed for the authenticated user.
- 409 Resource Conflict can be possible in complex systems. It can be caused by fulfilling the request. Duplicate entries, deleting root objects when cascade-delete not supported are a couple of examples.
- 410 Gone - Indicates that the resource at this endpoint is no longer available. Useful as a blanket response for old API versions
- 415 Unsupported Media Type - If incorrect content type was provided as part of the request
- 422 Unprocessable Entity - Used for validation errors
- 429 Too Many Requests - When a request is rejected due to rate limiting
HTTP Error Code
Client error codes
Server Error Codes
- 500 INTERNAL SERVER ERROR – The general catch-all error when the server-side throws an exception
- 501, Not Implemented. The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource
- 503 Service Unavailable. The server is currently unable to handle the request due to a temporary overloading or maintenance of the serve
Business Exceptions
While none of the HTTP response codes match well with a business rule exception, use of the HTTP code 403 indicates that the server is refusing to fulfill the request. This status code can be used when a business rule won’t allow fulfillment of the request - e.g. Enrollment when a class is full.
Timestamp
Per ISO 8601 guidelines, always use UTC time zone.
SecurityIt is recommended to use the HAL specification to represent responses in json or xml. HAL provides a set of conventions to express HATEOAS linking consistently.
Content negotiation
Content negotiation allows an API to provide multiple representations of a resource with the same URL. Two headers primarily used to enable this are Accept and Content-Type.
- Accept - used by the client to specify which representation type is preferred in the response when there are multiple options available.
- Content-Type - used by both the client and API to specify the representation of the request / response bodies.
Single resource
Single entity resources are targeted with urls that end in a unique identifier. For example:
Info | ||
---|---|---|
| ||
Resources are composed of name / value pairs where the values can be basic data types, objects, or arrays.
Basic data types
Strings:
Code Block |
---|
{
"title" : "REST API Guidelines"
} |
Numbers:
Code Block |
---|
{
"integer" : 1234,
"negativeInteger" : -1234,
"decimal" : 1234.01,
"scientificNotation" : 1.2E+3
} |
Boolean:
Code Block |
---|
{
"flag" : true
} |
Date/Time:
Date and time formats MUST follow the ISO8601 format specified in RFC3339 Section 5.6. Examples:
Code Block |
---|
{
"date" : "2019-03-25",
"timeLocal" : "13:30:57",
"timeUTC" : "13:30:57Z",
"timePSTOffset" : "13:30:57-08:00",
"dateTimeUTC" : "2019-03-25T13:30:57Z",
"dateTimePSTOffset" : "2019-03-25T13:30:57-08:00"
} |
Objects
Resources can have attributes values represented as other objects. If object is a sub-resource, meaning that can be specifically accessed via a URL, then it SHOULD be listed under the _embedded attribute following the HAL specification. A self link should be provided as well for easy access to the sub-resource.
Arrays
Attributes values can also be arrays. Arrays are collections of homogeneous values. The values can be basic data types or objects.
Links
Links are used to provide possible actions that can be made on a resource given its current state. More information can be found later in this document in the Links section.
Collections of resources
Resource collections are targeted with URLs that don’t specify a single resource with an identifier. For example:
Info | ||
---|---|---|
| ||
Even if a filtering option is provided that results in a single resource, an array of resources should still be expected by the user agent. Collections of resources SHOULD be listed under the _embedded attribute, following the HAL specification, indicating that individual resources can be directly accessed via a self URL.
If the resources in the collection are large, then a reduced representation can be used that only contains the minimum attributes necessary for identification. The reduced resources MUST be accompanied by a self link to retrieve the full resource by itself.
Paging
It is recommended to provide paging options on large collections of resources to let the user agent limit the number of resources retrieved at one time. Additionally, links SHOULD be provided to facilitate the traversal of the paged collection.
More information can be found later in the document under the Pagination section.
Sorting
It is recommended to provide sorting options on large collections of resources. Even if sorting options are not provided, it is recommended to specifically order the collection by default for consistent responses.
More information can be found later in the document under the Sorting section.
Filtering
It is recommended to provided filtering options on large collections of resources.
More information can be found later in the document under the Filtering section.
Section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
HTTP
|
Section | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Section | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Section | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Section | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Security
|
...
Section |
---|
References |
...