-
Notifications
You must be signed in to change notification settings - Fork 6
/
handlers.go
160 lines (130 loc) · 3.58 KB
/
handlers.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
package server
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"github.com/Clever/wag/samples/gen-go-errors/models"
"github.com/go-errors/errors"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/gorilla/mux"
"gopkg.in/Clever/kayvee-go.v6/logger"
)
var _ = strconv.ParseInt
var _ = strfmt.Default
var _ = swag.ConvertInt32
var _ = errors.New
var _ = mux.Vars
var _ = bytes.Compare
var _ = ioutil.ReadAll
var formats = strfmt.Default
var _ = formats
// convertBase64 takes in a string and returns a strfmt.Base64 if the input
// is valid base64 and an error otherwise.
func convertBase64(input string) (strfmt.Base64, error) {
temp, err := formats.Parse("byte", input)
if err != nil {
return strfmt.Base64{}, err
}
return *temp.(*strfmt.Base64), nil
}
// convertDateTime takes in a string and returns a strfmt.DateTime if the input
// is a valid DateTime and an error otherwise.
func convertDateTime(input string) (strfmt.DateTime, error) {
temp, err := formats.Parse("date-time", input)
if err != nil {
return strfmt.DateTime{}, err
}
return *temp.(*strfmt.DateTime), nil
}
// convertDate takes in a string and returns a strfmt.Date if the input
// is a valid Date and an error otherwise.
func convertDate(input string) (strfmt.Date, error) {
temp, err := formats.Parse("date", input)
if err != nil {
return strfmt.Date{}, err
}
return *temp.(*strfmt.Date), nil
}
func jsonMarshalNoError(i interface{}) string {
bytes, err := json.MarshalIndent(i, "", "\t")
if err != nil {
// This should never happen
return ""
}
return string(bytes)
}
// statusCodeForGetBook returns the status code corresponding to the returned
// object. It returns -1 if the type doesn't correspond to anything.
func statusCodeForGetBook(obj interface{}) int {
switch obj.(type) {
case *models.ExtendedError:
return 400
case *models.InternalError:
return 500
case *models.NotFound:
return 404
case models.ExtendedError:
return 400
case models.InternalError:
return 500
case models.NotFound:
return 404
default:
return -1
}
}
func (h handler) GetBookHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
input, err := newGetBookInput(r)
if err != nil {
logger.FromContext(ctx).AddContext("error", err.Error())
http.Error(w, jsonMarshalNoError(models.ExtendedError{Message: err.Error()}), http.StatusBadRequest)
return
}
err = input.Validate()
if err != nil {
logger.FromContext(ctx).AddContext("error", err.Error())
http.Error(w, jsonMarshalNoError(models.ExtendedError{Message: err.Error()}), http.StatusBadRequest)
return
}
err = h.GetBook(ctx, input)
if err != nil {
logger.FromContext(ctx).AddContext("error", err.Error())
if btErr, ok := err.(*errors.Error); ok {
logger.FromContext(ctx).AddContext("stacktrace", string(btErr.Stack()))
}
statusCode := statusCodeForGetBook(err)
if statusCode == -1 {
err = models.InternalError{Message: err.Error()}
statusCode = 500
}
http.Error(w, jsonMarshalNoError(err), statusCode)
return
}
w.WriteHeader(200)
w.Write([]byte(""))
}
// newGetBookInput takes in an http.Request an returns the input struct.
func newGetBookInput(r *http.Request) (*models.GetBookInput, error) {
var input models.GetBookInput
var err error
_ = err
idStr := mux.Vars(r)["id"]
if len(idStr) == 0 {
return nil, errors.New("parameter must be specified")
}
idStrs := []string{idStr}
if len(idStrs) > 0 {
var idTmp int64
idStr := idStrs[0]
idTmp, err = swag.ConvertInt64(idStr)
if err != nil {
return nil, err
}
input.ID = idTmp
}
return &input, nil
}