-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
62 lines (55 loc) · 1.65 KB
/
resource.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
package fasapay
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"time"
)
//ResourceAbstract base resource
type ResourceAbstract struct {
tr *Transport
cfg *Config
}
//BuildAuthParams method
func (ra *ResourceAbstract) buildAuthRequestParams(dt time.Time) *RequestAuthParams {
params := &RequestAuthParams{
ApiKey: ra.cfg.ApiKey,
Token: generateAuthToken(ra.cfg.ApiKey, ra.cfg.ApiSecretWord, dt),
}
return params
}
//BuildParams method
func (ra *ResourceAbstract) buildRequestParams(attributes *RequestParamsAttributes) RequestParams {
if attributes == nil {
dt := time.Now().UTC()
attributes = &RequestParamsAttributes{Id: fmt.Sprint(dt.Unix()), DateTime: dt}
}
return RequestParams{Id: attributes.Id, Auth: ra.buildAuthRequestParams(attributes.DateTime)}
}
//MarshalRequestParams method
func (ra *ResourceAbstract) marshalRequestParams(request interface{}) ([]byte, error) {
bts, err := xml.Marshal(request)
if err != nil {
return nil, err
}
req := []byte("req=")
req = append(req, bts...)
return req, nil
}
//UnmarshalResponse method
func (ra *ResourceAbstract) unmarshalResponse(resp *http.Response, v interface{}) error {
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("ResourceAbstract.unmarshalResponse read body: %v", err)
}
//reset the response body to the original unread state
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
return xml.Unmarshal(bodyBytes, &v)
}
//NewResourceAbstract Create new resource abstract
func NewResourceAbstract(transport *Transport, config *Config) ResourceAbstract {
return ResourceAbstract{tr: transport, cfg: config}
}