Skip to content

Commit 854d110

Browse files
authored
Configure HttpClient, added context parameter to all methods (#37)
Context is needed to limit time of execution of a method. Previously, context was passed to structure and it was stored inside structure which is a bad practice. Now we need to pass context to function, which is a best practice
1 parent 018d40e commit 854d110

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+698
-493
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func main() {
3636
client := ozon.NewClient("my-client-id", "my-api-key")
3737

3838
// Send request with parameters
39-
resp, err := client.Products().GetProductDetails(&ozon.GetProductDetailsParams{
39+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
40+
resp, err := client.Products().GetProductDetails(&ozon.GetProductDetailsParams{
4041
ProductId: 123456789,
4142
})
4243
if err != nil || resp.StatusCode != http.StatusOK {

client.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,33 @@ type HttpClient interface {
1515

1616
type Client struct {
1717
baseUrl string
18-
ctx context.Context
1918
Options map[string]string
2019

2120
client HttpClient
2221
}
2322

24-
func NewClient(baseUrl string, opts map[string]string) *Client {
23+
func NewClient(client HttpClient, baseUrl string, opts map[string]string) *Client {
2524
return &Client{
2625
Options: opts,
27-
ctx: context.Background(),
28-
client: http.DefaultClient,
26+
client: client,
2927
baseUrl: baseUrl,
3028
}
3129
}
3230

3331
func NewMockClient(handler http.HandlerFunc) *Client {
3432
return &Client{
35-
ctx: context.Background(),
3633
client: NewMockHttpClient(handler),
3734
}
3835
}
3936

40-
func (c Client) newRequest(method string, url string, body interface{}) (*http.Request, error) {
37+
func (c Client) newRequest(ctx context.Context, method string, url string, body interface{}) (*http.Request, error) {
4138
bodyJson, err := json.Marshal(body)
4239
if err != nil {
4340
return nil, err
4441
}
4542

4643
url = c.baseUrl + url
47-
req, err := http.NewRequestWithContext(c.ctx, method, url, bytes.NewBuffer(bodyJson))
44+
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(bodyJson))
4845
if err != nil {
4946
return nil, err
5047
}
@@ -56,8 +53,8 @@ func (c Client) newRequest(method string, url string, body interface{}) (*http.R
5653
return req, nil
5754
}
5855

59-
func (c Client) Request(method string, path string, req, resp interface{}, options map[string]string) (*Response, error) {
60-
httpReq, err := c.newRequest(method, path, req)
56+
func (c Client) Request(ctx context.Context, method string, path string, req, resp interface{}, options map[string]string) (*Response, error) {
57+
httpReq, err := c.newRequest(ctx, method, path, req)
6158
if err != nil {
6259
return nil, err
6360
}

client_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package core
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
7+
"time"
8+
)
9+
10+
const (
11+
testTimeout = 5 * time.Second
612
)
713

814
type TestRequestRequest struct {
@@ -55,7 +61,8 @@ func TestRequest(t *testing.T) {
5561
c := NewMockClient(NewMockHttpHandler(test.statusCode, test.response, test.headers))
5662

5763
respStruct := &TestRequestResponse{}
58-
resp, err := c.Request(http.MethodPost, "/", test.params, respStruct, nil)
64+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
65+
resp, err := c.Request(ctx, http.MethodPost, "/", test.params, respStruct, nil)
5966

6067
if err != nil {
6168
t.Error(err)

ozon/analytics.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56
"time"
67

@@ -94,12 +95,12 @@ type GetAnalyticsDataResultDimension struct {
9495
}
9596

9697
// Specify the period and metrics that are required. The response will contain analytical data grouped by the `dimensions` parameter.
97-
func (c Analytics) GetAnalyticsData(params *GetAnalyticsDataParams) (*GetAnalyticsDataResponse, error) {
98+
func (c Analytics) GetAnalyticsData(ctx context.Context, params *GetAnalyticsDataParams) (*GetAnalyticsDataResponse, error) {
9899
url := "/v1/analytics/data"
99100

100101
resp := &GetAnalyticsDataResponse{}
101102

102-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
103+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
103104
if err != nil {
104105
return nil, err
105106
}
@@ -157,12 +158,12 @@ type GetStocksOnWarehousesResultRow struct {
157158
}
158159

159160
// Report on stocks and products movement at Ozon warehouses
160-
func (c Analytics) GetStocksOnWarehouses(params *GetStocksOnWarehousesParams) (*GetStocksOnWarehousesResponse, error) {
161+
func (c Analytics) GetStocksOnWarehouses(ctx context.Context, params *GetStocksOnWarehousesParams) (*GetStocksOnWarehousesResponse, error) {
161162
url := "/v2/analytics/stock_on_warehouses"
162163

163164
resp := &GetStocksOnWarehousesResponse{}
164165

165-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
166+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
166167
if err != nil {
167168
return nil, err
168169
}

ozon/analytics_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
67

@@ -59,7 +60,8 @@ func TestGetAnalyticsData(t *testing.T) {
5960
for _, test := range tests {
6061
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
6162

62-
resp, err := c.Analytics().GetAnalyticsData(test.params)
63+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
64+
resp, err := c.Analytics().GetAnalyticsData(ctx, test.params)
6365
if err != nil {
6466
t.Error(err)
6567
}
@@ -119,7 +121,8 @@ func TestGetStocksOnWarehouses(t *testing.T) {
119121
for _, test := range tests {
120122
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
121123

122-
resp, err := c.Analytics().GetStocksOnWarehouses(test.params)
124+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
125+
resp, err := c.Analytics().GetStocksOnWarehouses(ctx, test.params)
123126
if err != nil {
124127
t.Error(err)
125128
}

ozon/brands.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56

67
core "github.com/diphantxm/ozon-api-client"
@@ -44,12 +45,12 @@ type ListCertifiedBrandsResultCertificate struct {
4445
}
4546

4647
// List of certified brands
47-
func (c Brands) List(params *ListCertifiedBrandsParams) (*ListCertifiedBrandsResponse, error) {
48+
func (c Brands) List(ctx context.Context, params *ListCertifiedBrandsParams) (*ListCertifiedBrandsResponse, error) {
4849
url := "/v1/brand/company-certification/list"
4950

5051
resp := &ListCertifiedBrandsResponse{}
5152

52-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
53+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
5354
if err != nil {
5455
return nil, err
5556
}

ozon/brands_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
67

@@ -52,7 +53,8 @@ func TestListCertifiedBrands(t *testing.T) {
5253
for _, test := range tests {
5354
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
5455

55-
resp, err := c.Brands().List(test.params)
56+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
57+
resp, err := c.Brands().List(ctx, test.params)
5658
if err != nil {
5759
t.Error(err)
5860
}

ozon/cancellations.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56
"time"
67

@@ -81,12 +82,12 @@ type CancellationInfoState struct {
8182
}
8283

8384
// Method for getting information about a rFBS cancellation request
84-
func (c Cancellations) GetInfo(params *GetCancellationInfoParams) (*GetCancellationInfoResponse, error) {
85+
func (c Cancellations) GetInfo(ctx context.Context, params *GetCancellationInfoParams) (*GetCancellationInfoResponse, error) {
8586
url := "/v1/delivery-method/list"
8687

8788
resp := &GetCancellationInfoResponse{}
8889

89-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
90+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
9091
if err != nil {
9192
return nil, err
9293
}
@@ -153,12 +154,12 @@ type ListCancellationResponseCounters struct {
153154
}
154155

155156
// Method for getting a list of rFBS cancellation requests
156-
func (c Cancellations) List(params *ListCancellationsParams) (*ListCancellationsResponse, error) {
157+
func (c Cancellations) List(ctx context.Context, params *ListCancellationsParams) (*ListCancellationsResponse, error) {
157158
url := "/v1/conditional-cancellation/list"
158159

159160
resp := &ListCancellationsResponse{}
160161

161-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
162+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
162163
if err != nil {
163164
return nil, err
164165
}
@@ -181,12 +182,12 @@ type ApproveRejectCancellationsResponse struct {
181182

182183
// The method allows to approve an rFBS cancellation request in the ON_APPROVAL status.
183184
// The order will be canceled and the money will be returned to the customer
184-
func (c Cancellations) Approve(params *ApproveRejectCancellationsParams) (*ApproveRejectCancellationsResponse, error) {
185+
func (c Cancellations) Approve(ctx context.Context, params *ApproveRejectCancellationsParams) (*ApproveRejectCancellationsResponse, error) {
185186
url := "/v1/conditional-cancellation/approve"
186187

187188
resp := &ApproveRejectCancellationsResponse{}
188189

189-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
190+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
190191
if err != nil {
191192
return nil, err
192193
}
@@ -198,12 +199,12 @@ func (c Cancellations) Approve(params *ApproveRejectCancellationsParams) (*Appro
198199
// The method allows to reject an rFBS cancellation request in the ON_APPROVAL status. Explain your decision in the comment parameter.
199200
//
200201
// The order will remain in the same status and must be delivered to the customer
201-
func (c Cancellations) Reject(params *ApproveRejectCancellationsParams) (*ApproveRejectCancellationsResponse, error) {
202+
func (c Cancellations) Reject(ctx context.Context, params *ApproveRejectCancellationsParams) (*ApproveRejectCancellationsResponse, error) {
202203
url := "/v1/conditional-cancellation/reject"
203204

204205
resp := &ApproveRejectCancellationsResponse{}
205206

206-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
207+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
207208
if err != nil {
208209
return nil, err
209210
}

ozon/cancellations_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56
"testing"
67

@@ -62,7 +63,8 @@ func TestGetCancellationInfo(t *testing.T) {
6263
for _, test := range tests {
6364
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
6465

65-
resp, err := c.Cancellations().GetInfo(test.params)
66+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
67+
resp, err := c.Cancellations().GetInfo(ctx, test.params)
6668
if err != nil {
6769
t.Error(err)
6870
}
@@ -171,7 +173,8 @@ func TestListCancellations(t *testing.T) {
171173
for _, test := range tests {
172174
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
173175

174-
resp, err := c.Cancellations().List(test.params)
176+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
177+
resp, err := c.Cancellations().List(ctx, test.params)
175178
if err != nil {
176179
t.Error(err)
177180
}
@@ -215,7 +218,8 @@ func TestApproveCancellations(t *testing.T) {
215218
for _, test := range tests {
216219
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
217220

218-
resp, err := c.Cancellations().Approve(test.params)
221+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
222+
resp, err := c.Cancellations().Approve(ctx, test.params)
219223
if err != nil {
220224
t.Error(err)
221225
}
@@ -259,7 +263,8 @@ func TestRejectCancellations(t *testing.T) {
259263
for _, test := range tests {
260264
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
261265

262-
resp, err := c.Cancellations().Reject(test.params)
266+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
267+
resp, err := c.Cancellations().Reject(ctx, test.params)
263268
if err != nil {
264269
t.Error(err)
265270
}

ozon/categories.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ozon
22

33
import (
4+
"context"
45
"net/http"
56

67
core "github.com/diphantxm/ozon-api-client"
@@ -40,12 +41,12 @@ type GetProductTreeResult struct {
4041
// New products can be created in the last level categories only.
4142
// This means that you need to match these particular categories with the categories of your site.
4243
// It is not possible to create categories by user request
43-
func (c Categories) Tree(params *GetProductTreeParams) (*GetProductTreeResponse, error) {
44+
func (c Categories) Tree(ctx context.Context, params *GetProductTreeParams) (*GetProductTreeResponse, error) {
4445
url := "/v2/category/tree"
4546

4647
resp := &GetProductTreeResponse{}
4748

48-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
49+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
4950
if err != nil {
5051
return nil, err
5152
}
@@ -134,12 +135,12 @@ type GetCategoryAttributesResultAttribute struct {
134135
// You can check whether the attribute has a nested directory by the `dictionary_id` parameter.
135136
// The 0 value means there is no directory. If the value is different, then there are directories.
136137
// You can get them using the `/v2/category/attribute/values` method
137-
func (c Categories) Attributes(params *GetCategoryAttributesParams) (*GetCategoryAttributesResponse, error) {
138+
func (c Categories) Attributes(ctx context.Context, params *GetCategoryAttributesParams) (*GetCategoryAttributesResponse, error) {
138139
url := "/v3/category/attribute"
139140

140141
resp := &GetCategoryAttributesResponse{}
141142

142-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
143+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
143144
if err != nil {
144145
return nil, err
145146
}
@@ -187,12 +188,12 @@ type GetAttributeDictionaryResult struct {
187188

188189
// You can use the `/v3/category/attribute` method to check if an attribute has a nested directory.
189190
// If there are directories, get them using this method
190-
func (c Categories) AttributesDictionary(params *GetAttributeDictionaryParams) (*GetAttributeDictionaryResponse, error) {
191+
func (c Categories) AttributesDictionary(ctx context.Context, params *GetAttributeDictionaryParams) (*GetAttributeDictionaryResponse, error) {
191192
url := "/v2/category/attribute/values"
192193

193194
resp := &GetAttributeDictionaryResponse{}
194195

195-
response, err := c.client.Request(http.MethodPost, url, params, resp, nil)
196+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
196197
if err != nil {
197198
return nil, err
198199
}

0 commit comments

Comments
 (0)