-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiaphub.go
202 lines (168 loc) · 4.02 KB
/
iaphub.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package iaphub
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// Get User
type Platform string
// App environment (production by default)
type Env string
// Direction
type Order string
const (
PlatformIOS Platform = "ios"
PlatformAndroid Platform = "android"
EnvProduction Env = "production"
Ask Order = "ask"
Desc Order = "desc"
)
const ApiUrl = "https://api.iaphub.com/v1"
const (
pathGetUser = "/app/%s/user/%s"
pathMigrateUser = "/app/%s/user/%s/migrate"
pathUpdateUser = "/app/%s/user/%s"
pathGetReceipt = "/app/%s/receipt/%s"
pathUpdateReceipt = "/app/%s/user/%s/receipt"
pathGetPurchase = "/app/%s/purchase/%s"
pathGetPurchases = "/app/%s/purchases"
pathGetSubscription = "/app/%s/subscription/%s"
)
type Client struct {
apiKey string
appId string
client *http.Client
env Env
}
func NewClient(apiKey string, appId string, options ...Option) (*Client, error) {
config := &config{
requestTimeout: 3 * time.Second,
client: http.DefaultClient,
env: EnvProduction,
}
for _, o := range options {
err := o(config)
if err != nil {
return nil, err
}
}
return &Client{
apiKey: apiKey,
appId: appId,
client: config.client,
env: config.env,
}, nil
}
func (c *Client) requestGet(path string, queryParams map[string]string) ([]byte, error) {
fpath := ApiUrl + path
request, err := c.newRequest(http.MethodGet, fpath, queryParams, nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(request)
if err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err = resp.Body.Close(); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(body))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
return body, err
}
func (c *Client) requestPost(path string, queryParams map[string]string, data interface{}) ([]byte, error) {
fpath := ApiUrl + path
request, err := c.newRequest(http.MethodPost, fpath, queryParams, data)
if err != nil {
return nil, err
}
resp, err := c.client.Do(request)
if err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
} else if err = resp.Body.Close(); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, string(body))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
return body, err
}
func (c *Client) newRequest(method string, path string, params map[string]string, data interface{}) (*http.Request, error) {
baseURL, err := url.Parse(path)
if err != nil {
return nil, err
}
ps := url.Values{}
if params != nil {
for k, v := range params {
ps.Set(k, v)
}
}
ps.Set("environment", string(c.env))
baseURL.RawQuery = ps.Encode()
req, err := http.NewRequest(method, baseURL.String(), nil)
if err != nil {
return nil, err
}
if data != nil {
jsonString, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err = http.NewRequest(method, baseURL.String(), bytes.NewBuffer(jsonString))
if err != nil {
return nil, err
}
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Set("Authorization", "ApiKey "+c.apiKey)
return req, err
}
// UseClient sets custom HTTP client.
func UseClient(httpClient *http.Client) Option {
return func(c *config) error {
if httpClient == nil {
return errors.New("HTTP client is not specified")
}
c.client = httpClient
return nil
}
}
func UseEnv(env Env) Option {
return func(c *config) error {
if env == "" {
return errors.New("environment is not specified")
}
c.env = env
return nil
}
}
type config struct {
requestTimeout time.Duration
client *http.Client
env Env
}
type Option func(*config) error