You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 2.7 kB

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/Unknwon/paginater"
  13. macaron "gopkg.in/macaron.v1"
  14. )
  15. type APIContext struct {
  16. *Context
  17. Org *APIOrganization
  18. }
  19. // Error responses error message to client with given message.
  20. // If status is 500, also it prints error to log.
  21. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  22. var message string
  23. if err, ok := obj.(error); ok {
  24. message = err.Error()
  25. } else {
  26. message = obj.(string)
  27. }
  28. if status == 500 {
  29. log.Error(4, "%s: %s", title, message)
  30. }
  31. ctx.JSON(status, map[string]string{
  32. "message": message,
  33. "url": base.DocURL,
  34. })
  35. }
  36. // SetLinkHeader sets pagination link header by given totol number and page size.
  37. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  38. page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
  39. links := make([]string, 0, 4)
  40. if page.HasNext() {
  41. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Next()))
  42. }
  43. if !page.IsLast() {
  44. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.TotalPages()))
  45. }
  46. if !page.IsFirst() {
  47. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppUrl, ctx.Req.URL.Path[1:]))
  48. }
  49. if page.HasPrevious() {
  50. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Previous()))
  51. }
  52. if len(links) > 0 {
  53. ctx.Header().Set("Link", strings.Join(links, ","))
  54. }
  55. }
  56. func APIContexter() macaron.Handler {
  57. return func(c *Context) {
  58. ctx := &APIContext{
  59. Context: c,
  60. }
  61. c.Map(ctx)
  62. }
  63. }
  64. // ExtractOwnerAndRepo returns a handler that populates the `Repo.Owner` and
  65. // `Repo.Repository` fields of an APIContext
  66. func ExtractOwnerAndRepo() macaron.Handler {
  67. return func(ctx *APIContext) {
  68. owner, err := models.GetUserByName(ctx.Params(":username"))
  69. if err != nil {
  70. if models.IsErrUserNotExist(err) {
  71. ctx.Error(422, "", err)
  72. } else {
  73. ctx.Error(500, "GetUserByName", err)
  74. }
  75. return
  76. }
  77. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  78. if err != nil {
  79. if models.IsErrRepoNotExist(err) {
  80. ctx.Status(404)
  81. } else {
  82. ctx.Error(500, "GetRepositoryByName", err)
  83. }
  84. return
  85. }
  86. ctx.Repo.Owner = owner
  87. ctx.Data["Owner"] = owner
  88. ctx.Repo.Repository = repo
  89. ctx.Data["Repository"] = repo
  90. }
  91. }