http urls monitor.

responses.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "strconv"
  20. "github.com/go-openapi/swag"
  21. )
  22. // Responses is a container for the expected responses of an operation.
  23. // The container maps a HTTP response code to the expected response.
  24. // It is not expected from the documentation to necessarily cover all possible HTTP response codes,
  25. // since they may not be known in advance. However, it is expected from the documentation to cover
  26. // a successful operation response and any known errors.
  27. //
  28. // The `default` can be used a default response object for all HTTP codes that are not covered
  29. // individually by the specification.
  30. //
  31. // The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
  32. // for a successful operation call.
  33. //
  34. // For more information: http://goo.gl/8us55a#responsesObject
  35. type Responses struct {
  36. VendorExtensible
  37. ResponsesProps
  38. }
  39. // JSONLookup implements an interface to customize json pointer lookup
  40. func (r Responses) JSONLookup(token string) (interface{}, error) {
  41. if token == "default" {
  42. return r.Default, nil
  43. }
  44. if ex, ok := r.Extensions[token]; ok {
  45. return &ex, nil
  46. }
  47. if i, err := strconv.Atoi(token); err == nil {
  48. if scr, ok := r.StatusCodeResponses[i]; ok {
  49. return scr, nil
  50. }
  51. }
  52. return nil, fmt.Errorf("object has no field %q", token)
  53. }
  54. // UnmarshalJSON hydrates this items instance with the data from JSON
  55. func (r *Responses) UnmarshalJSON(data []byte) error {
  56. if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
  57. return err
  58. }
  59. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  60. return err
  61. }
  62. if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {
  63. r.ResponsesProps = ResponsesProps{}
  64. }
  65. return nil
  66. }
  67. // MarshalJSON converts this items object to JSON
  68. func (r Responses) MarshalJSON() ([]byte, error) {
  69. b1, err := json.Marshal(r.ResponsesProps)
  70. if err != nil {
  71. return nil, err
  72. }
  73. b2, err := json.Marshal(r.VendorExtensible)
  74. if err != nil {
  75. return nil, err
  76. }
  77. concated := swag.ConcatJSON(b1, b2)
  78. return concated, nil
  79. }
  80. // ResponsesProps describes all responses for an operation.
  81. // It tells what is the default response and maps all responses with a
  82. // HTTP status code.
  83. type ResponsesProps struct {
  84. Default *Response
  85. StatusCodeResponses map[int]Response
  86. }
  87. // MarshalJSON marshals responses as JSON
  88. func (r ResponsesProps) MarshalJSON() ([]byte, error) {
  89. toser := map[string]Response{}
  90. if r.Default != nil {
  91. toser["default"] = *r.Default
  92. }
  93. for k, v := range r.StatusCodeResponses {
  94. toser[strconv.Itoa(k)] = v
  95. }
  96. return json.Marshal(toser)
  97. }
  98. // UnmarshalJSON unmarshals responses from JSON
  99. func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
  100. var res map[string]Response
  101. if err := json.Unmarshal(data, &res); err != nil {
  102. return nil
  103. }
  104. if v, ok := res["default"]; ok {
  105. r.Default = &v
  106. delete(res, "default")
  107. }
  108. for k, v := range res {
  109. if nk, err := strconv.Atoi(k); err == nil {
  110. if r.StatusCodeResponses == nil {
  111. r.StatusCodeResponses = map[int]Response{}
  112. }
  113. r.StatusCodeResponses[nk] = v
  114. }
  115. }
  116. return nil
  117. }