Skip to content

Commit 090b2af

Browse files
authored
added /v1/description-category/attribute/values/search support (#100)
1 parent 823386e commit 090b2af

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

ENDPOINTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [x] Product category tree
55
- [x] Category characteristics list
66
- [x] Characteristics value directory
7+
- [x] Search characteristics value directory
78

89
## Uploading and updating products
910
- [x] Create or update a product

ozon/categories.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,63 @@ func (c *Categories) AttributesDictionary(ctx context.Context, params *GetAttrib
224224

225225
return resp, nil
226226
}
227+
228+
type SearchAttributeDictionaryParams struct {
229+
// Characteristics identifier
230+
AttributeId int64 `json:"attribute_id"`
231+
232+
// Category identifier
233+
DescriptionCategoryId int64 `json:"description_category_id"`
234+
235+
// The value to be searched for
236+
// - minimum—2 characters
237+
Value string `json:"value"`
238+
239+
// Number of values in the response:
240+
//
241+
// - maximum—100,
242+
// - minimum—1.
243+
Limit int64 `json:"limit,omitempty"`
244+
245+
// Product type identifier
246+
TypeId int64 `json:"type_id"`
247+
}
248+
249+
type SearchAttributeDictionaryResponse struct {
250+
core.CommonResponse
251+
252+
// Characteristic values
253+
Result []SearchAttributeDictionaryResult `json:"result"`
254+
}
255+
256+
type SearchAttributeDictionaryResult struct {
257+
// Characteristic value identifier
258+
Id int64 `json:"id"`
259+
260+
// Additional description
261+
Info string `json:"info"`
262+
263+
// Image link
264+
Picture string `json:"picture"`
265+
266+
// Product characteristic value
267+
Value string `json:"value"`
268+
}
269+
270+
// Returns found characteristics value directory.
271+
//
272+
// To check if an attribute has a nested directory,
273+
// use the `/v1/description-category/attribute` method.
274+
func (c *Categories) SearchAttributesDictionary(ctx context.Context, params *SearchAttributeDictionaryParams) (*SearchAttributeDictionaryResponse, error) {
275+
url := "/v1/description-category/attribute/values/search"
276+
277+
resp := &SearchAttributeDictionaryResponse{}
278+
279+
response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil)
280+
if err != nil {
281+
return nil, err
282+
}
283+
response.CopyCommonResponse(&resp.CommonResponse)
284+
285+
return resp, nil
286+
}

ozon/categories_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,71 @@ func TestGetAttributeDictionary(t *testing.T) {
203203
}
204204
}
205205
}
206+
207+
func TestSearchAttributeDictionary(t *testing.T) {
208+
t.Parallel()
209+
210+
tests := []struct {
211+
statusCode int
212+
headers map[string]string
213+
params *SearchAttributeDictionaryParams
214+
response string
215+
}{
216+
// Test Ok
217+
{
218+
http.StatusOK,
219+
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
220+
&SearchAttributeDictionaryParams{
221+
AttributeId: 123456,
222+
DescriptionCategoryId: 12,
223+
Value: "34",
224+
Limit: 5,
225+
TypeId: 6,
226+
},
227+
`{
228+
"has_next": true,
229+
"result": [
230+
{
231+
"id": 0,
232+
"info": "string",
233+
"picture": "string",
234+
"value": "string"
235+
}
236+
]
237+
}`,
238+
},
239+
// Test No Client-Id or Api-Key
240+
{
241+
http.StatusUnauthorized,
242+
map[string]string{},
243+
&SearchAttributeDictionaryParams{},
244+
`{
245+
"code": 16,
246+
"message": "Client-Id and Api-Key headers are required"
247+
}`,
248+
},
249+
}
250+
251+
for _, test := range tests {
252+
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
253+
254+
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
255+
resp, err := c.Categories().SearchAttributesDictionary(ctx, test.params)
256+
if err != nil {
257+
t.Error(err)
258+
continue
259+
}
260+
261+
compareJsonResponse(t, test.response, &GetAttributeDictionaryResponse{})
262+
263+
if resp.StatusCode != test.statusCode {
264+
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
265+
}
266+
267+
if resp.StatusCode == http.StatusOK {
268+
if len(resp.Result) > int(test.params.Limit) {
269+
t.Errorf("Length of response result is bigger than limit")
270+
}
271+
}
272+
}
273+
}

0 commit comments

Comments
 (0)