forked from andrew-waters/gomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.go
51 lines (45 loc) · 1.04 KB
/
wrapper.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
package gomo
import (
"encoding/json"
"net/http"
"net/url"
"strings"
)
// wrapper is a wrapper around a request and response to the API
type wrapper struct {
method string
endpoint string
statusCode int
executionTime APIExecution
body requestBody
query url.Values
request *http.Request
response map[string]json.RawMessage
resources []resourceTarget
errors []APIError
}
type resourceTarget struct {
section string
target interface{}
}
func (w *wrapper) addResource(section string, target interface{}) {
w.resources = append(
w.resources,
resourceTarget{section, target},
)
}
func (w *wrapper) apply(resources ...RequestResource) {
for _, resource := range resources {
resource(w)
}
}
// newWrapper creates a new wrapper for this call
func newWrapper(method string, endpoint string, resources ...RequestResource) wrapper {
wrapper := wrapper{
method: strings.ToUpper(method),
endpoint: endpoint,
query: make(url.Values),
}
wrapper.apply(resources...)
return wrapper
}