-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
145 lines (117 loc) Β· 2.85 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
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
package paymego
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
type SubscribeAPI struct {
headers xAuthHeaders
baseURL string
httpClient http.Client
logger *log.Logger
timeout time.Duration
}
type SubsribeAPIOpts struct {
PaycomID string
PaycomKey string
Logger *log.Logger
HTTPClient http.Client
BaseURL string
Timeout time.Duration
}
type xAuthHeaders struct {
paycomID string
paycomKey string
}
// NewSubscribeAPI returns new instance of SubscribeAPI
func NewSubscribeAPI(args SubsribeAPIOpts) (SubscribeAPI, error) {
err := args.validate()
if err != nil {
return SubscribeAPI{}, err
}
subscribeAPI := SubscribeAPI{
httpClient: args.HTTPClient,
baseURL: args.BaseURL,
logger: args.Logger,
headers: getXAuthHeaders(args.PaycomID, args.PaycomKey),
timeout: args.Timeout,
}
return subscribeAPI, nil
}
func (c *SubscribeAPI) sendRequest(
ctx context.Context,
requestID, method string,
params interface{},
withID bool,
timeout ...time.Duration,
) (*PaymeResponse, error) {
var requestTimeout time.Duration
if len(timeout) > 0 {
requestTimeout = timeout[0]
} else {
requestTimeout = c.timeout
}
// Create a context with the specified timeout.
ctx, cancel := context.WithTimeout(ctx, requestTimeout)
defer cancel()
data := map[string]interface{}{
"id": requestID,
"method": method,
"params": params,
}
requestBody, _ := json.Marshal(data)
req, err := http.NewRequestWithContext(ctx, "POST", c.baseURL, bytes.NewBuffer(requestBody))
if err != nil {
return nil, err
}
if withID {
req.Header.Set("X-Auth", c.headers.paycomID)
} else {
req.Header.Set("X-Auth", fmt.Sprintf("%s:%s", c.headers.paycomID, c.headers.paycomKey))
}
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
response, err := client.Do(req)
timeoutError := errors.Is(err, context.DeadlineExceeded)
if err != nil {
if timeoutError {
return nil, ErrPaymeTimeoutError
}
return nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var responseJson PaymeResponse
err = json.Unmarshal(responseBody, &responseJson)
if err != nil {
return nil, err
}
// Handle error response with payme specific error codes
responseJson, err = handleErrorResponse(responseJson)
if err != nil {
logrus.Errorf("error response from payme - %v error - %v", responseJson.Error, err)
}
return &responseJson, err
}
func (s SubsribeAPIOpts) validate() error {
if s.PaycomID == "" {
return ErrErrEmptyOrInvalidPaycomID
}
if s.PaycomKey == "" {
return ErrEmptyOrInvalidPaycomKey
}
return nil
}
func getXAuthHeaders(paycomID, paycomKey string) xAuthHeaders {
return xAuthHeaders{paycomID: paycomID, paycomKey: paycomKey}
}