-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_simple_api.go
401 lines (301 loc) · 12 KB
/
example_simple_api.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"errors"
"log"
"net/http"
"strconv"
"github.com/ooaklee/reply"
)
/////////////////////////////////////////////////
/////// Custom Transition Object Example ////////
// This is an example of how you can create a
// custom response structure based on your
// requirements.
type fooReplyTransferObject struct {
HTTPWriter http.ResponseWriter `json:"-"`
Headers map[string]string `json:"-"`
StatusCode int `json:"-"`
Bar barEmbeddedExample `json:"bar,omitempty"`
}
type barEmbeddedExample struct {
Errors []reply.TransferObjectError `json:"errors,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
Data interface{} `json:"data,omitempty"`
AccessToken string `json:"access_token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
}
func (t *fooReplyTransferObject) SetHeaders(headers map[string]string) {
t.Headers = headers
}
func (t *fooReplyTransferObject) SetStatusCode(code int) {
t.StatusCode = code
}
func (t *fooReplyTransferObject) SetMeta(meta map[string]interface{}) {
t.Bar.Meta = meta
}
func (t *fooReplyTransferObject) SetWriter(writer http.ResponseWriter) {
t.HTTPWriter = writer
}
func (t *fooReplyTransferObject) SetTokenOne(token string) {
t.Bar.AccessToken = token
}
func (t *fooReplyTransferObject) SetTokenTwo(token string) {
t.Bar.RefreshToken = token
}
func (t *fooReplyTransferObject) GetWriter() http.ResponseWriter {
return t.HTTPWriter
}
func (t *fooReplyTransferObject) GetStatusCode() int {
return t.StatusCode
}
func (t *fooReplyTransferObject) SetData(data interface{}) {
t.Bar.Data = data
}
func (t *fooReplyTransferObject) RefreshTransferObject() reply.TransferObject {
return &fooReplyTransferObject{}
}
func (t *fooReplyTransferObject) SetErrors(transferObjectErrors []reply.TransferObjectError) {
t.Bar.Errors = transferObjectErrors
}
////////////////////
/////////////////////////////////////////////////
//// Custom Transition Object Error Example /////
// This is an example of how you can create a
// custom response structure for errrors retutned
// in the response
type barError struct {
// Title a short summary of the problem
Title string `json:"title,omitempty"`
// Message a description of the error
Message string `json:"message,omitempty"`
// About holds the link that gives further insight into the error
About string `json:"about,omitempty"`
// More randomd top level attribute to make error
// difference
More struct {
// Status the HTTP status associated with error
Status string `json:"status,omitempty"`
// Code internal error code used to reference error
Code string `json:"code,omitempty"`
// Meta contains additional meta-information about the error
Meta interface{} `json:"meta,omitempty"`
} `json:"more,omitempty"`
}
// SetTitle adds title to error
func (b *barError) SetTitle(title string) {
b.Title = title
}
// GetTitle returns error's title
func (b *barError) GetTitle() string {
return b.Title
}
// SetDetail adds detail to error
func (b *barError) SetDetail(detail string) {
b.Message = detail
}
// GetDetail return error's detail
func (b *barError) GetDetail() string {
return b.Message
}
// SetAbout adds about to error
func (b *barError) SetAbout(about string) {
b.About = about
}
// GetAbout return error's about
func (b *barError) GetAbout() string {
return b.About
}
// SetStatusCode converts and add http status code to error
func (b *barError) SetStatusCode(status int) {
b.More.Status = strconv.Itoa(status)
}
// GetStatusCode returns error's HTTP status code
func (b *barError) GetStatusCode() string {
return b.More.Status
}
// SetCode adds internal code to error
func (b *barError) SetCode(code string) {
b.More.Code = code
}
// GetCode returns error's internal code
func (b *barError) GetCode() string {
return b.More.Code
}
// SetMeta adds meta property to error
func (b *barError) SetMeta(meta interface{}) {
b.More.Meta = meta
}
// GetMeta returns error's meta property
func (b *barError) GetMeta() interface{} {
return b.More.Meta
}
// RefreshTransferObject returns an empty instance of transfer object
// error
func (b *barError) RefreshTransferObject() reply.TransferObjectError {
return &barError{}
}
////////////////////
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
// Example implementation of Error Manifest
var baseManifest []reply.ErrorManifest = []reply.ErrorManifest{
{"example-404-error": reply.ErrorManifestItem{Title: "resource not found", StatusCode: http.StatusNotFound}},
{"example-name-validation-error": reply.ErrorManifestItem{Title: "Validation Error", Detail: "The name provided does not meet validation requirements", StatusCode: http.StatusBadRequest, About: "www.example.com/reply/validation/1011", Code: "1011"}},
{"example-dob-validation-error": reply.ErrorManifestItem{Title: "Validation Error", Detail: "Check your DoB, and try again.", Code: "100YT", StatusCode: http.StatusBadRequest}},
}
// Replier with default Transition Object & Transition Object Error
var replier *reply.Replier = reply.NewReplier(baseManifest)
// Replier with custom Transition Object & default Transition Object Error
var replierWithCustomTransitionObj *reply.Replier = reply.NewReplier(baseManifest, reply.WithTransferObject(&fooReplyTransferObject{}))
// Replier with standard Transition Object & custom Transition Object Error
var replierWithCustomTransitionObjError *reply.Replier = reply.NewReplier(baseManifest, reply.WithTransferObjectError(&barError{}))
// Replier with custom Transition Object & custom Transition Object Error
var replierWithCustomTransitionObjs *reply.Replier = reply.NewReplier(baseManifest, reply.WithTransferObjectError(&barError{}), reply.WithTransferObject(&fooReplyTransferObject{}))
func simpleUsersAPINotFoundWithCustomTransitionObjsHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")
_ = replierWithCustomTransitionObjs.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: serverErr,
})
}
func simpleUsersAPINotFoundHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: serverErr,
})
}
func simpleUsersAPIMultiErrorHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErrs := []error{errors.New("example-dob-validation-error"), errors.New("example-name-validation-error")}
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Errors: serverErrs,
})
}
func simpleUsersAPIHandler(w http.ResponseWriter, r *http.Request) {
mockedQueriedUsers := []user{
{ID: 1, Name: "John Doe"},
{ID: 2, Name: "Sam Smith"},
}
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Data: mockedQueriedUsers,
})
}
func simpleUsersAPINoManifestEntryHandler(w http.ResponseWriter, r *http.Request) {
// unregisterdErr an error that's unregistered in manifest
// should return 500
unregisterdErr := errors.New("unexpected-error")
// mock passing additional headers in request
mockAdditionalHeaders := map[string]string{
"correlation-id": "d7c09ac2-fa46-4ece-bcde-1d7ad81d2230",
}
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: unregisterdErr,
Headers: mockAdditionalHeaders,
})
}
func simpleTokensAPIHandler(w http.ResponseWriter, r *http.Request) {
mockedAccessToken := "05e42c11-8bdd-423d-a2c1-c3c5c6604a30"
mockedRefreshToken := "0e95c426-d373-41a5-bfe1-08db322527bd"
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
TokenOne: mockedAccessToken,
TokenTwo: mockedRefreshToken,
})
}
func simpleAPIDefaultResponseHandler(w http.ResponseWriter, r *http.Request) {
// Do something that only needs an empty response body, and 200 status code
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
})
}
func simpleUsersAPINotFoundCustomReplierHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")
replierWithCustomTransitionObj.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: serverErr,
})
}
//////////////////////////////
//// Handlers Using Aides ////
func simpleUsersAPIMultiErrorUsingAideWithCustomErrorHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErrs := []error{errors.New("example-dob-validation-error"), errors.New("example-name-validation-error")}
_ = replierWithCustomTransitionObjError.NewHTTPMultiErrorResponse(w, serverErrs)
}
func simpleUsersAPIMultiErrorUsingAideHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErrs := []error{errors.New("example-dob-validation-error"), errors.New("example-name-validation-error")}
_ = replier.NewHTTPMultiErrorResponse(w, serverErrs)
}
func simpleUsersAPINotFoundUsingAideHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")
_ = replier.NewHTTPErrorResponse(w, serverErr)
}
func simpleUsersAPIUsingAideHandler(w http.ResponseWriter, r *http.Request) {
mockedQueriedUsers := []user{
{ID: 1, Name: "John Doe"},
{ID: 2, Name: "Sam Smith"},
}
_ = replier.NewHTTPDataResponse(w, http.StatusCreated, mockedQueriedUsers)
}
func simpleUsersAPINoManifestEntryUsingAideHandler(w http.ResponseWriter, r *http.Request) {
// unregisterdErr an error that's unregistered in manifest
// should return 500
unregisterdErr := errors.New("unexpected-error")
// mock passing additional headers in request
mockAdditionalHeaders := map[string]string{
"correlation-id": "d7c09ac2-fa46-4ece-bcde-1d7ad81d2230",
}
_ = replier.NewHTTPErrorResponse(w, unregisterdErr, reply.WithHeaders(mockAdditionalHeaders))
}
func simpleTokensAPIUsingAideHandler(w http.ResponseWriter, r *http.Request) {
mockedAccessToken := "05e42c11-8bdd-423d-a2c1-c3c5c6604a30"
mockedRefreshToken := "0e95c426-d373-41a5-bfe1-08db322527bd"
_ = replier.NewHTTPTokenResponse(w, http.StatusOK, mockedAccessToken, mockedRefreshToken)
}
func simpleAPIDefaultResponseUsingAideHandler(w http.ResponseWriter, r *http.Request) {
// Do something that only needs an empty response body.
// Note: 200 status code will be returned if status code passed is 0.
// Otherwise passed code would be used
_ = replier.NewHTTPBlankResponse(w, http.StatusOK)
}
func simpleUsersAPINotFoundCustomReplierUsingAideHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")
replierWithCustomTransitionObj.NewHTTPErrorResponse(w, serverErr)
}
/////////////////////////////
func handleRequest() {
var port string = ":8081"
http.HandleFunc("/errors", simpleUsersAPIMultiErrorHandler)
http.HandleFunc("/users", simpleUsersAPIHandler)
http.HandleFunc("/users/3", simpleUsersAPINotFoundHandler)
http.HandleFunc("/users/4", simpleUsersAPINoManifestEntryHandler)
http.HandleFunc("/tokens/refresh", simpleTokensAPIHandler)
http.HandleFunc("/defaults/1", simpleAPIDefaultResponseHandler)
http.HandleFunc("/users/3/custom", simpleUsersAPINotFoundCustomReplierHandler)
http.HandleFunc("/users/404/custom", simpleUsersAPINotFoundWithCustomTransitionObjsHandler)
http.HandleFunc("/aides/errors", simpleUsersAPIMultiErrorUsingAideHandler)
http.HandleFunc("/aides/errors/custom", simpleUsersAPIMultiErrorUsingAideWithCustomErrorHandler)
http.HandleFunc("/aides/users", simpleUsersAPIUsingAideHandler)
http.HandleFunc("/aides/users/3", simpleUsersAPINotFoundUsingAideHandler)
http.HandleFunc("/aides/users/4", simpleUsersAPINoManifestEntryUsingAideHandler)
http.HandleFunc("/aides/tokens/refresh", simpleTokensAPIUsingAideHandler)
http.HandleFunc("/aides/defaults/1", simpleAPIDefaultResponseUsingAideHandler)
http.HandleFunc("/aides/users/3/custom", simpleUsersAPINotFoundCustomReplierUsingAideHandler)
log.Printf("Serving simple API on port %s...", port)
log.Fatal(http.ListenAndServe(port, nil))
}
func main() {
handleRequest()
}