-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
155 lines (126 loc) · 2.91 KB
/
client.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
package crunchclient
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
)
const (
DefaultTimeout = 10 * time.Second
DefaultUserAgent = "github.com/verizonconnect/42crunch-client-go"
)
type contextKey string
type Client struct {
httpClient *http.Client
baseURL *url.URL
userAgent string
debug bool
Collections CollectionsService
API ApiService
}
func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
if baseURL == "" {
return nil, fmt.Errorf("no api base url provided")
}
u, err := url.ParseRequestURI(baseURL)
if err != nil {
return nil, err
}
client := Client{
baseURL: u,
httpClient: &http.Client{
Timeout: DefaultTimeout,
},
userAgent: DefaultUserAgent,
debug: false,
}
for _, option := range options {
if optionErr := option(&client); optionErr != nil {
return nil, optionErr
}
}
client.Collections = CollectionsService{client: &client}
client.API = ApiService{client: &client}
return &client, nil
}
func (c Client) newRequest(ctx context.Context, method, path string, options ...requestOption) (*http.Request, error) {
u, err := c.baseURL.Parse(path)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.userAgent)
for _, option := range options {
if err = option(req); err != nil {
return nil, err
}
}
return req, nil
}
type requestOption func(*http.Request) error
func (c Client) doRequest(req *http.Request, v interface{}) (resp apiResponse, err error) {
if c.debug {
reqDump, _ := httputil.DumpRequestOut(req, true)
log.Printf("sending request:\n>>>>>>\n%s\n>>>>>>\n", string(reqDump))
}
res, err := c.httpClient.Do(req)
if err != nil {
return
}
defer res.Body.Close()
if c.debug {
resDump, _ := httputil.DumpResponse(res, true)
log.Printf("received response:\n<<<<<<\n%s\n<<<<<<\n", string(resDump))
}
err = checkResponseForError(res)
if err != nil {
return
}
if v != nil {
switch vt := v.(type) {
case *string:
if content, readErr := io.ReadAll(res.Body); readErr == nil {
*vt = strings.TrimSpace(string(content))
} else {
err = readErr
return
}
default:
err = json.NewDecoder(res.Body).Decode(v)
if err != nil {
return
}
}
}
resp, err = c.newAPIResponse(res)
return resp, err
}
type apiResponse struct {
*http.Response
TotalCount int
}
func (c Client) newAPIResponse(res *http.Response) (a apiResponse, err error) {
a = apiResponse{Response: res}
totalCount, ok := a.Header["X-Total-Count"]
if ok && len(totalCount) > 0 {
totalCountVal, convErr := strconv.Atoi(totalCount[0])
if convErr != nil {
err = convErr
return
}
a.TotalCount = totalCountVal
}
return
}
type ClientOption func(*Client) error