-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (67 loc) · 2.11 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
package mercadopago
import (
"encoding/json"
"net/http"
)
// Client is the api client
type Client interface {
// NewCardToken creates a new CardToken
NewCardToken(params CardTokenParams) (*CardToken, error)
// GetIdentificationTypes returns all available identification types
GetIdentificationTypes() (IdentificationTypes, error)
// GetPaymentMethods returns all available payment methods
GetPaymentMethods() (PaymentMethods, error)
// GetPaymentMethodsForBin returns all available payment methods for the given bin
GetPaymentMethodsForBin(bin string) (PaymentMethods, error)
// GetInstallments returns all available installments for the given
// payment method, amount and issuer
GetInstallments(params GetInstallmentsParams) (Installments, error)
// GetCardIssuers returns all available issuers for the given payment method
GetCardIssuers(paymentMethodID string) (Issuers, error)
// NewPayment creates a new Payment
NewPayment(params PaymentParams) (*Payment, error)
// NewTestUser creates a new test user
NewTestUser(params TestUserParams) (*TestUser, error)
}
// client is the api client implementation
type client struct {
version string
httpClient *http.Client
baseURL string
accessToken string
publicKey string
}
// NewClient creates a new Client
func NewClient(baseURL, publicKey, accessToken string) Client {
return &client{
httpClient: http.DefaultClient,
baseURL: baseURL,
accessToken: accessToken,
publicKey: publicKey,
version: "1.3.1",
}
}
func (c *client) requestAndDecode(req *http.Request, response interface{}) error {
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
err = json.NewDecoder(resp.Body).Decode(response)
return err
}
var errResp Error
err = json.NewDecoder(resp.Body).Decode(&errResp)
if err != nil {
return err
}
// convert cause's code from float to int
for i, cause := range errResp.Cause {
if code, ok := cause.Code.(float64); ok {
cause.Code = int(code)
errResp.Cause[i] = cause
}
}
return errResp
}