-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
83 lines (75 loc) · 2.01 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
package emailacid
import (
"encoding/json"
"fmt"
"net/url"
"path"
"github.com/Sirupsen/logrus"
"github.com/parnurzeal/gorequest"
"github.com/pkg/errors"
"github.com/pressly/lg"
)
type EmailAcidClient struct {
APIKey string
Password string
url string
ClientTypes []ClientType
}
type EmailAcidError struct {
Error EmailAcidErrorBody `json:error:omitempty"`
}
type EmailAcidErrorBody struct {
Name string `json:"name:omitempty"`
Message string `json:"message:omitempty"`
}
func New(APIKey, password string, clientTypes []ClientType, verbose bool) *EmailAcidClient {
logger := logrus.New()
if verbose {
logger.Level = logrus.DebugLevel
}
lg.DefaultLogger = logger
lg.RedirectStdlogOutput(logger)
return &EmailAcidClient{
APIKey: APIKey,
Password: password,
url: "https://api.emailonacid.com/v5",
ClientTypes: clientTypes,
}
}
func (client *EmailAcidClient) buildRequest(method, resourcePath string) (*gorequest.SuperAgent, error) {
request := gorequest.New()
u, err := url.Parse(client.url)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, resourcePath)
url := u.String()
switch method {
case gorequest.POST:
request = request.Post(url)
case gorequest.PUT:
request = request.Put(url)
case gorequest.DELETE:
request = request.Delete(url)
case gorequest.GET:
request = request.Get(url)
default:
return nil, fmt.Errorf("invalid method %s", method)
}
request = request.SetBasicAuth(client.APIKey, client.Password)
return request, nil
}
func sendRequest(request *gorequest.SuperAgent, in, out interface{}) (string, error) {
lg.Debugf("req [%s] %s", request.Method, request.Url)
res, body, errs := request.Send(in).EndStruct(out)
if len(errs) != 0 {
return "", errs[0]
}
lg.Debugf("res [%d]", res.StatusCode)
if res.StatusCode < 200 || res.StatusCode > 299 {
var apiError EmailAcidError
json.Unmarshal([]byte(body), &apiError)
return string(body), errors.Errorf("error making request: %s", apiError.Error.Message)
}
return string(body), nil
}