From 090b2afb63133cad56017b8de91ad749ed00e4cb Mon Sep 17 00:00:00 2001 From: s1berc0de <141402384+s1berc0de@users.noreply.github.com> Date: Thu, 29 Aug 2024 21:53:31 +0700 Subject: [PATCH] added /v1/description-category/attribute/values/search support (#100) --- ENDPOINTS.md | 1 + ozon/categories.go | 60 ++++++++++++++++++++++++++++++++++++ ozon/categories_test.go | 68 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) diff --git a/ENDPOINTS.md b/ENDPOINTS.md index 71c4ef4..5143289 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -4,6 +4,7 @@ - [x] Product category tree - [x] Category characteristics list - [x] Characteristics value directory +- [x] Search characteristics value directory ## Uploading and updating products - [x] Create or update a product diff --git a/ozon/categories.go b/ozon/categories.go index b933b8e..c85251b 100644 --- a/ozon/categories.go +++ b/ozon/categories.go @@ -224,3 +224,63 @@ func (c *Categories) AttributesDictionary(ctx context.Context, params *GetAttrib return resp, nil } + +type SearchAttributeDictionaryParams struct { + // Characteristics identifier + AttributeId int64 `json:"attribute_id"` + + // Category identifier + DescriptionCategoryId int64 `json:"description_category_id"` + + // The value to be searched for + // - minimum—2 characters + Value string `json:"value"` + + // Number of values in the response: + // + // - maximum—100, + // - minimum—1. + Limit int64 `json:"limit,omitempty"` + + // Product type identifier + TypeId int64 `json:"type_id"` +} + +type SearchAttributeDictionaryResponse struct { + core.CommonResponse + + // Characteristic values + Result []SearchAttributeDictionaryResult `json:"result"` +} + +type SearchAttributeDictionaryResult struct { + // Characteristic value identifier + Id int64 `json:"id"` + + // Additional description + Info string `json:"info"` + + // Image link + Picture string `json:"picture"` + + // Product characteristic value + Value string `json:"value"` +} + +// Returns found characteristics value directory. +// +// To check if an attribute has a nested directory, +// use the `/v1/description-category/attribute` method. +func (c *Categories) SearchAttributesDictionary(ctx context.Context, params *SearchAttributeDictionaryParams) (*SearchAttributeDictionaryResponse, error) { + url := "/v1/description-category/attribute/values/search" + + resp := &SearchAttributeDictionaryResponse{} + + response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil) + if err != nil { + return nil, err + } + response.CopyCommonResponse(&resp.CommonResponse) + + return resp, nil +} diff --git a/ozon/categories_test.go b/ozon/categories_test.go index 72f688c..c8c7018 100644 --- a/ozon/categories_test.go +++ b/ozon/categories_test.go @@ -203,3 +203,71 @@ func TestGetAttributeDictionary(t *testing.T) { } } } + +func TestSearchAttributeDictionary(t *testing.T) { + t.Parallel() + + tests := []struct { + statusCode int + headers map[string]string + params *SearchAttributeDictionaryParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &SearchAttributeDictionaryParams{ + AttributeId: 123456, + DescriptionCategoryId: 12, + Value: "34", + Limit: 5, + TypeId: 6, + }, + `{ + "has_next": true, + "result": [ + { + "id": 0, + "info": "string", + "picture": "string", + "value": "string" + } + ] + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &SearchAttributeDictionaryParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, + } + + for _, test := range tests { + c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers)) + + ctx, _ := context.WithTimeout(context.Background(), testTimeout) + resp, err := c.Categories().SearchAttributesDictionary(ctx, test.params) + if err != nil { + t.Error(err) + continue + } + + compareJsonResponse(t, test.response, &GetAttributeDictionaryResponse{}) + + if resp.StatusCode != test.statusCode { + t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) + } + + if resp.StatusCode == http.StatusOK { + if len(resp.Result) > int(test.params.Limit) { + t.Errorf("Length of response result is bigger than limit") + } + } + } +}