@@ -2,71 +2,28 @@ package coinbase
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
- "time"
7
5
8
6
"github.com/benalucorp/coinbase-commerce-go/pkg/api"
7
+ "github.com/benalucorp/coinbase-commerce-go/pkg/api/stub"
9
8
"github.com/benalucorp/coinbase-commerce-go/pkg/entity"
10
- "github.com/go-resty/resty/v2"
11
9
)
12
10
13
- type Config struct {
14
- // Key is the authentication API key.
15
- // Most requests to the Commerce API must be authenticated with an API key.
16
- // You can create an API key in your Settings page after creating a Coinbase Commerce account.
17
- // Reference: https://commerce.coinbase.com/docs/api/#authentication
18
- Key string
19
- // Timeout describes total waiting time before a request is treated as timeout.
20
- // Default: 1 min.
21
- Timeout time.Duration
22
- // RetryCount describes total number of retry in case error occurred.
23
- // Set 0 to disable retry mechanism.
24
- // Default: 3.
25
- RetryCount int
26
- // RetryMaxWaitTime describes total waiting time between each retry.
27
- // Default: 2 second.
28
- RetryMaxWaitTime time.Duration
29
- // Debug describes the client to enter debug mode.
30
- Debug bool
31
- }
32
-
33
- func (c * Config ) Validate () error {
34
- if c .Key == "" {
35
- return errors .New ("config: invalid key" )
36
- }
37
- if c .Timeout <= 0 {
38
- c .Timeout = time .Minute
39
- }
40
- if c .RetryCount < 0 {
41
- c .RetryCount = 3
42
- }
43
- if c .RetryMaxWaitTime <= 0 {
44
- c .RetryMaxWaitTime = 2 * time .Second
45
- }
46
- return nil
47
- }
48
-
49
11
// NewClient creates a client to interact with Coinbase Commerce API.
50
- func NewClient (cfg Config ) (* Client , error ) {
12
+ func NewClient (cfg api. Config ) (* Client , error ) {
51
13
if err := cfg .Validate (); err != nil {
52
14
return nil , err
53
15
}
54
16
55
- r := resty .New ().
56
- SetHostURL (api .HostURL ).
57
- SetHeaders (api .DefaultHeaders (cfg .Key )).
58
- SetTimeout (cfg .Timeout ).
59
- SetRetryCount (cfg .RetryCount ).
60
- SetRetryMaxWaitTime (cfg .RetryMaxWaitTime ).
61
- SetDebug (cfg .Debug )
62
-
63
17
return & Client {
64
- charges : api .NewCharges (r ),
18
+ charges : api .NewCharges (cfg ),
19
+ chargesStub : stub .NewCharges (),
65
20
}, nil
66
21
}
67
22
23
+ // Client is the main client to interact with Coinbase Commerce API.
68
24
type Client struct {
69
- charges * api.Charges
25
+ charges api.ChargesItf
26
+ chargesStub api.ChargesItf
70
27
}
71
28
72
29
// CreateCharge charge a customer with certain amount of currency.
@@ -76,14 +33,9 @@ type Client struct {
76
33
// to the blockchain before the charge expires.
77
34
// Reference: https://commerce.coinbase.com/docs/api/#create-a-charge
78
35
func (c Client ) CreateCharge (ctx context.Context , req * entity.CreateChargeReq ) (* entity.CreateChargeResp , error ) {
79
- if c . charges == nil {
80
- return nil , errors . New ( "client: initialize first" )
36
+ if stub . Ok ( ctx ) {
37
+ return c . chargesStub . Create ( ctx , req )
81
38
}
82
-
83
- if req == nil {
84
- return nil , errors .New ("payload: missing" )
85
- }
86
-
87
39
return c .charges .Create (ctx , req )
88
40
}
89
41
@@ -92,32 +44,21 @@ func (c Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (
92
44
// This information is also returned when a charge is first created.
93
45
// Reference: https://commerce.coinbase.com/docs/api/#show-a-charge
94
46
func (c Client ) ShowCharge (ctx context.Context , req * entity.ShowChargeReq ) (* entity.ShowChargeResp , error ) {
95
- if c .charges == nil {
96
- return nil , errors .New ("client: initialize first" )
97
- }
98
-
99
- if req == nil {
100
- return nil , errors .New ("payload: missing" )
101
- }
102
-
103
47
if err := req .Validate (); err != nil {
104
48
return nil , err
105
49
}
106
-
50
+ if stub .Ok (ctx ) {
51
+ return c .chargesStub .Show (ctx , req )
52
+ }
107
53
return c .charges .Show (ctx , req )
108
54
}
109
55
110
56
// ListCharges lists all the charges.
111
57
// Reference: https://commerce.coinbase.com/docs/api/#list-charges
112
58
func (c Client ) ListCharges (ctx context.Context , req * entity.ListChargesReq ) (* entity.ListChargesResp , error ) {
113
- if c .charges == nil {
114
- return nil , errors .New ("client: initialize first" )
115
- }
116
-
117
- if req == nil {
118
- return nil , errors .New ("payload: missing" )
59
+ if stub .Ok (ctx ) {
60
+ return c .chargesStub .List (ctx , req )
119
61
}
120
-
121
62
return c .charges .List (ctx , req )
122
63
}
123
64
@@ -131,18 +72,12 @@ func (c Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*e
131
72
//
132
73
// Reference: https://commerce.coinbase.com/docs/api/#cancel-a-charge
133
74
func (c Client ) CancelCharge (ctx context.Context , req * entity.CancelChargeReq ) (* entity.CancelChargeResp , error ) {
134
- if c .charges == nil {
135
- return nil , errors .New ("client: initialize first" )
136
- }
137
-
138
- if req == nil {
139
- return nil , errors .New ("payload: missing" )
140
- }
141
-
142
75
if err := req .Validate (); err != nil {
143
76
return nil , err
144
77
}
145
-
78
+ if stub .Ok (ctx ) {
79
+ return c .chargesStub .Cancel (ctx , req )
80
+ }
146
81
return c .charges .Cancel (ctx , req )
147
82
}
148
83
@@ -155,17 +90,11 @@ func (c Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (
155
90
//
156
91
// Reference: https://commerce.coinbase.com/docs/api/#resolve-a-charge
157
92
func (c Client ) ResolveCharge (ctx context.Context , req * entity.ResolveChargeReq ) (* entity.ResolveChargeResp , error ) {
158
- if c .charges == nil {
159
- return nil , errors .New ("client: initialize first" )
160
- }
161
-
162
- if req == nil {
163
- return nil , errors .New ("payload: missing" )
164
- }
165
-
166
93
if err := req .Validate (); err != nil {
167
94
return nil , err
168
95
}
169
-
96
+ if stub .Ok (ctx ) {
97
+ return c .chargesStub .Resolve (ctx , req )
98
+ }
170
99
return c .charges .Resolve (ctx , req )
171
100
}
0 commit comments