[关闭]
@xiaoyixy 2020-07-16T02:21:04.000000Z 字数 10180 阅读 674

How to design a REST API

API设计


Learning REST in pieces is one thing, while applying all those learned concepts into real application design is completely another challenge. In this tutorial, we will learn to design REST APIs for a network based application. Please note that the takeaway from this whole exercise is the learning of how to apply REST principles in design process.

Steps in designing REST Services

Identify Object Model

The very first step in designing a REST API based application is – identifying the objects which will be presented as resources.

For a network based application, object modeling is pretty much simpler. There can be many things such as devices, managed entities, routers, modems etc. For simplicity sake, we will consider only two resources i.e.

Here configuration is sub-resource of a device. A device can have many configuration options.

Note that both objects/resources in our above model will have a unique identifier, which is the integer id property.

Create Model URIs

Now when object model is ready, it’s time to decide the resource URIs. At this step, while designing the resource URIs[HTML Preview] – focus on the relationship between resources and its sub-resources. These resource URIs are endpoints for RESTful services.

In our application, a device is a top-level resource. And configuration is sub-resource under device. Let’s write down the URIs.

  1. /devices
  2. /devices/{id}
  3. /configurations
  4. /configurations/{id}
  5. /devices/{id}/configurations
  6. /devices/{id}/configurations/{id}

Notice that these URIs do not use any verb or operation. It’s very important to not include any verb in URIs. URIs should all be nouns only.

Determine Representations

Now when resource URIs have been decided, let’s work on their representations. Mostly representations are defined in either XML or JSON format. We will see XML examples as its more expressive on how data is composed.

Collection of Device Resource

When returning a collection resource, include only most important information about resource. This will keep the size of payload small, and so will improve the performance of REST APIs.

  1. <devices size="2">
  2. <link rel="self" href="/devices"/>
  3. <device id="12345">
  4. <link rel="self" href="/devices/12345"/>
  5. <deviceFamily>apple-es</deviceFamily>
  6. <OSVersion>10.3R2.11</OSVersion>
  7. <platform>SRX100B</platform>
  8. <serialNumber>32423457</serialNumber>
  9. <connectionStatus>up</connectionStatus>
  10. <ipAddr>192.168.21.9</ipAddr>
  11. <name>apple-srx_200</name>
  12. <status>active</status>
  13. </device>
  14. <device id="556677">
  15. <link rel="self" href="/devices/556677"/>
  16. <deviceFamily>apple-es</deviceFamily>
  17. <OSVersion>10.3R2.11</OSVersion>
  18. <platform>SRX100B</platform>
  19. <serialNumber>6453534</serialNumber>
  20. <connectionStatus>up</connectionStatus>
  21. <ipAddr>192.168.20.23</ipAddr>
  22. <name>apple-srx_200</name>
  23. <status>active</status>
  24. </device>
  25. </devices>

Single Device Resource

Opposite to collection URI, here include complete information of a device in this URI. Here, also include a list of links for sub-resources and other supported operations. This will make your REST API HATEOAS driven.

  1. <device id="12345">
  2. <link rel="self" href="/devices/12345"/>
  3. <id>12345</id>
  4. <deviceFamily>apple-es</deviceFamily>
  5. <OSVersion>10.0R2.10</OSVersion>
  6. <platform>SRX100-LM</platform>
  7. <serialNumber>32423457</serialNumber>
  8. <name>apple-srx_100_lehar</name>
  9. <hostName>apple-srx_100_lehar</hostName>
  10. <ipAddr>192.168.21.9</ipAddr>
  11. <status>active</status>
  12. <configurations size="2">
  13. <link rel="self" href="/configurations" />
  14. <configuration id="42342">
  15. <link rel="self" href="/configurations/42342" />
  16. </configuration>
  17. <configuration id="675675">
  18. <link rel="self" href="/configurations/675675" />
  19. </configuration>
  20. </configurations>
  21. <method href="/devices/12345/exec-rpc" rel="rpc"/>
  22. <method href="/devices/12345/synch-config"rel="synch device configuration"/>
  23. </device>

Configuration Resource Collection

Similar to device collection representation, create configuration collection representation with only minimal information.

  1. <configurations size="20">
  2. <link rel="self" href="/configurations" />
  3. <configuration id="42342">
  4. <link rel="self" href="/configurations/42342" />
  5. </configuration>
  6. <configuration id="675675">
  7. <link rel="self" href="/configurations/675675" />
  8. </configuration>
  9. ...
  10. ...
  11. </configurations>

Please note that configurations collection representation inside device is similar to top-level configurations URI. Only difference is that configurations for a device are only two, so only two configuration items are listed as subresource under device.

Single Configuration Resource

Now, single configuration resource representation must have all possible information about this resource – including relevant links.

  1. <configuration id="42342">
  2. <link rel="self" href="/configurations/42342" />
  3. <content><![CDATA[...]]></content>
  4. <status>active</status>
  5. <link rel="raw configuration content" href="/configurations/42342/raw" />
  6. </configuration>

Configuration Resource Collection Under Single Device

This resource collection of configurations will be a subset of primary collection of configurations, and will be specific a device only. As it is the subset of primary collection, DO NOT create a different representation data fields than primary collection. Use same presentation fields as primary collection.

  1. <configurations size="2">
  2. <link rel="self" href="/devices/12345/configurations" />
  3. <configuration id="53324">
  4. <link rel="self" href="/devices/12345/configurations/53324" />
  5. <link rel="detail" href="/configurations/53324" />
  6. </configuration>
  7. <configuration id="333443">
  8. <link rel="self" href="/devices/12345/configurations/333443" />
  9. <link rel="detail" href="/configurations/333443" />
  10. </configuration>
  11. </configurations>

Notice that this subresource collection has two links. One for its direct representation inside sub-collection i.e. /devices/12345/configurations/333443 and other pointing to its location in primary collection i.e. /configurations/333443.

Having two links is important as you can provide access to a device specific configuration in more unique manner, and you will have ability to mask some fields (if design require it) which shall not be visible in a secondary collection.

Single Configuration Resource Under Single Device

This representation should have either exactly similar representation as of Configuration representation from primary collection; OR you may mask few fields.

This subresource representation will also have an additional link to its primary presentation.

  1. <configuration id="11223344">
  2. <link rel="self" href="/devices/12345/configurations/11223344" />
  3. <link rel="detail" href="/configurations/11223344" />
  4. <content><![CDATA[...]]></content>
  5. <status>active</status>
  6. <link rel="raw configuration content" href="/configurations/11223344/raw" />
  7. </configuration>

Now, before moving forward to next section, let’s note down few observations so you don’t miss them.

Assign HTTP Methods

So our resource URIs and their representation are fixed now. Let’s decide the possible operations in application and map these operations on resource URIs. A user of network application can perform browse, create, update or delete operations. So let’s map them.

Browse all devices or configurations [Primary Collection]

  1. HTTP GET /devices
  2. HTTP GET /configurations

If the collection size is large, you can apply paging and filtering as well. e.g. Below requests will fetch first 20 records from collection.

  1. HTTP GET /devices?startIndex=0&size=20
  2. HTTP GET /configurations?startIndex=0&size=20

Browse all devices or configurations [Secondary Collection]

  1. HTTP GET /devices/{id}/configurations

It will be mostly a small size collection – so no need to enable filtering or soring here.

Browse single device or configuration [Primary Collection]
To get the complete detail of a device or configuration, use GET operation on singular resource URIs.

  1. HTTP GET /devices/{id}
  2. HTTP GET /configurations/{id}

Browse single device or configuration [Secondary Collection]

  1. HTTP GET /devices/{id}/configurations/{id}

Subresource representation will be either same as or subset of primary presentation.

Create a device or configuration

Create is not idempotent operation, and in HTTP protocol – POST is also not idempotent. So use POST.

  1. HTTP POST /devices
  2. HTTP POST /configurations

Please note that request payload will not contain any id attribute, as server is responsible for deciding it. Response of create request will look like this:

  1. HTTP/1.1 201 Created
  2. Content-Type: application/xml
  3. Location: http://example.com/network-app/configurations/678678
  4. <configuration id="678678">
  5. <link rel="self" href="/configurations/678678" />
  6. <content><![CDATA[...]]></content>
  7. <status>active</status>
  8. <link rel="raw configuration content" href="/configurations/678678/raw" />
  9. </configuration>

Update a device or configuration

Update operation is an idempotent operation and HTTP PUT is also is idempotent method. So we can use PUT method for update operations.

  1. HTTP PUT /devices/{id}
  2. HTTP PUT /configurations/{id}
  3. PUT response may look like this.
  4. HTTP/1.1 200 OK
  5. Content-Type: application/xml
  6. <configuration id="678678">
  7. <link rel="self" href="/configurations/678678" />
  8. <content><![CDATA[. updated content here .]]></content>
  9. <status>active</status>
  10. <link rel="raw configuration content" href="/configurations/678678/raw" />
  11. </configuration>

Remove a device or configuration

Removing is always a DELETE operation.

  1. HTTP DELETE /devices/{id}
  2. HTTP DELETE /configurations/{id}

A successful response SHOULD be 202 (Accepted) if resource has been queues for deletion (async operation), or 200 (OK) / 204 (No Content) if resource has been deleted permanently (sync operation).

In case of async operation, application shall return a task id which can be tracked for success/failure status.

Please note that you should put enough analysis in deciding the behavior when a subresource is deleted from system. Normally, you may want to SOFT DELETE a resource in these requests – in other words, set their status INACTIVE. By following this approach, you will not need to find and remove its references from other places as well.

Applying or Removing a configuration from a device

In real application, you will need to apply the configuration on device – OR you may want to remove the configuration from device (not from primary collection). You shall use PUT and DELETE methods in this case, because of its idempotent nature.

  1. //Apply Configuration on a device
  2. HTTP PUT /devices/{id}/configurations
  3. //Remove Configuration on a device
  4. HTTP DELETE /devices/{id}/configurations/{id}

More Actions

So far we have designed only object model, URIs and then decided HTTP methods or operations on them. You need to work on other aspects of the application as well:

1) Logging
2) Security
3) Discovery etc.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注