文章目录
模仿 Github,设计一个博客网站的 API
功能介绍
需要设计一个博客网站,而首先需要明确博客网站所需的功能。博客网站要管理的主要是用户与博客,所以需要设计的API也围绕这两个来设计。除此之外,还有一些认证功能需要实现。
用户:
- Get a single user
- Get the authenticated user
- Update the authenticated user
- Get contextual information about a user
- Get all users
博客:
- List your blogs
- List user blogs
- List all public blogs
- Publish a blog
- Get a blog
- Update a blog
- Delete a blog
- List all comments for a blog
- Publish a comment for a blog
用户认证:
- Regesiter
- Login
具体API说明
首先明确一点,如果请求URL不包含:username参数,则响应将针对已登录用户(并且您必须随请求传递身份验证信息)。
用户
用户方面参考github的API,基本一致,在这里就不详细描述了,具体请看https://developer.github.com/v3/users/。
博客
List your blogs
GET /user/blogs
参数:
名字 | 类型 | 描述 |
---|---|---|
type | string | 不同的分类类型 |
visibility | string | 文章的可见度,可以是all、public和private |
sort | string | 以什么进行排序,可以是创建时间、更新时间等 |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"owner": {
"login": "某用户",
"id": 1,
...
},
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
...
}
}
]
List user blogs
GET /users/:username/blogs
参数:
名字 | 类型 | 描述 |
---|---|---|
type | string | 不同的分类类型 |
sort | string | 以什么进行排序,可以是创建时间、更新时间等 |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"owner": {
"login": "某用户",
"id": 1,
...
},
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
...
}
}
]
List all public blogs
GET /blogs
参数:
无
响应:
Status: 200 OK
------------------------------------------------------------------------
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"owner": {
"login": "某用户",
"id": 1,
...
},
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
...
}
}
]
Publish a blog
POST /user/blogs
参数:
名字 | 类型 | 描述 |
---|---|---|
content | string | 博客内容 |
name | string | 博客名字 |
description | string | 说明 |
private | boolean | 可见度 |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
{
"isPublished":true ,
"article":{
"articleID": 10,
"name": "articleNames",
"owner": {
"name": "ownerName",
"id": 123,
"url": "owner blog url",
"type": "User"
},
"article_url": "article url"
"private": false,
"description": "...",
"reading number": 0,
"created_at": "2012-01-01T00:31:50Z",
"updated_at": "2013-01-01T00:31:50Z",
"words": 524,
"language": "English",
"content":"article contents...."
},
}
Get a blog
GET /blogs/:owner/:blog
参数:
无
响应:
Status: 200 OK
------------------------------------------------------------------------
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"owner": {
"login": "某用户",
"id": 1,
...
},
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
...
}
}
]
Update a blog
PATCH /blogs/:owner/:blog
参数:
名字 | 类型 | 描述 |
---|---|---|
id | string | 博客id |
title | string | 博客标题 |
content | string | 需要修改的内容 |
… | … | … |
响应:
Status: 204 No Content
Status: 403 Forbidden
{
"message": " 你没有权限删除",
}
Delete a blog
DELETE /blogs/:owner/:blog
参数:
名字 | 类型 | 描述 |
---|---|---|
id | string | 博客id |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
{
"isUpdated":true,
"articles":{
"articleID": 10,
"name": "articleNames",
"owner": {
"name": "ownerName",
"id": 123,
"url": "owner blog url",
"type": "User"
},
"article_url": "article url"
"private": false,
"description": "...",
"reading number": 10,
"created_at": "2012-01-01T00:31:50Z",
"updated_at": "2013-01-05T17:58:47Z",
"words": 524,
"language": "English",
"content":"article contents...."
},
}
List all comments for a blog
GET /blogs/:owner/:blog/comments
参数:
无
响应:
Status: 200 OK
------------------------------------------------------------------------
{
"id": 1,
"author": "user1",
"url": "xx",
items:[{
"contents":xxx,
"user": {
"id": 2,
"name":"user2"
"url": "xxx",
"type": "User",
"site_admin": false
},
....
]
"created_at": "2011-04-18T23:23:56Z",
"updated_at": "2011-04-18T23:23:56Z"
}
Publish a comment for a blog
POST /blogs/:owner/:blog/comments
参数:
名字 | 类型 | 描述 |
---|---|---|
content | string | 评论内容 |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
{
"id": "0",
"articleId": "1744"
"content": "*****",
"createdAt": "2019-11-19T00:00:00Z"
}
用户认证
Regesiter
POST /users
参数:
名字 | 类型 | 描述 |
---|---|---|
username | string | 用户名 |
password | string | 密码 |
string | 邮箱地址 | |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
{
“isRegesiter” : "true"
"id": 1
"user_name": " ",
"created_at": "2019-11-20T00:00:00Z"
}
Login
POST /login
参数:
名字 | 类型 | 描述 |
---|---|---|
username | string | 用户名 |
password | string | 密码 |
… | … | … |
响应:
Status: 200 OK
------------------------------------------------------------------------
{
{
“isLogin” : "true"
"username": "Zhangqzh"
"created_at": "2019-11-20T00:00:00Z"
"updated_at": "2019-11-20T01:00:00Z"
}
使用无效的凭据进行身份验证将返回401 Unauthorized:
curl -i https://api.exampel.com -u foo:bar
HTTP / 1.1 401未经授权的
{
“ message”:“错误的凭据”,
...
}
在短时间内检测到多个具有无效凭据的请求后,API会临时拒绝该用户的所有身份验证尝试(包括具有有效凭据的请求)403 Forbidden:
curl -i https://api.exampel.com -u valid_username:valid_password
HTTP / 1.1 403禁止
{
“ message”:“已超过最大登录尝试次数。请稍后再试。”,
...
}
总结
本次作业进行了博客网站API的设计,当然由于时间关系,很多细节都没有很好的说明,所以说就设计一个简单的博客网站,工作量还是挺多的。