-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
161 lines (144 loc) · 4.28 KB
/
http.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
package fasapay
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
//RequestBuilder handler
type RequestBuilder struct {
cfg *Config
}
//BuildUri method
func (rb *RequestBuilder) buildUri() (uri *url.URL, err error) {
u, err := url.Parse(rb.cfg.Uri)
if err != nil {
return nil, fmt.Errorf("RequestBuilder.buildUri parse: %v", err)
}
return u, err
}
//BuildBody method
func (rb *RequestBuilder) buildBody(body []byte) (io.Reader, error) {
return bytes.NewBuffer(body), nil
}
//BuildHeaders method
func (rb *RequestBuilder) buildHeaders() http.Header {
headers := http.Header{}
headers.Set("Content-Type", "application/x-www-form-urlencoded")
return headers
}
//BuildRequest method
func (rb *RequestBuilder) buildRequest(ctx context.Context, body []byte) (req *http.Request, err error) {
//build body
bodyReader, err := rb.buildBody(body)
//build uri
uri, err := rb.buildUri()
if err != nil {
return nil, fmt.Errorf("RequestBuilder.buildRequest build uri: %v", err)
}
//build request
req, err = http.NewRequestWithContext(ctx, "POST", uri.String(), bodyReader)
if err != nil {
return nil, fmt.Errorf("RequestBuilder.buildRequest new request error: %v", err)
}
//build headers
req.Header = rb.buildHeaders()
return req, nil
}
//NewHttpTransport create new http transport
func NewHttpTransport(config *Config, h *http.Client) *Transport {
rb := &RequestBuilder{cfg: config}
return &Transport{http: h, rb: rb}
}
//Transport wrapper
type Transport struct {
http *http.Client
rb *RequestBuilder
}
//SendRequest Send request method
func (tr *Transport) SendRequest(ctx context.Context, body []byte) (resp *http.Response, err error) {
req, err := tr.rb.buildRequest(ctx, body)
if err != nil {
return nil, fmt.Errorf("transport.SendRequest: %v", err)
}
return tr.http.Do(req)
}
//RequestParamsAttributes struct
type RequestParamsAttributes struct {
Id string `json:"id"`
DateTime time.Time `json:"date_time"`
}
//RequestParams struct
type RequestParams struct {
XMLName xml.Name `xml:"fasa_request"`
Id string `xml:"id,attr"`
Auth *RequestAuthParams `xml:"auth" json:"auth"`
}
//RequestAuthParams struct
type RequestAuthParams struct {
XMLName xml.Name `xml:"auth" json:"-"`
ApiKey string `xml:"api_key" json:"api_key"`
Token string `xml:"token" json:"token"`
}
//ResponseBody struct
type ResponseBody struct {
XMLName xml.Name `xml:"fasa_response" json:"-"`
Id string `xml:"id,attr" json:"id"`
DateTime string `xml:"date_time,attr" json:"date_time"`
Errors *ResponseBodyErrors `xml:"errors,omitempty" json:"errors,omitempty"`
}
//IsSuccess method
func (r *ResponseBody) IsSuccess() bool {
return r.Errors == nil
}
//GetError method
func (r *ResponseBody) GetError() string {
var message string
switch r.Errors.Code {
case ErrorCodeNotValidXmlRequest:
message = ErrorMessageNotValidXmlRequest
break
case ErrorCodeUnauthorized:
message = ErrorMessageUnauthorized
break
case ErrorCodeNotAcceptableTransfer:
message = ErrorMessageNotAcceptableTransfer
break
case ErrorCodeDetailRequestError:
message = ErrorMessageDetailRequestError
break
case ErrorCodeHistoryRequestError:
message = ErrorMessageHistoryRequestError
break
case ErrorCodeBalanceRequestError:
message = ErrorMessageBalanceRequestError
break
case ErrorCodeAccountRequestError:
message = ErrorMessageAccountRequestError
break
default:
message = ErrorMessageUnexpectedError
break
}
return message
}
//ResponseBodyErrors struct
type ResponseBodyErrors struct {
XMLName xml.Name `xml:"errors" json:"-"`
Id string `xml:"id,attr,omitempty" json:"id,omitempty"`
Mode string `xml:"mode,attr" json:"mode"`
Code uint64 `xml:"code,attr" json:"code"`
Data []*ResponseBodyErrorParams `xml:"data" json:"data"`
}
//ResponseBodyErrorParams struct
type ResponseBodyErrorParams struct {
XMLName xml.Name `xml:"data" json:"-"`
Code uint64 `xml:"code,omitempty" json:"code,omitempty"`
Attribute string `xml:"attribute,omitempty" json:"attribute,omitempty"`
Message string `xml:"message,omitempty" json:"message,omitempty"`
Detail string `xml:"detail,omitempty" json:"detail,omitempty"`
}