-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
153 lines (128 loc) · 3.64 KB
/
client.go
File metadata and controls
153 lines (128 loc) · 3.64 KB
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
package bytesend
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"time"
)
const defaultBaseURL = "https://bytesend.cloud/api/v1"
const userAgent = "bytesend-go/0.1.0"
const maxResponseBodyBytes int64 = 10 * 1024 * 1024 // 10 MB
type Client struct {
apiKey string
baseURL string
httpClient *http.Client
Emails *EmailsService
Contacts *ContactsService
ContactBooks *ContactBooksService
Domains *DomainsService
Campaigns *CampaignsService
Analytics *AnalyticsService
}
type ClientOption func(*Client)
func WithBaseURL(url string) ClientOption {
return func(c *Client) {
c.baseURL = url
}
}
func WithHTTPClient(h *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = h
}
}
func NewClient(apiKey string, opts ...ClientOption) (*Client, error) {
if apiKey == "" {
apiKey = os.Getenv("BYTESEND_API_KEY")
if apiKey == "" {
return nil, errors.New("missing API key")
}
}
c := &Client{
apiKey: apiKey,
baseURL: defaultBaseURL,
httpClient: &http.Client{Timeout: 30 * time.Second},
}
for _, opt := range opts {
opt(c)
}
c.Emails = &EmailsService{client: c}
c.Contacts = &ContactsService{client: c}
c.ContactBooks = &ContactBooksService{client: c}
c.Domains = &DomainsService{client: c}
c.Campaigns = &CampaignsService{client: c}
c.Analytics = &AnalyticsService{client: c}
return c, nil
}
func (c *Client) buildPath(path string, params map[string]string) string {
v := url.Values{}
for k, val := range params {
if val != "" {
v.Set(k, val)
}
}
if len(v) == 0 {
return path
}
return path + "?" + v.Encode()
}
func (c *Client) doRequest(ctx context.Context, method, path string, body any, v any, extraHeaders map[string]string) error {
var buf io.Reader
if body != nil {
b := &bytes.Buffer{}
if err := json.NewEncoder(b).Encode(body); err != nil {
return err
}
buf = b
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, buf)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", userAgent)
for k, val := range extraHeaders {
req.Header.Set(k, val)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
limited := io.LimitReader(resp.Body, maxResponseBodyBytes)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if v != nil {
if err := json.NewDecoder(limited).Decode(v); err != nil && err != io.EOF {
return err
}
}
return nil
}
errResp := &ErrorResponse{Message: resp.Status, Code: "INTERNAL_SERVER_ERROR"}
_ = json.NewDecoder(limited).Decode(errResp)
return errResp
}
func (c *Client) get(ctx context.Context, path string, out any) error {
return c.doRequest(ctx, http.MethodGet, path, nil, out, nil)
}
func (c *Client) post(ctx context.Context, path string, body any, out any) error {
return c.doRequest(ctx, http.MethodPost, path, body, out, nil)
}
func (c *Client) postWithHeaders(ctx context.Context, path string, body any, out any, headers map[string]string) error {
return c.doRequest(ctx, http.MethodPost, path, body, out, headers)
}
func (c *Client) put(ctx context.Context, path string, body any, out any) error {
return c.doRequest(ctx, http.MethodPut, path, body, out, nil)
}
func (c *Client) patch(ctx context.Context, path string, body any, out any) error {
return c.doRequest(ctx, http.MethodPatch, path, body, out, nil)
}
func (c *Client) delete(ctx context.Context, path string, body any, out any) error {
return c.doRequest(ctx, http.MethodDelete, path, body, out, nil)
}