forked from yyoshiki41/go-pushwoosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
166 lines (137 loc) · 3.59 KB
/
client.go
File metadata and controls
166 lines (137 loc) · 3.59 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
// Package pushwoosh provides functions and structs for accessing the Pushwoosh Remote API.
package pushwoosh
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
"go.uber.org/zap"
)
const (
apiV13 = "1.3"
defaultHTTPTimeout = 120 * time.Second
)
var (
httpClient = &http.Client{Timeout: defaultHTTPTimeout}
)
// Config is a setting for Pushwoosh Remote APIs.
type Config struct {
Logger *zap.Logger
Endpoint string
ApplicationCode string
AccessToken string
}
// Result represents API Response for Pushwoosh.
type Result struct {
StatusCode int64 `json:"status_code"`
StatusMessage string `json:"status_message"`
Response ResponseMessages `json:"response"`
}
// ResponseMessages represents messages from Pushwoosh API
type ResponseMessages struct {
Messages []string `json:"Messages"`
UnknownDevices map[string][]string `json:"UnknownDevices"`
}
// Client represents an API client for Pushwoosh.
type Client struct {
httpClient *http.Client
config *Config
logger *zap.Logger
}
// NewClient returns a new pushwoosh API client.
func NewClient(config *Config) (*Client, error) {
if httpClient == nil {
return nil, errors.New("httpClient is nil")
}
if config == nil {
return nil, errors.New("config is nil")
}
var logger *zap.Logger
if config.Logger == nil {
logger = zap.NewNop()
} else {
logger = config.Logger
}
return &Client{
httpClient: httpClient,
config: config,
logger: logger,
}, nil
}
func (c *Client) call(ctx context.Context, method string, apiEndpoint string, params interface{}, res interface{}) error {
u, err := url.Parse(c.config.Endpoint)
if err != nil {
return err
}
u.Path = path.Join(u.Path, apiV13, apiEndpoint)
p, err := newRequestParams(c.config.ApplicationCode, c.config.AccessToken, params)
if err != nil {
return err
}
jsonParams, err := json.Marshal(p)
if err != nil {
return err
}
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(jsonParams))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req = req.WithContext(ctx)
c.logger.Debug("poshwoosh API request",
zap.String("method", req.Method),
zap.String("path", u.Path),
zap.String("body", string(jsonParams)))
response, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
if res == nil {
return nil
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("pushwoosh %s %s responses wish HTTP status %s", req.Method, u.Path, response.Status)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
c.logger.Debug("poshwoosh API response",
zap.String("method", req.Method),
zap.String("path", u.Path),
zap.String("body", string(data)))
return json.Unmarshal(data, &res)
}
type requestParams map[string]interface{}
func newRequestParams(application, auth string, params interface{}) (requestParams, error) {
jsonParams, err := json.Marshal(params)
if err != nil {
return nil, err
}
var reqValues map[string]interface{}
if err := json.Unmarshal(jsonParams, &reqValues); err != nil {
return nil, err
}
reqValues["application"] = application
reqValues["auth"] = auth
return requestParams{
"request": reqValues,
}, nil
}
func (p *requestParams) setApplication(application string) {
(*p)["application"] = application
}
func (p *requestParams) setAuth(auth string) {
(*p)["auth"] = auth
}
// SetHTTPClient overrides the default HTTP client.
func SetHTTPClient(client *http.Client) {
httpClient = client
}