http urls monitor.

response.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "github.com/go-openapi/jsonpointer"
  18. "github.com/go-openapi/swag"
  19. )
  20. // ResponseProps properties specific to a response
  21. type ResponseProps struct {
  22. Description string `json:"description,omitempty"`
  23. Schema *Schema `json:"schema,omitempty"`
  24. Headers map[string]Header `json:"headers,omitempty"`
  25. Examples map[string]interface{} `json:"examples,omitempty"`
  26. }
  27. // Response describes a single response from an API Operation.
  28. //
  29. // For more information: http://goo.gl/8us55a#responseObject
  30. type Response struct {
  31. Refable
  32. ResponseProps
  33. VendorExtensible
  34. }
  35. // JSONLookup look up a value by the json property name
  36. func (r Response) JSONLookup(token string) (interface{}, error) {
  37. if ex, ok := r.Extensions[token]; ok {
  38. return &ex, nil
  39. }
  40. if token == "$ref" {
  41. return &r.Ref, nil
  42. }
  43. ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
  44. return ptr, err
  45. }
  46. // UnmarshalJSON hydrates this items instance with the data from JSON
  47. func (r *Response) UnmarshalJSON(data []byte) error {
  48. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  49. return err
  50. }
  51. if err := json.Unmarshal(data, &r.Refable); err != nil {
  52. return err
  53. }
  54. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. // MarshalJSON converts this items object to JSON
  60. func (r Response) MarshalJSON() ([]byte, error) {
  61. b1, err := json.Marshal(r.ResponseProps)
  62. if err != nil {
  63. return nil, err
  64. }
  65. b2, err := json.Marshal(r.Refable)
  66. if err != nil {
  67. return nil, err
  68. }
  69. b3, err := json.Marshal(r.VendorExtensible)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return swag.ConcatJSON(b1, b2, b3), nil
  74. }
  75. // NewResponse creates a new response instance
  76. func NewResponse() *Response {
  77. return new(Response)
  78. }
  79. // ResponseRef creates a response as a json reference
  80. func ResponseRef(url string) *Response {
  81. resp := NewResponse()
  82. resp.Ref = MustCreateRef(url)
  83. return resp
  84. }
  85. // WithDescription sets the description on this response, allows for chaining
  86. func (r *Response) WithDescription(description string) *Response {
  87. r.Description = description
  88. return r
  89. }
  90. // WithSchema sets the schema on this response, allows for chaining.
  91. // Passing a nil argument removes the schema from this response
  92. func (r *Response) WithSchema(schema *Schema) *Response {
  93. r.Schema = schema
  94. return r
  95. }
  96. // AddHeader adds a header to this response
  97. func (r *Response) AddHeader(name string, header *Header) *Response {
  98. if header == nil {
  99. return r.RemoveHeader(name)
  100. }
  101. if r.Headers == nil {
  102. r.Headers = make(map[string]Header)
  103. }
  104. r.Headers[name] = *header
  105. return r
  106. }
  107. // RemoveHeader removes a header from this response
  108. func (r *Response) RemoveHeader(name string) *Response {
  109. delete(r.Headers, name)
  110. return r
  111. }
  112. // AddExample adds an example to this response
  113. func (r *Response) AddExample(mediaType string, example interface{}) *Response {
  114. if r.Examples == nil {
  115. r.Examples = make(map[string]interface{})
  116. }
  117. r.Examples[mediaType] = example
  118. return r
  119. }