作业内容
模仿github API,用markdown编写设计一个博客网站的部分REST API。
参考链接:GitHub API
Schema
- 所有的API访问都是通过HTTPS进行的,并通过
https://api.myblog.com
进行访问。所有数据都以JSON的形式发送和接收,空白字段被包含为null,而不是被忽略。 - 所有时间戳返回ISO 8601格式:
YYYY-MM-DDTHH:MM:SSZ
- 响应码为2XX表示成功,4XX或5XX表示失败
API
获得所有的REST API说明
curl https://api.myblog.com
Status:200 OK
-----------------------------------------------------------------------
{
"current_user_url": "https://api.myblog.com/user",
"current_user_articles_url": "https://api.myblog.com/user/articles/id",
"content_search_url": "https://api.myblog.com/search/content?q={query}",
"user_search_url": "https://api.myblog.com/search/user?q={query}",
"followers_url": "https://api.myblog.com/user/followers",
"following_url": "https://api.myblog.com/following={target}"
}
登录账户
curl -u username -p password https://api.myblog.com
成功返回:
{
"code": "200",
"msg": "SUCCESS"
}
失败返回:
{
"code": "404",
"msg": "Not Found"
}
查看指定用户的主页
GET /:username
给定用户名,用户存在返回用户主页内容,失败返回404
curl https://api.myblog.com/username
Status:200 OK
-----------------------------------------------------------------------
{
"user_name": "username",
"user_id": 123456
"followers_count": 20,
"following_count": 20,
"like_count": 20,
"articles_count": 10,
"total_page_view": 4613,
"items": [
{
"articleID": 123456789,
"tilte": "articleTitle",
"owner": "username",
"article_url": "https://api.myblog.com/user/articles/123456"
"public": true,
"introduction": "摘要内容",
"content":"文章内容",
"created_time": "2019-11-20T12:31:50Z",
"reading_count": 4869,
"comment_count": 4869,
},
...
]
}
Status:404 Not Found
-----------------------------------------------------------------------
{
"code": "404",
"msg": "Not Found"
}
查看某一用户的某一篇文章
GET /:username/articles/:id
给定用户名和文章的id,查询成功返回文章内容,失败返回404
curl https://api.myblog.com/username/articles/id
Status:200 OK
-----------------------------------------------------------------------
{
"user_name": "username",
"followers_count": 20,
"following_count": 20,
"like_count": 20,
"articles_count": 10,
"total_page_view": 4613,
"articleID": 123456789,
"title": "articleTitle",
"article_url": "https://api.myblog.com/user/articles/id"
"public": true,
"created_time": "2019-11-20T12:31:50Z",
"reading_count": 123,
"content":"文章内容",
"comment": [
{
"comment_name": "comment user name",
"comment_content": "content",
"comment_time": : "2019-11-21T12:31:50Z"
},
...
]
}
Status:404 NOT FOUND
-----------------------------------------------------------------------
{
"code": "404",
"msg": "Not Found"
}
搜索包含关键字的文章
POST /search/content?q={query}
query为需要查询文字包含的字段
curl https://api.myblog.com/search/content?q=YouWantToSearch
Status:200 OK
-----------------------------------------------------------------------
{
"articles": [
{
"articleID": 123456789,
"tilte": "articleTitle",
"owner": "username",
"article_url": "https://api.myblog.com/user/articles/123456"
"public": true,
"introduction": "摘要内容",
"content":"文章内容....YouWantToSearch...",
"created_time": "2019-11-20T12:31:50Z",
"reading_count": 4869,
"comment_count": 4869,
},
...
]
}
搜索包含关键字的用户
POST /search/user?q={query}
返回含有用户名中含有query字段的所有用户
curl https://api.myblog.com/search/user?q=YouWantToSearch
Status:200 OK
-----------------------------------------------------------------------
{
"users": [
{
"user_name": "username",
"user_id": 123456,
"followers_count": 20,
"following_count": 20,
"like_count": 20,
"articles_count": 10,
"total_page_view": 4613,
},
...
]
}
查看某一用户的粉丝
GET /:username/followers
curl https://api.myblog.com/username/followers
Status:200 OK
-----------------------------------------------------------------------
{
"followers_count": 4869
"users": [
{
"user_name": "username",
"user_id": 123456,
},
...
]
}
Status:404 NOT FOUND
-----------------------------------------------------------------------
{
"code": "404",
"msg": "Not Found"
}
关注某一个用户
POST /following={target}
curl https://api.myblog.com/following={target}
用户未登录:
{
"message": "Requires authentication",
"documentation_url": "https://developer.myblog.com"
}
指定用户不存在:
{
"message": "Can not find target",
"documentation_url": "https://developer.myblog.com"
}
关注成功:
{
"message": "Follow successfully"
}