http urls monitor.

operation.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. // OperationProps describes an operation
  21. type OperationProps struct {
  22. Description string `json:"description,omitempty"`
  23. Consumes []string `json:"consumes,omitempty"`
  24. Produces []string `json:"produces,omitempty"`
  25. Schemes []string `json:"schemes,omitempty"` // the scheme, when present must be from [http, https, ws, wss]
  26. Tags []string `json:"tags,omitempty"`
  27. Summary string `json:"summary,omitempty"`
  28. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  29. ID string `json:"operationId,omitempty"`
  30. Deprecated bool `json:"deprecated,omitempty"`
  31. Security []map[string][]string `json:"security,omitempty"` //Special case, see MarshalJSON function
  32. Parameters []Parameter `json:"parameters,omitempty"`
  33. Responses *Responses `json:"responses,omitempty"`
  34. }
  35. // MarshalJSON takes care of serializing operation properties to JSON
  36. //
  37. // We use a custom marhaller here to handle a special cases related to
  38. // the Security field. We need to preserve zero length slice
  39. // while omitting the field when the value is nil/unset.
  40. func (op OperationProps) MarshalJSON() ([]byte, error) {
  41. type Alias OperationProps
  42. if op.Security == nil {
  43. return json.Marshal(&struct {
  44. Security []map[string][]string `json:"security,omitempty"`
  45. *Alias
  46. }{
  47. Security: op.Security,
  48. Alias: (*Alias)(&op),
  49. })
  50. }
  51. return json.Marshal(&struct {
  52. Security []map[string][]string `json:"security"`
  53. *Alias
  54. }{
  55. Security: op.Security,
  56. Alias: (*Alias)(&op),
  57. })
  58. }
  59. // Operation describes a single API operation on a path.
  60. //
  61. // For more information: http://goo.gl/8us55a#operationObject
  62. type Operation struct {
  63. VendorExtensible
  64. OperationProps
  65. }
  66. // SuccessResponse gets a success response model
  67. func (o *Operation) SuccessResponse() (*Response, int, bool) {
  68. if o.Responses == nil {
  69. return nil, 0, false
  70. }
  71. for k, v := range o.Responses.StatusCodeResponses {
  72. if k/100 == 2 {
  73. return &v, k, true
  74. }
  75. }
  76. return o.Responses.Default, 0, false
  77. }
  78. // JSONLookup look up a value by the json property name
  79. func (o Operation) JSONLookup(token string) (interface{}, error) {
  80. if ex, ok := o.Extensions[token]; ok {
  81. return &ex, nil
  82. }
  83. r, _, err := jsonpointer.GetForToken(o.OperationProps, token)
  84. return r, err
  85. }
  86. // UnmarshalJSON hydrates this items instance with the data from JSON
  87. func (o *Operation) UnmarshalJSON(data []byte) error {
  88. if err := json.Unmarshal(data, &o.OperationProps); err != nil {
  89. return err
  90. }
  91. if err := json.Unmarshal(data, &o.VendorExtensible); err != nil {
  92. return err
  93. }
  94. return nil
  95. }
  96. // MarshalJSON converts this items object to JSON
  97. func (o Operation) MarshalJSON() ([]byte, error) {
  98. b1, err := json.Marshal(o.OperationProps)
  99. if err != nil {
  100. return nil, err
  101. }
  102. b2, err := json.Marshal(o.VendorExtensible)
  103. if err != nil {
  104. return nil, err
  105. }
  106. concated := swag.ConcatJSON(b1, b2)
  107. return concated, nil
  108. }
  109. // NewOperation creates a new operation instance.
  110. // It expects an ID as parameter but not passing an ID is also valid.
  111. func NewOperation(id string) *Operation {
  112. op := new(Operation)
  113. op.ID = id
  114. return op
  115. }
  116. // WithID sets the ID property on this operation, allows for chaining.
  117. func (o *Operation) WithID(id string) *Operation {
  118. o.ID = id
  119. return o
  120. }
  121. // WithDescription sets the description on this operation, allows for chaining
  122. func (o *Operation) WithDescription(description string) *Operation {
  123. o.Description = description
  124. return o
  125. }
  126. // WithSummary sets the summary on this operation, allows for chaining
  127. func (o *Operation) WithSummary(summary string) *Operation {
  128. o.Summary = summary
  129. return o
  130. }
  131. // WithExternalDocs sets/removes the external docs for/from this operation.
  132. // When you pass empty strings as params the external documents will be removed.
  133. // When you pass non-empty string as one value then those values will be used on the external docs object.
  134. // So when you pass a non-empty description, you should also pass the url and vice versa.
  135. func (o *Operation) WithExternalDocs(description, url string) *Operation {
  136. if description == "" && url == "" {
  137. o.ExternalDocs = nil
  138. return o
  139. }
  140. if o.ExternalDocs == nil {
  141. o.ExternalDocs = &ExternalDocumentation{}
  142. }
  143. o.ExternalDocs.Description = description
  144. o.ExternalDocs.URL = url
  145. return o
  146. }
  147. // Deprecate marks the operation as deprecated
  148. func (o *Operation) Deprecate() *Operation {
  149. o.Deprecated = true
  150. return o
  151. }
  152. // Undeprecate marks the operation as not deprected
  153. func (o *Operation) Undeprecate() *Operation {
  154. o.Deprecated = false
  155. return o
  156. }
  157. // WithConsumes adds media types for incoming body values
  158. func (o *Operation) WithConsumes(mediaTypes ...string) *Operation {
  159. o.Consumes = append(o.Consumes, mediaTypes...)
  160. return o
  161. }
  162. // WithProduces adds media types for outgoing body values
  163. func (o *Operation) WithProduces(mediaTypes ...string) *Operation {
  164. o.Produces = append(o.Produces, mediaTypes...)
  165. return o
  166. }
  167. // WithTags adds tags for this operation
  168. func (o *Operation) WithTags(tags ...string) *Operation {
  169. o.Tags = append(o.Tags, tags...)
  170. return o
  171. }
  172. // AddParam adds a parameter to this operation, when a parameter for that location
  173. // and with that name already exists it will be replaced
  174. func (o *Operation) AddParam(param *Parameter) *Operation {
  175. if param == nil {
  176. return o
  177. }
  178. for i, p := range o.Parameters {
  179. if p.Name == param.Name && p.In == param.In {
  180. params := append(o.Parameters[:i], *param)
  181. params = append(params, o.Parameters[i+1:]...)
  182. o.Parameters = params
  183. return o
  184. }
  185. }
  186. o.Parameters = append(o.Parameters, *param)
  187. return o
  188. }
  189. // RemoveParam removes a parameter from the operation
  190. func (o *Operation) RemoveParam(name, in string) *Operation {
  191. for i, p := range o.Parameters {
  192. if p.Name == name && p.In == name {
  193. o.Parameters = append(o.Parameters[:i], o.Parameters[i+1:]...)
  194. return o
  195. }
  196. }
  197. return o
  198. }
  199. // SecuredWith adds a security scope to this operation.
  200. func (o *Operation) SecuredWith(name string, scopes ...string) *Operation {
  201. o.Security = append(o.Security, map[string][]string{name: scopes})
  202. return o
  203. }
  204. // WithDefaultResponse adds a default response to the operation.
  205. // Passing a nil value will remove the response
  206. func (o *Operation) WithDefaultResponse(response *Response) *Operation {
  207. return o.RespondsWith(0, response)
  208. }
  209. // RespondsWith adds a status code response to the operation.
  210. // When the code is 0 the value of the response will be used as default response value.
  211. // When the value of the response is nil it will be removed from the operation
  212. func (o *Operation) RespondsWith(code int, response *Response) *Operation {
  213. if o.Responses == nil {
  214. o.Responses = new(Responses)
  215. }
  216. if code == 0 {
  217. o.Responses.Default = response
  218. return o
  219. }
  220. if response == nil {
  221. delete(o.Responses.StatusCodeResponses, code)
  222. return o
  223. }
  224. if o.Responses.StatusCodeResponses == nil {
  225. o.Responses.StatusCodeResponses = make(map[int]Response)
  226. }
  227. o.Responses.StatusCodeResponses[code] = *response
  228. return o
  229. }