-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
110 lines (90 loc) · 2.16 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
package postmark
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
var (
// PostmarkAPIAddr is the default, production target for API requests.
PostmarkAPIAddr = "https://api.postmarkapp.com"
)
// Client provides access to Postmark API endpoints in this module.
type Client struct {
APIAddr string
AccountToken string
ServerToken string
}
type apiError struct {
ErrorCode int `json:"ErrorCode"`
Message string `json:"Message"`
}
type parameters struct {
method string
endpoint string
expectedStatusCode int
respTarget interface{}
token string
payload interface{}
}
// NewClient returns a new client configured for use.
func NewClient(apiToken string, serverToken string) *Client {
return &Client{
APIAddr: PostmarkAPIAddr,
AccountToken: apiToken,
ServerToken: serverToken,
}
}
func (c *Client) doRequest(opts parameters) error {
url := fmt.Sprintf("%s%s", c.APIAddr, opts.endpoint)
req, err := http.NewRequest(opts.method, url, nil)
if err != nil {
return err
}
if opts.payload != nil {
reqBody, err := json.Marshal(opts.payload)
if err != nil {
return err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
if opts.token == c.AccountToken {
req.Header.Add("X-Postmark-Account-Token", c.AccountToken)
}
if opts.token == c.ServerToken {
req.Header.Add("X-Postmark-Server-Token", c.ServerToken)
}
httpC := &http.Client{}
resp, err := httpC.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// Handle API errors
if resp.StatusCode != opts.expectedStatusCode {
var apiErr apiError
err = json.Unmarshal(body, &apiErr)
if err != nil {
return err
}
return fmt.Errorf("api error code %d: %s", apiErr.ErrorCode, apiErr.Message)
}
if opts.respTarget == nil {
// Short-circuit if we're not actually looking for the response
return nil
}
err = json.Unmarshal(body, opts.respTarget)
if err != nil {
return err
}
return nil
}