@zhongjianxin
2018-11-26T02:54:39.000000Z
字数 7372
阅读 957
OOCL
A API Design Case:
https://www.ibm.com/developerworks/cn/webservices/0907_rest_soap/
https://www.upwork.com/hiring/development/soap-vs-rest-comparing-two-apis/
Resourced base vs ehavior base like OOP vs OPP
This API is designed to show how not to design a REST API.
GET /lists-posts
---
200 OK
{
"posts": [{
"id": 1,
"title": "Hello World",
"body": "My first blog post"
}]
}
POST /create-post
{
"post": {
"title": "Hello World",
"body": "My first blog post"
}
}
---
200 OK
{"status": "created", "id": 1}
GET /show-post?id=1
{
"post": {
"title": "Hello World",
"body": "My first blog post"
}
}
POST /update-post?id=1
{
"post": {
"title": "Hello World",
"body": "My first blog post"
}
}
---
200 OK
{"status": "updated"}
GET /delete-post?id=1
---
200 OK
{"status": "deleted"}
Simple RESTful API for managing blog posts.
The blog API v1 deals with the following resources.
GET /posts
---
200 OK
Content-Type: application/json
[{
"id": 1,
"title": "Hello World",
"body": "My first blog post"
}]
POST /posts
{
"title": "Hello World",
"body": "My first blog post"
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post"
}
GET /posts/1
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post"
}
PUT /posts/1
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits"
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits"
}
DELETE /posts/1
---
200 OK
Content-Type: application/json
{}
Simple RESTful API for managing blog posts.
Includes posts and tags.
The blog API v3 deals with the following resources.
GET /posts
---
200 OK
Content-Type: application/json
[{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"]
}]
POST /posts
{
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"]
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"]
}
GET /posts/1
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"]
}
PUT /posts/1
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"]
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"]
}
DELETE /posts/1
---
200 OK
Content-Type: application/json
{}
GET /tags
---
200 OK
[{
"name": "hello",
"posts": [
1
]
}]
GET /tags/hello
---
200 OK
{
"name": "hello",
"posts": [
1
]
}
How to get a tag and all its posts for rendering a tag page?
/tag/tagname/posts
/tag/tagname (always include posts in the tag representation)
/tag/tagname?expand=true
How to find all the posts that are tagged with both tag A and tag B?
/posts?tag=A&tag=B
How to add a new tag to an existing post?
PATCH /posts/1
{
"add": {
"tag": "new-tag"
},
"delete": {
"tag": "old-tag"
}
}
See JSONPatch for more ideas.
How about exposing the tag as a subresource?
PUT /posts/1/tags/new-tag
How to delete a tag from an existing post?
DELETE /posts/1/tags/old-tag
Simple RESTful API for managing blog posts.
Includes posts, tags and comments.
The blog API v3 deals with the following resources.
GET /posts
---
200 OK
Content-Type: application/json
[{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"],
"comments": [{
"id": 12,
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}]
}]
POST /posts
{
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"]
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"],
"comments": []
}
GET /posts/1
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"],
"comments": [{
"id": "12"
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}]
}
PUT /posts/1
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"]
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"],
"comments": [{
"id": 12,
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}]
}
DELETE /posts/1
---
200 OK
Content-Type: application/json
{}
GET /posts/1/comments
---
200 OK
[{
"id": 12,
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}]
POST /posts/1/comments
{
"author": "bahubali",
"comment": "mahishmathi",
}
---
200 OK
{
"id": 12,
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}
GET /posts/1/comments/12
---
200 OK
{
"id": 12,
"author": "bahubali",
"comment": "mahishmathi",
"timestamp": "2017-05-12 10:15:00"
}
PUT /posts/1/comments/12
{
"id": 12,
"author": "bahubali2",
"comment": "do you know why kattappa killed bahubali?",
"timestamp": "2017-05-12 10:15:00"
}
---
200 OK
{
"id": 12,
"author": "bahubali2",
"comment": "do you know why kattappa killed bahubali?",
"timestamp": "2017-05-12 10:15:00"
}
DELETE /posts/1/comments/12
---
200 OK
{}
Simple RESTful API for managing blog posts.
Includes posts, tags, comments and authors.
Every post will have exactly is one author. For simplicity it is assumed that it can't be changed later.
The blog API v4 deals with the following resources.
GET /posts
---
200 OK
Content-Type: application/json
[{
"id": 1,
"author": {
"id": 4
"name": "Anand"
},
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"],
"comments": []
}]
GET /posts/1
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post",
"tags": ["hello"],
"comments": [],
"author": {
"id": 4
"name": "Anand"
}
}
PUT /posts/1
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"]
}
---
200 OK
Content-Type: application/json
{
"id": 1,
"title": "Hello World",
"body": "My first blog post with some edits",
"tags": ["hello"],
"author": {
"id": 4
"name": "Anand"
}
}
DELETE /posts/1
---
200 OK
Content-Type: application/json
{}
Since an author is always required to create a post, it would be easier, if can constuct
GET /authors
---
200 OK
[{
"id": 1,
"name": "Anand"
}]
POST /authors
GET /authors/1
PUT /authors/1
DELETE /authors/1
GET /authors/1/posts
POST /authors/1/posts
{
"title": "Building RESTful APIs",
"body": "The workshop is today."
}
---
200 OK
{
"id": 3,
"title": "Building RESTful APIs",
"body": "The workshop is today",
"author": {
"id": 1,
"name": "Anand"
}
}