diff options
author | Unknwon <u@gogs.io> | 2016-07-04 17:27:06 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-07-04 17:27:06 +0800 |
commit | 4b25bdfbc45479d64e3ffb04abbd08672ccc47fb (patch) | |
tree | bbede594875a146bf4c34ce2760202cffe6aeb33 /routers/api | |
parent | 528682a294834611a95b2cf6c80efdccdf6fd791 (diff) | |
download | gitea-4b25bdfbc45479d64e3ffb04abbd08672ccc47fb.tar.gz gitea-4b25bdfbc45479d64e3ffb04abbd08672ccc47fb.zip |
#3058 #3059 support correct page size and link header
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/convert/utils.go | 19 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 12 |
2 files changed, 23 insertions, 8 deletions
diff --git a/routers/api/v1/convert/utils.go b/routers/api/v1/convert/utils.go new file mode 100644 index 0000000000..f34fb3d2b6 --- /dev/null +++ b/routers/api/v1/convert/utils.go @@ -0,0 +1,19 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "github.com/gogits/gogs/modules/setting" +) + +// ToCorrectPageSize makes sure page size is in allowed range. +func ToCorrectPageSize(size int) int { + if size <= 0 { + size = 10 + } else if size > setting.API.MaxResponseItems { + size = setting.API.MaxResponseItems + } + return size +} diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 90a323c257..222098ac66 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -7,8 +7,6 @@ package repo import ( "path" - "github.com/Unknwon/com" - api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" @@ -23,11 +21,8 @@ import ( func Search(ctx *context.APIContext) { opts := &models.SearchRepoOptions{ Keyword: path.Base(ctx.Query("q")), - OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(), - PageSize: com.StrTo(ctx.Query("limit")).MustInt(), - } - if opts.PageSize == 0 { - opts.PageSize = 10 + OwnerID: ctx.QueryInt64("uid"), + PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), } // Check visibility. @@ -50,7 +45,7 @@ func Search(ctx *context.APIContext) { } } - repos, _, err := models.SearchRepositoryByName(opts) + repos, count, err := models.SearchRepositoryByName(opts) if err != nil { ctx.JSON(500, map[string]interface{}{ "ok": false, @@ -74,6 +69,7 @@ func Search(ctx *context.APIContext) { } } + ctx.SetLinkHeader(int(count), setting.API.MaxResponseItems) ctx.JSON(200, map[string]interface{}{ "ok": true, "data": results, |