-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrow.go
More file actions
171 lines (144 loc) · 3.5 KB
/
Copy pathenrow.go
File metadata and controls
171 lines (144 loc) · 3.5 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package enrow
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const defaultBaseURL = "https://api.enrow.io"
type Client struct {
Email *EmailFinder
Verify *EmailVerifier
Phone *PhoneFinder
ReverseEmail *ReverseEmailResource
Account *AccountResource
apiKey string
baseURL string
http *http.Client
}
type Option func(*Client)
func WithBaseURL(u string) Option {
return func(c *Client) { c.baseURL = u }
}
func WithHTTPClient(hc *http.Client) Option {
return func(c *Client) { c.http = hc }
}
func New(apiKey string, opts ...Option) *Client {
c := &Client{
apiKey: apiKey,
baseURL: defaultBaseURL,
http: &http.Client{Timeout: 30 * time.Second},
}
for _, opt := range opts {
opt(c)
}
c.Email = &EmailFinder{client: c}
c.Verify = &EmailVerifier{client: c}
c.Phone = &PhoneFinder{client: c}
c.ReverseEmail = &ReverseEmailResource{client: c}
c.Account = &AccountResource{client: c}
return c
}
func (c *Client) doPost(path string, body any, result any) error {
data, err := json.Marshal(body)
if err != nil {
return err
}
req, err := http.NewRequest("POST", c.baseURL+path, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("x-api-key", c.apiKey)
req.Header.Set("Content-Type", "application/json")
return c.doRequest(req, result)
}
func (c *Client) doGet(path string, params map[string]string, result any) error {
u := c.baseURL + path
if len(params) > 0 {
q := url.Values{}
for k, v := range params {
q.Set(k, v)
}
u += "?" + q.Encode()
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return err
}
req.Header.Set("x-api-key", c.apiKey)
return c.doRequest(req, result)
}
func (c *Client) doRequest(req *http.Request, result any) error {
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return parseError(resp.StatusCode, body)
}
return json.Unmarshal(body, result)
}
func parseError(status int, body []byte) error {
var apiErr struct {
Error string `json:"error"`
Message string `json:"message"`
}
if err := json.Unmarshal(body, &apiErr); err != nil {
apiErr.Message = string(body)
}
switch status {
case 401:
return &AuthenticationError{EnrowError{Status: status, Err: apiErr.Error, Message: apiErr.Message}}
case 422:
return &InsufficientBalanceError{EnrowError{Status: status, Err: apiErr.Error, Message: apiErr.Message}}
case 429:
return &RateLimitError{EnrowError: EnrowError{Status: status, Err: apiErr.Error, Message: apiErr.Message}}
default:
return &EnrowError{Status: status, Err: apiErr.Error, Message: apiErr.Message}
}
}
func poll[T any](fetcher func() (*T, error), isDone func(*T) bool, interval, timeout time.Duration) (*T, error) {
if interval == 0 {
interval = 2 * time.Second
}
if timeout == 0 {
timeout = 30 * time.Second
}
start := time.Now()
for {
result, err := fetcher()
if err != nil {
return nil, err
}
if isDone(result) {
return result, nil
}
if time.Since(start) >= timeout {
return result, nil
}
time.Sleep(interval)
}
}
// PollOptions controls auto-polling behavior.
type PollOptions struct {
WaitForResult bool
PollInterval time.Duration
Timeout time.Duration
}
func defaultPoll(opts *PollOptions) *PollOptions {
if opts == nil {
return &PollOptions{}
}
return opts
}
func formatPath(pattern string, args ...any) string {
return fmt.Sprintf(pattern, args...)
}