-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.go
111 lines (98 loc) · 2.36 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
111
package revenuecat
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Client makes authorized calls to the RevenueCat API.
type Client struct {
apiKey string
apiURL string
http doer
sandbox bool
}
type Option func(*Client)
type doer interface {
Do(req *http.Request) (*http.Response, error)
}
// New returns a new *Client for the provided API key.
// For more information on authentication, see https://docs.revenuecat.com/docs/authentication.
func New(apiKey string, opts ...Option) *Client {
c := &Client{
apiKey: apiKey,
apiURL: "https://api.revenuecat.com/v1/",
http: &http.Client{
// Set a long timeout here since calls to Apple are probably involved.
Timeout: 10 * time.Second,
},
}
for _, opt := range opts {
opt(c)
}
return c
}
// WithHTTPClient - Option to set the HTTP client
func WithHTTPClient(client doer) Option {
return func(c *Client) {
c.http = client
}
}
// WithAPIURL - Option to set the API URL
func WithAPIURL(url string) Option {
return func(c *Client) {
c.apiURL = url
}
}
// WithSandboxEnabled - Option to enable or disable sandbox mode
func WithSandboxEnabled(enabled bool) Option {
return func(c *Client) {
c.sandbox = enabled
}
}
func (c *Client) call(method, path string, reqBody interface{}, platform string, respBody interface{}) error {
var reqBodyJSON io.Reader
if reqBody != nil {
js, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("error marshaling request body: %v", err)
}
reqBodyJSON = bytes.NewBuffer(js)
}
req, err := http.NewRequest(method, c.apiURL+path, reqBodyJSON)
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
req.Header.Add("Authorization", "Bearer "+c.apiKey)
req.Header.Add("Content-Type", "application/json")
if platform != "" {
req.Header.Add("X-Platform", platform)
}
if c.sandbox {
req.Header.Add("X-Is-Sandbox", "true")
}
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("error making request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
var errResp Error
err = json.NewDecoder(resp.Body).Decode(&errResp)
if err != nil {
return err
}
return errResp
}
if respBody == nil {
// Expecting an empty body.
return nil
}
err = json.NewDecoder(resp.Body).Decode(respBody)
if err != nil {
return fmt.Errorf("error decoding response: %v", err)
}
return nil
}