forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
86 lines (71 loc) · 1.75 KB
/
http.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
package mailjet
import (
"encoding/csv"
"fmt"
"net/http"
)
type httpClient struct {
client *http.Client
apiKeyPublic string
apiKeyPrivate string
headers map[string]string
request *http.Request
response interface{}
}
// APIKeyPublic returns the public key.
func (c *httpClient) APIKeyPublic() string {
return c.apiKeyPublic
}
// APIKeyPrivate returns the secret key.
func (c *httpClient) APIKeyPrivate() string {
return c.apiKeyPrivate
}
func (c *httpClient) Client() *http.Client {
return c.client
}
func (c *httpClient) SetClient(client *http.Client) {
c.client = client
}
func (c *httpClient) Send(req *http.Request) *httpClient {
c.request = req
return c
}
func (c *httpClient) With(headers map[string]string) *httpClient {
c.headers = headers
return c
}
func (c *httpClient) Read(response interface{}) *httpClient {
c.response = response
return c
}
func (c *httpClient) Call() (count, total int, err error) {
defer c.reset()
for key, value := range c.headers {
c.request.Header.Add(key, value)
}
resp, err := c.doRequest(c.request)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return count, total, err
} else if resp == nil {
return count, total, fmt.Errorf("empty response")
}
if c.response != nil {
if resp.Header["Content-Type"] != nil {
contentType := resp.Header["Content-Type"][0]
if contentType == "application/json" {
return readJSONResult(resp.Body, c.response)
} else if contentType == "text/csv" {
c.response, err = csv.NewReader(resp.Body).ReadAll()
}
}
}
return count, total, err
}
func (c *httpClient) reset() {
c.headers = make(map[string]string)
c.request = nil
c.response = nil
}