From 69123d53a9336b029d7fd6f9be62440ae749e845 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sat, 22 Jul 2017 17:21:14 +0200 Subject: [PATCH] Updated APIdoc + New API endpoint for user profile Now you can access a user __public__ profile by querying the api endpoint /profile?id=USERID --- apidoc.json | 2 +- apidoc/api_data.js | 105 +++++++++++++++++++++++++++++++++++--- apidoc/api_data.json | 105 +++++++++++++++++++++++++++++++++++--- apidoc/api_project.js | 4 +- apidoc/api_project.json | 4 +- controllers/api/api.go | 67 ++++++++++++++++++++---- controllers/api/router.go | 1 + models/user.go | 2 +- 8 files changed, 261 insertions(+), 29 deletions(-) diff --git a/apidoc.json b/apidoc.json index 0276aa94..d2e33dfe 100644 --- a/apidoc.json +++ b/apidoc.json @@ -1,6 +1,6 @@ { "name": "nyaapantsu", - "version": "1.1.0", + "version": "1.1.1", "description": "How to use the Nyaa Pantsu API", "title": "Nyaa Pantsu API documentation", "url": "https://nyaa.pantsu.cat/api", diff --git a/apidoc/api_data.js b/apidoc/api_data.js index 29fd1192..abacba03 100644 --- a/apidoc/api_data.js +++ b/apidoc/api_data.js @@ -3,7 +3,7 @@ define({ "api": [ "type": "get", "url": "/search/", "title": "Search Torrents", - "version": "1.1.0", + "version": "1.1.1", "name": "FindTorrents", "group": "Torrents", "parameter": { @@ -353,7 +353,7 @@ define({ "api": [ "type": "get", "url": "/view/:id", "title": "Request Torrent information", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrent", "group": "Torrents", "parameter": { @@ -570,7 +570,7 @@ define({ "api": [ "type": "get", "url": "/head/:id", "title": "Request Torrent Head", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrentHead", "group": "Torrents", "parameter": { @@ -622,7 +622,7 @@ define({ "api": [ "type": "get", "url": "/", "title": "Request Torrents index", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrents", "group": "Torrents", "parameter": { @@ -699,7 +699,7 @@ define({ "api": [ "type": "post", "url": "/update/", "title": "Update a Torrent", - "version": "1.1.0", + "version": "1.1.1", "name": "UpdateTorrent", "group": "Torrents", "parameter": { @@ -853,7 +853,7 @@ define({ "api": [ "type": "post", "url": "/upload", "title": "Upload a Torrent", - "version": "1.1.0", + "version": "1.1.1", "name": "UploadTorrent", "group": "Torrents", "parameter": { @@ -1014,7 +1014,7 @@ define({ "api": [ "type": "post", "url": "/login/", "title": "Login a user", - "version": "1.1.0", + "version": "1.1.1", "name": "Login", "group": "Users", "parameter": { @@ -1107,5 +1107,96 @@ define({ "api": [ } ] } + }, + { + "type": "post", + "url": "/profile/", + "title": "Get a user profile", + "version": "1.1.1", + "name": "Profile", + "group": "Users", + "parameter": { + "fields": { + "Parameter": [ + { + "group": "Parameter", + "type": "Number", + "optional": false, + "field": "id", + "description": "

User ID.

" + } + ] + } + }, + "success": { + "fields": { + "Success 200": [ + { + "group": "Success 200", + "type": "Boolean", + "optional": false, + "field": "ok", + "description": "

The request is done without failing

" + }, + { + "group": "Success 200", + "type": "String[]", + "optional": false, + "field": "infos", + "description": "

Messages information relative to the request

" + }, + { + "group": "Success 200", + "type": "Object", + "optional": false, + "field": "data", + "description": "

The user object

" + } + ] + }, + "examples": [ + { + "title": "Success-Response:", + "content": " HTTP/1.1 200 OK\n\t\t{\n\t\t\tdata:\n \t\t[{\n\t\t\t\t\tuser_id:1,\n\t\t\t\t\tusername:\"username\",\n\t\t\t\t\tstatus:1,\n\t\t\t\t\tmd5:\"\",\n\t\t\t\t\tcreated_at:\"date\",\n\t\t\t\t\tliking_count:0,\n\t\t\t\t\tliked_count:0\n\t\t\t\t}],\n\t\t\tinfos: [\"Logged\", ... ],\n\t\t\tok:true\n\t\t}", + "type": "json" + } + ] + }, + "filename": "controllers/api/api.go", + "groupTitle": "Users", + "error": { + "fields": { + "Error 4xx": [ + { + "group": "Error 4xx", + "type": "Boolean", + "optional": false, + "field": "ok", + "description": "

The request couldn't be done due to some errors.

" + }, + { + "group": "Error 4xx", + "type": "String[]", + "optional": false, + "field": "errors", + "description": "

List of errors messages.

" + }, + { + "group": "Error 4xx", + "type": "Object[]", + "optional": false, + "field": "all_errors", + "description": "

List of errors object messages for each wrong field

" + } + ] + }, + "examples": [ + { + "title": "Error-Response:", + "content": " HTTP/1.1 200 OK\n {\n \"ok\": false,\n \"errors\": [ ... ]\n \"all_errors\": {\n\t\t \t\"username\": [ ... ],\n }\n }", + "type": "json" + } + ] + } } ] }); diff --git a/apidoc/api_data.json b/apidoc/api_data.json index 57f88d46..421d9e49 100644 --- a/apidoc/api_data.json +++ b/apidoc/api_data.json @@ -3,7 +3,7 @@ "type": "get", "url": "/search/", "title": "Search Torrents", - "version": "1.1.0", + "version": "1.1.1", "name": "FindTorrents", "group": "Torrents", "parameter": { @@ -353,7 +353,7 @@ "type": "get", "url": "/view/:id", "title": "Request Torrent information", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrent", "group": "Torrents", "parameter": { @@ -570,7 +570,7 @@ "type": "get", "url": "/head/:id", "title": "Request Torrent Head", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrentHead", "group": "Torrents", "parameter": { @@ -622,7 +622,7 @@ "type": "get", "url": "/", "title": "Request Torrents index", - "version": "1.1.0", + "version": "1.1.1", "name": "GetTorrents", "group": "Torrents", "parameter": { @@ -699,7 +699,7 @@ "type": "post", "url": "/update/", "title": "Update a Torrent", - "version": "1.1.0", + "version": "1.1.1", "name": "UpdateTorrent", "group": "Torrents", "parameter": { @@ -853,7 +853,7 @@ "type": "post", "url": "/upload", "title": "Upload a Torrent", - "version": "1.1.0", + "version": "1.1.1", "name": "UploadTorrent", "group": "Torrents", "parameter": { @@ -1014,7 +1014,7 @@ "type": "post", "url": "/login/", "title": "Login a user", - "version": "1.1.0", + "version": "1.1.1", "name": "Login", "group": "Users", "parameter": { @@ -1107,5 +1107,96 @@ } ] } + }, + { + "type": "post", + "url": "/profile/", + "title": "Get a user profile", + "version": "1.1.1", + "name": "Profile", + "group": "Users", + "parameter": { + "fields": { + "Parameter": [ + { + "group": "Parameter", + "type": "Number", + "optional": false, + "field": "id", + "description": "

User ID.

" + } + ] + } + }, + "success": { + "fields": { + "Success 200": [ + { + "group": "Success 200", + "type": "Boolean", + "optional": false, + "field": "ok", + "description": "

The request is done without failing

" + }, + { + "group": "Success 200", + "type": "String[]", + "optional": false, + "field": "infos", + "description": "

Messages information relative to the request

" + }, + { + "group": "Success 200", + "type": "Object", + "optional": false, + "field": "data", + "description": "

The user object

" + } + ] + }, + "examples": [ + { + "title": "Success-Response:", + "content": " HTTP/1.1 200 OK\n\t\t{\n\t\t\tdata:\n \t\t[{\n\t\t\t\t\tuser_id:1,\n\t\t\t\t\tusername:\"username\",\n\t\t\t\t\tstatus:1,\n\t\t\t\t\tmd5:\"\",\n\t\t\t\t\tcreated_at:\"date\",\n\t\t\t\t\tliking_count:0,\n\t\t\t\t\tliked_count:0\n\t\t\t\t}],\n\t\t\tinfos: [\"Logged\", ... ],\n\t\t\tok:true\n\t\t}", + "type": "json" + } + ] + }, + "filename": "controllers/api/api.go", + "groupTitle": "Users", + "error": { + "fields": { + "Error 4xx": [ + { + "group": "Error 4xx", + "type": "Boolean", + "optional": false, + "field": "ok", + "description": "

The request couldn't be done due to some errors.

" + }, + { + "group": "Error 4xx", + "type": "String[]", + "optional": false, + "field": "errors", + "description": "

List of errors messages.

" + }, + { + "group": "Error 4xx", + "type": "Object[]", + "optional": false, + "field": "all_errors", + "description": "

List of errors object messages for each wrong field

" + } + ] + }, + "examples": [ + { + "title": "Error-Response:", + "content": " HTTP/1.1 200 OK\n {\n \"ok\": false,\n \"errors\": [ ... ]\n \"all_errors\": {\n\t\t \t\"username\": [ ... ],\n }\n }", + "type": "json" + } + ] + } } ] diff --git a/apidoc/api_project.js b/apidoc/api_project.js index cad61d5a..cf3b174a 100644 --- a/apidoc/api_project.js +++ b/apidoc/api_project.js @@ -1,6 +1,6 @@ define({ "name": "nyaapantsu", - "version": "1.1.0", + "version": "1.1.1", "description": "How to use the Nyaa Pantsu API", "title": "Nyaa Pantsu API documentation", "url": "https://nyaa.pantsu.cat/api", @@ -26,7 +26,7 @@ define({ "apidoc": "0.3.0", "generator": { "name": "apidoc", - "time": "2017-07-20T21:04:54.468Z", + "time": "2017-07-22T15:20:08.679Z", "url": "http://apidocjs.com", "version": "0.17.6" } diff --git a/apidoc/api_project.json b/apidoc/api_project.json index 6cc79c91..24409c9b 100644 --- a/apidoc/api_project.json +++ b/apidoc/api_project.json @@ -1,6 +1,6 @@ { "name": "nyaapantsu", - "version": "1.1.0", + "version": "1.1.1", "description": "How to use the Nyaa Pantsu API", "title": "Nyaa Pantsu API documentation", "url": "https://nyaa.pantsu.cat/api", @@ -26,7 +26,7 @@ "apidoc": "0.3.0", "generator": { "name": "apidoc", - "time": "2017-07-20T21:04:54.468Z", + "time": "2017-07-22T15:20:08.679Z", "url": "http://apidocjs.com", "version": "0.17.6" } diff --git a/controllers/api/api.go b/controllers/api/api.go index 41ab5455..860766b1 100644 --- a/controllers/api/api.go +++ b/controllers/api/api.go @@ -27,7 +27,7 @@ import ( /** * @apiDefine NotFoundError - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiError {String[]} errors List of errors messages with a 404 error message in it. * * @apiErrorExample Error-Response: @@ -38,7 +38,7 @@ import ( */ /** * @apiDefine RequestError - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiError {Boolean} ok The request couldn't be done due to some errors. * @apiError {String[]} errors List of errors messages. * @apiError {Object[]} all_errors List of errors object messages for each wrong field @@ -56,7 +56,7 @@ import ( /** * @api {get} / Request Torrents index - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName GetTorrents * @apiGroup Torrents * @@ -89,7 +89,7 @@ func APIHandler(c *gin.Context) { /** * @api {get} /view/:id Request Torrent information - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName GetTorrent * @apiGroup Torrents * @@ -179,7 +179,7 @@ func APIViewHandler(c *gin.Context) { /** * @api {get} /head/:id Request Torrent Head - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName GetTorrentHead * @apiGroup Torrents * @@ -211,7 +211,7 @@ func APIViewHeadHandler(c *gin.Context) { /** * @api {post} /upload Upload a Torrent - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName UploadTorrent * @apiGroup Torrents * @@ -298,7 +298,7 @@ func APIUploadHandler(c *gin.Context) { /** * @api {post} /update/ Update a Torrent - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName UpdateTorrent * @apiGroup Torrents * @@ -368,7 +368,7 @@ func APIUpdateHandler(c *gin.Context) { /** * @api {get} /search/ Search Torrents - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName FindTorrents * @apiGroup Torrents * @@ -453,7 +453,7 @@ func APISearchHandler(c *gin.Context) { /** * @api {post} /login/ Login a user - * @apiVersion 1.1.0 + * @apiVersion 1.1.1 * @apiName Login * @apiGroup Users * @@ -510,6 +510,55 @@ func APILoginHandler(c *gin.Context) { apiResponseHandler(c) } +/** + * @api {post} /profile/ Get a user profile + * @apiVersion 1.1.1 + * @apiName Profile + * @apiGroup Users + * + * @apiParam {Number} id User ID. + * + * @apiSuccess {Boolean} ok The request is done without failing + * @apiSuccess {String[]} infos Messages information relative to the request + * @apiSuccess {Object} data The user object + * + * @apiSuccessExample Success-Response: + * HTTP/1.1 200 OK + * { + * data: + * [{ + * user_id:1, + * username:"username", + * status:1, + * md5:"", + * created_at:"date", + * liking_count:0, + * liked_count:0 + * }], + * infos: ["Logged", ... ], + * ok:true + * } + * + * @apiUse RequestError + */ +// APIProfileHandler : Get a public profile with API +func APIProfileHandler(c *gin.Context) { + c.Header("Content-Type", "application/json") + messages := msg.GetMessages(c) + id, err := strconv.ParseUint(c.Query("id"), 10, 32) + if err != nil { + id = 0 + } + user, _, errorUser := users.FindByID(uint(id)) + if errorUser == nil { + user.APIToken = "" // We erase apitoken from public profile + apiResponseHandler(c, user.ToJSON()) + return + } + messages.Error(errorUser) + apiResponseHandler(c) +} + // APIRefreshTokenHandler : Refresh Token with API func APIRefreshTokenHandler(c *gin.Context) { c.Header("Content-Type", "application/json") diff --git a/controllers/api/router.go b/controllers/api/router.go index a26e7fa5..fdee42ca 100644 --- a/controllers/api/router.go +++ b/controllers/api/router.go @@ -11,6 +11,7 @@ func init() { api.HEAD("/view/:id", APIViewHeadHandler) api.POST("/upload", APIUploadHandler) api.POST("/login", APILoginHandler) + api.GET("/profile", APIProfileHandler) api.GET("/token/check", APICheckTokenHandler) api.GET("/token/refresh", APIRefreshTokenHandler) api.Any("/search", APISearchHandler) diff --git a/models/user.go b/models/user.go index 4bf3e249..47c7726b 100644 --- a/models/user.go +++ b/models/user.go @@ -63,7 +63,7 @@ type UserJSON struct { ID uint `json:"user_id"` Username string `json:"username"` Status int `json:"status"` - APIToken string `json:"token"` + APIToken string `json:"token,omitempty"` MD5 string `json:"md5"` CreatedAt string `json:"created_at"` LikingCount int `json:"liking_count"`