api.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package context
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/setting"
  10. "net/http"
  11. "strings"
  12. "gitlab.com/yoginth/paginater"
  13. log "gopkg.in/clog.v1"
  14. "gopkg.in/macaron.v1"
  15. )
  16. type APIContext struct {
  17. *Context // TODO: Reduce to only needed fields instead of full shadow
  18. // Base URL for the version of API endpoints, e.g. https://gitote.in/api/v1
  19. BaseURL string
  20. Org *APIOrganization
  21. }
  22. // FIXME: move to gitlab.com/gitote/gitote-client
  23. const DOC_URL = "https://gitlab.com/gitote/gitote-client/wiki"
  24. // Error responses error message to client with given message.
  25. // If status is 500, also it prints error to log.
  26. func (c *APIContext) Error(status int, title string, obj interface{}) {
  27. var message string
  28. if err, ok := obj.(error); ok {
  29. message = err.Error()
  30. } else {
  31. message = obj.(string)
  32. }
  33. if status == http.StatusInternalServerError {
  34. log.Error(3, "%s: %s", title, message)
  35. }
  36. c.JSON(status, map[string]string{
  37. "message": message,
  38. "url": DOC_URL,
  39. })
  40. }
  41. // NotFound renders the 404 response.
  42. func (c *APIContext) NotFound() {
  43. c.Status(http.StatusNotFound)
  44. }
  45. // ServerError renders the 500 response.
  46. func (c *APIContext) ServerError(title string, err error) {
  47. c.Error(http.StatusInternalServerError, title, err)
  48. }
  49. // NotFoundOrServerError use error check function to determine if the error
  50. // is about not found. It responses with 404 status code for not found error,
  51. // or error context description for logging purpose of 500 server error.
  52. func (c *APIContext) NotFoundOrServerError(title string, errck func(error) bool, err error) {
  53. if errck(err) {
  54. c.NotFound()
  55. return
  56. }
  57. c.ServerError(title, err)
  58. }
  59. // SetLinkHeader sets pagination link header by given total number and page size.
  60. func (c *APIContext) SetLinkHeader(total, pageSize int) {
  61. page := paginater.New(total, pageSize, c.QueryInt("page"), 0)
  62. links := make([]string, 0, 4)
  63. if page.HasNext() {
  64. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, c.Req.URL.Path[1:], page.Next()))
  65. }
  66. if !page.IsLast() {
  67. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, c.Req.URL.Path[1:], page.TotalPages()))
  68. }
  69. if !page.IsFirst() {
  70. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, c.Req.URL.Path[1:]))
  71. }
  72. if page.HasPrevious() {
  73. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, c.Req.URL.Path[1:], page.Previous()))
  74. }
  75. if len(links) > 0 {
  76. c.Header().Set("Link", strings.Join(links, ","))
  77. }
  78. }
  79. func APIContexter() macaron.Handler {
  80. return func(ctx *Context) {
  81. c := &APIContext{
  82. Context: ctx,
  83. BaseURL: setting.AppURL + "api/v1",
  84. }
  85. ctx.Map(c)
  86. }
  87. }