Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added /v1/description-category/attribute/values/search support #100

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ENDPOINTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions ozon/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
68 changes: 68 additions & 0 deletions ozon/categories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
Loading