-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.go
144 lines (114 loc) · 3.72 KB
/
interfaces.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
package pantopoda
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/Kamva/nautilus"
code "github.com/Kamva/pantopoda/http"
"github.com/Kamva/shark"
)
// RequestData is an interface for incoming request payload
type RequestData interface {
nautilus.Taggable
// Validate runs request data validation and returns validation error if
Validate() ValidationError
}
// ErrorType is a string subtype used inside ValidationError type which
// determines the type of validation error. There are two type of validation
// errors; BadRequest, that returned when the request payload did not match
// with request data structure. And RuleViolation, that returned when any of
// specified rules in validation tag violated in incoming request payload.
type ErrorType string
// BadRequest that returned when the request payload did not match with
// request data structure
const BadRequest ErrorType = "bad_request"
// RuleViolation that returned when any of specified rules in validation tags
// violated in incoming request payload.
const RuleViolation ErrorType = "validation_failed"
// ValidationError is an error type containing validation error bag and error
// type determine type of validation error.
type ValidationError struct {
// ErrorBag contains any validation errors on request fields.
ErrorBag shark.ErrorBag
// ErrorType is a string determine type of the validation error.
ErrorType ErrorType
}
// RequestHeaders represents the key-value pairs in an HTTP header.
type RequestHeaders map[string]string
// RequestBody represents the json body in an HTTP request body.
type RequestBody interface {
ToJSON() []byte
}
// JSONBody represents the json object body.
type JSONBody map[string]interface{}
// ToJSON converts the JSONBody to json bytes
func (body JSONBody) ToJSON() []byte {
b, err := json.Marshal(body)
shark.PanicIfError(err)
return b
}
// JSONArray represents the body with an array of json objects.
type JSONArray []JSONBody
// ToJSON converts the JSONArray to json bytes
func (body JSONArray) ToJSON() []byte {
b, err := json.Marshal(body)
shark.PanicIfError(err)
return b
}
// QueryParams represent url query params.
type QueryParams map[string][]string
// ToString converts QueryParams map to its string representation.
func (q QueryParams) ToString() string {
outSlice := make([]string, 0)
for key, value := range q {
if len(value) > 1 {
for _, v := range value {
outSlice = append(outSlice, fmt.Sprintf("%s[]=%s", key, v))
}
} else {
outSlice = append(outSlice, fmt.Sprintf("%s=%s", key, value))
}
}
return strings.Join(outSlice, "&")
}
// Empty checks if query param is empty.
func (q QueryParams) Empty() bool {
return len(q) == 0
}
// Request represent all data such as payload, query params, and header of a
// JSON HTTP request call.
type Request struct {
// Payload represent json body of HTTP call.
Payload RequestBody
// Query represent query params of HTTP call endpoint.
Query QueryParams
// Headers represent headers of HTTP call.
Headers RequestHeaders
}
// HasBody checks that request has payload
func (r *Request) HasBody() bool {
return r.Payload != nil
}
// Response represents HTTP call response body.
type Response struct {
json []byte
StatusCode code.StatusCode
Headers http.Header
}
// Unmarshal parses the JSON-encoded response and stores the result in the value
// pointed to by v.
func (r Response) Unmarshal(v interface{}) error {
return json.Unmarshal(r.json, v)
}
// ToString convert the response body to its string value.
func (r Response) ToString() string {
return string(r.json)
}
func newResponse(res *http.Response, body []byte) Response {
return Response{
json: body,
StatusCode: code.StatusCode(res.StatusCode),
Headers: res.Header,
}
}