go语言学习-博客网站API设计

模仿 Github,设计一个博客网站的 API

功能介绍

需要设计一个博客网站,而首先需要明确博客网站所需的功能。博客网站要管理的主要是用户与博客,所以需要设计的API也围绕这两个来设计。除此之外,还有一些认证功能需要实现。

用户:

  1. Get a single user
  2. Get the authenticated user
  3. Update the authenticated user
  4. Get contextual information about a user
  5. Get all users

博客:

  1. List your blogs
  2. List user blogs
  3. List all public blogs
  4. Publish a blog
  5. Get a blog
  6. Update a blog
  7. Delete a blog
  8. List all comments for a blog
  9. Publish a comment for a blog

用户认证:

  1. Regesiter
  2. Login

具体API说明

首先明确一点,如果请求URL不包含:username参数,则响应将针对已登录用户(并且您必须随请求传递身份验证信息)。

用户

用户方面参考github的API,基本一致,在这里就不详细描述了,具体请看https://developer.github.com/v3/users/

博客
List your blogs

GET /user/blogs

参数:

名字类型描述
typestring不同的分类类型
visibilitystring文章的可见度,可以是all、public和private
sortstring以什么进行排序,可以是创建时间、更新时间等

响应:

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

参数:

名字类型描述
typestring不同的分类类型
sortstring以什么进行排序,可以是创建时间、更新时间等

响应:

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

参数:

名字类型描述
contentstring博客内容
namestring博客名字
descriptionstring说明
privateboolean可见度

响应:

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

参数:

名字类型描述
idstring博客id
titlestring博客标题
contentstring需要修改的内容

响应:

Status: 204 No Content
Status: 403 Forbidden
{
  "message": " 你没有权限删除",
}
Delete a blog

DELETE /blogs/:owner/:blog

参数:

名字类型描述
idstring博客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

参数:

名字类型描述
contentstring评论内容

响应:

Status: 200 OK
------------------------------------------------------------------------
{
    "id": "0",
    "articleId": "1744"
    "content": "*****",
    "createdAt": "2019-11-19T00:00:00Z"
}
用户认证
Regesiter

POST /users
参数:

名字类型描述
usernamestring用户名
passwordstring密码
emailstring邮箱地址

响应:

Status: 200 OK
------------------------------------------------------------------------
{
   “isRegesiter” : "true"
  "id": 1
  "user_name": " ",
  "created_at": "2019-11-20T00:00:00Z"
}
Login

POST /login
参数:

名字类型描述
usernamestring用户名
passwordstring密码

响应:

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的设计,当然由于时间关系,很多细节都没有很好的说明,所以说就设计一个简单的博客网站,工作量还是挺多的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值