-
Notifications
You must be signed in to change notification settings - Fork 1
/
petstore_test.go
289 lines (277 loc) · 7.75 KB
/
petstore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package sashay_test
import (
"fmt"
"github.com/rgalanakis/sashay"
"io/ioutil"
"net/http"
"os"
)
// Stand-in for whatever HTTP framework you are using
type FrameworkRouter struct{}
func (FrameworkRouter) ServeHTTP(http.ResponseWriter, *http.Request) {}
func (FrameworkRouter) AddRoute(method, path string, h http.HandlerFunc) {}
// main.go
func PetStoreSwagger() *sashay.Sashay {
return sashay.New(
"Swagger Petstore",
"A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification",
"1.0.0").
AddAPIKeySecurity("header", "api_key").
SetTermsOfService("http://swagger.io/terms/").
SetContact("", "", "[email protected]").
SetLicense("Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0.html").
AddServer("http://petstore.swagger.io/api", "Public API server").
AddTag("pet", "Everything about your Pets").
AddTag("store", "Access to Petstore orders").
AddTag("user", "Operations about user")
}
// noinspection GoUnusedExportedFunction
func StartServer() {
sw := PetStoreSwagger()
router := &FrameworkRouter{}
RegisterRoutes(router, sw)
if len(os.Args) > 0 && os.Args[0] == "-swagger" {
yaml := sw.BuildYAML()
ioutil.WriteFile("swagger.yml", []byte(yaml), 0644)
os.Exit(0)
}
http.ListenAndServe(":8080", router)
}
// handlers.go file
// noinspection GoUnusedParameter
func GetPets(http.ResponseWriter, *http.Request) {
// Your code here
}
var CreatePet http.HandlerFunc
var GetPet http.HandlerFunc
var DeletePet http.HandlerFunc
// models.go file
type Pet struct {
ID int `json:"id"`
Name string `json:"name"`
Tag string `json:"tag"`
}
type Error struct {
Code int32 `json:"code"`
Message string `json:"message"`
}
// routes.go file
type Route struct {
operation sashay.Operation
handler http.HandlerFunc
}
func RegisterRoutes(router *FrameworkRouter, sw *sashay.Sashay) {
for _, route := range routes {
sw.Add(route.operation)
router.AddRoute(route.operation.Method, route.operation.Path, route.handler)
}
}
var routes = []Route{
{
sashay.NewOperation(
"GET",
"/pets",
"Returns all pets from the system that the user has access to",
struct {
Tags []string `query:"tags" description:"tags to filter by"`
Limit int32 `query:"limit" description:"maximum number of results to return"`
}{},
[]Pet{},
Error{},
), GetPets,
}, {
sashay.NewOperation(
"POST",
"/pets",
"Creates a new pet in the store. Duplicates are allowed",
struct {
Name string `json:"name"`
Tag string `json:"tag"`
}{},
sashay.NewResponse(200, "pet response", Pet{}), // Normally a 201
Error{},
), CreatePet,
}, {
sashay.NewOperation(
"GET",
"/pets/:id",
"Returns a user based on a single ID, if the user does not have access to the pet",
struct {
ID int `path:"id" description:"ID of pet to fetch"`
}{},
Pet{},
Error{},
), GetPet,
}, {
sashay.NewOperation(
"DELETE",
"/pets/:id",
"deletes a single pet based on the ID supplied",
struct {
ID int `path:"id" description:"ID of pet to delete"`
}{},
nil, // 204 response
Error{},
), DeletePet,
},
}
func Example_petstore() {
sw := PetStoreSwagger()
RegisterRoutes(&FrameworkRouter{}, sw)
fmt.Println(sw.BuildYAML())
// Output:
// openapi: 3.0.0
// info:
// title: Swagger Petstore
// description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
// termsOfService: http://swagger.io/terms/
// contact:
// email: [email protected]
// license:
// name: Apache 2.0
// url: http://www.apache.org/licenses/LICENSE-2.0.html
// version: 1.0.0
// tags:
// - name: pet
// description: Everything about your Pets
// - name: store
// description: Access to Petstore orders
// - name: user
// description: Operations about user
// servers:
// - url: http://petstore.swagger.io/api
// description: Public API server
// paths:
// /pets:
// get:
// operationId: getPets
// summary: Returns all pets from the system that the user has access to
// parameters:
// - name: tags
// in: query
// description: tags to filter by
// schema:
// type: array
// items:
// type: string
// - name: limit
// in: query
// description: maximum number of results to return
// schema:
// type: integer
// format: int32
// responses:
// '200':
// description: ok response
// content:
// application/json:
// schema:
// type: array
// items:
// $ref: '#/components/schemas/Pet'
// 'default':
// description: error response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Error'
// post:
// operationId: postPets
// summary: Creates a new pet in the store. Duplicates are allowed
// requestBody:
// required: true
// content:
// application/json:
// schema:
// type: object
// properties:
// name:
// type: string
// tag:
// type: string
// responses:
// '200':
// description: pet response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Pet'
// 'default':
// description: error response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Error'
// /pets/{id}:
// get:
// operationId: getPetsId
// summary: Returns a user based on a single ID, if the user does not have access to the pet
// parameters:
// - name: id
// in: path
// required: true
// description: ID of pet to fetch
// schema:
// type: integer
// format: int64
// responses:
// '200':
// description: ok response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Pet'
// 'default':
// description: error response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Error'
// delete:
// operationId: deletePetsId
// summary: deletes a single pet based on the ID supplied
// parameters:
// - name: id
// in: path
// required: true
// description: ID of pet to delete
// schema:
// type: integer
// format: int64
// responses:
// '204':
// description: The operation completed successfully.
// 'default':
// description: error response
// content:
// application/json:
// schema:
// $ref: '#/components/schemas/Error'
// components:
// schemas:
// Error:
// type: object
// properties:
// code:
// type: integer
// format: int32
// message:
// type: string
// Pet:
// type: object
// properties:
// id:
// type: integer
// format: int64
// name:
// type: string
// tag:
// type: string
// securitySchemes:
// apiKeyAuth:
// type: apiKey
// in: header
// name: api_key
// security:
// - apiKeyAuth: []
}