-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrapper.go
56 lines (48 loc) · 1.21 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
52
53
54
55
56
package gomo
import (
"net/http"
"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 interface{}
Request *http.Request
Response response
}
// APIResponse contains the response data to the call
type response struct {
Data interface{} `json:"data"`
Meta interface{} `json:"meta"`
Included interface{} `json:"included"`
Errors []APIError `json:"errors"`
}
// newAPIWrapper creates a new wrapper for this call
func newWrapper(method string, endpoint string, resources ...interface{}) wrapper {
var targetResource interface{}
var targetIncludes interface{}
for k, resource := range resources {
switch k {
case 0:
targetResource = resource
case 1:
targetIncludes = resource
}
}
// set the resource type if the entity has the SetType method
if targetResource, ok := targetResource.(interface{ SetType() }); ok {
targetResource.SetType()
}
return wrapper{
Method: strings.ToUpper(method),
Endpoint: endpoint,
Body: targetResource,
Response: response{
Data: targetResource,
Included: targetIncludes,
},
}
}