add methods for ozon attributes and characteristics
This commit is contained in:
		
							
								
								
									
										194
									
								
								ozon/categories.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								ozon/categories.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,194 @@
 | 
			
		||||
package ozon
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	core "github.com/diphantxm/ozon-api-client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Categories struct {
 | 
			
		||||
	client *core.Client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetProductTreeParams struct {
 | 
			
		||||
	// Category identifier
 | 
			
		||||
	CategoryId int64 `json:"category_id"`
 | 
			
		||||
 | 
			
		||||
	// Response language
 | 
			
		||||
	Language string `json:"language" default:"DEFAULT"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetProductTreeResponse struct {
 | 
			
		||||
	core.CommonResponse
 | 
			
		||||
 | 
			
		||||
	// Category list
 | 
			
		||||
	Result []struct {
 | 
			
		||||
		// Category identifier
 | 
			
		||||
		CategoryId int64 `json:"category_id"`
 | 
			
		||||
 | 
			
		||||
		// Subcategory tree
 | 
			
		||||
		Children []GetProductTreeResponse `json:"children"`
 | 
			
		||||
 | 
			
		||||
		// Category name
 | 
			
		||||
		Title string `json:"title"`
 | 
			
		||||
	} `json:"result"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Returns product categories in the tree view.
 | 
			
		||||
// New products can be created in the last level categories only.
 | 
			
		||||
// This means that you need to match these particular categories with the categories of your site.
 | 
			
		||||
// It is not possible to create categories by user request
 | 
			
		||||
func (c Categories) Tree(params *GetProductTreeParams) (*GetProductTreeResponse, error) {
 | 
			
		||||
	url := "/v2/category/tree"
 | 
			
		||||
 | 
			
		||||
	resp := &GetProductTreeResponse{}
 | 
			
		||||
 | 
			
		||||
	response, err := c.client.Request(http.MethodPost, url, params, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	response.CopyCommonResponse(&resp.CommonResponse)
 | 
			
		||||
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetCategoryAttributesParams struct {
 | 
			
		||||
	// Filter by characteristics
 | 
			
		||||
	AttributeType string `json:"attribute_type" default:"ALL"`
 | 
			
		||||
 | 
			
		||||
	// Category identifier
 | 
			
		||||
	CategoryId []int64 `json:"category_id"`
 | 
			
		||||
 | 
			
		||||
	// Response language
 | 
			
		||||
	Language string `json:"language" default:"DEFAULT"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetCategoryAttributesResponse struct {
 | 
			
		||||
	core.CommonResponse
 | 
			
		||||
 | 
			
		||||
	// Method result
 | 
			
		||||
	Result []struct {
 | 
			
		||||
		// Array of product characteristics
 | 
			
		||||
		Attributes []struct {
 | 
			
		||||
			// Indication that the dictionary attribute values depend on the category:
 | 
			
		||||
			//   - true — the attribute has its own set of values for each category.
 | 
			
		||||
			//   - false — the attribute has the same set of values for all categories
 | 
			
		||||
			CategoryDependent bool `json:"category_dependent"`
 | 
			
		||||
 | 
			
		||||
			// Characteristic description
 | 
			
		||||
			Description string `json:"description"`
 | 
			
		||||
 | 
			
		||||
			// Directory identifier
 | 
			
		||||
			DictionaryId int64 `json:"dictionary_id"`
 | 
			
		||||
 | 
			
		||||
			// Characteristics group identifier
 | 
			
		||||
			GroupId int64 `json:"group_id"`
 | 
			
		||||
 | 
			
		||||
			// Characteristics group name
 | 
			
		||||
			GroupName string `json:"group_name"`
 | 
			
		||||
 | 
			
		||||
			// Document generation task number
 | 
			
		||||
			Id int64 `json:"id"`
 | 
			
		||||
 | 
			
		||||
			// Indicates that the attribute is aspect. An aspect attribute is a characteristic that distinguishes products of the same model.
 | 
			
		||||
			//
 | 
			
		||||
			// For example, clothes and shoes of the same model may have different colors and sizes. That is, color and size are aspect attributes.
 | 
			
		||||
			//
 | 
			
		||||
			// Values description:
 | 
			
		||||
			//   - true — the attribute is aspect and cannot be changed after the products are delivered to the warehouse or sold from the seller's warehouse.
 | 
			
		||||
			//   - false — the attribute is not aspect and can be changed at any time
 | 
			
		||||
			IsAspect bool `json:"is_aspect"`
 | 
			
		||||
 | 
			
		||||
			// Indicates that the characteristic is a set of values:
 | 
			
		||||
			//   - true — the characteristic is a set of values,
 | 
			
		||||
			//   - false — the characteristic consists of a single value
 | 
			
		||||
			IsCollection bool `json:"is_collection"`
 | 
			
		||||
 | 
			
		||||
			// Indicates that the characteristic is mandatory:
 | 
			
		||||
			//   - true — a mandatory characteristic,
 | 
			
		||||
			//   - false — you can leave the characteristic out
 | 
			
		||||
			IsRequired bool `json:"is_required"`
 | 
			
		||||
 | 
			
		||||
			// Name
 | 
			
		||||
			Name string `json:"name"`
 | 
			
		||||
 | 
			
		||||
			// Characteristic type
 | 
			
		||||
			Type string `json:"type"`
 | 
			
		||||
		} `json:"attributes"`
 | 
			
		||||
 | 
			
		||||
		// Category identifier
 | 
			
		||||
		CategoryId int64 `json:"category_id"`
 | 
			
		||||
	} `json:"result"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Getting characteristics for specified product category.
 | 
			
		||||
//
 | 
			
		||||
// Pass up to 20 category identifiers in the `category_id` list.
 | 
			
		||||
//
 | 
			
		||||
// You can check whether the attribute has a nested directory by the `dictionary_id` parameter.
 | 
			
		||||
// The 0 value means there is no directory. If the value is different, then there are directories.
 | 
			
		||||
// You can get them using the `/v2/category/attribute/values` method
 | 
			
		||||
func (c Categories) Attributes(params *GetCategoryAttributesParams) (*GetCategoryAttributesResponse, error) {
 | 
			
		||||
	url := "/v3/category/attribute"
 | 
			
		||||
 | 
			
		||||
	resp := &GetCategoryAttributesResponse{}
 | 
			
		||||
 | 
			
		||||
	response, err := c.client.Request(http.MethodPost, url, params, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	response.CopyCommonResponse(&resp.CommonResponse)
 | 
			
		||||
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetAttributeDictionaryParams struct {
 | 
			
		||||
	// Characteristics identifier
 | 
			
		||||
	AttributeId int64 `json:"attribute_id"`
 | 
			
		||||
 | 
			
		||||
	// Category identifier
 | 
			
		||||
	CategoryId int64 `json:"category_id"`
 | 
			
		||||
 | 
			
		||||
	// Response language
 | 
			
		||||
	// The default language is Russian
 | 
			
		||||
	Language string `json:"language" default:"DEFAULT"`
 | 
			
		||||
 | 
			
		||||
	LastValueId int64 `json:"last_value_id"`
 | 
			
		||||
 | 
			
		||||
	// Number of values in the response:
 | 
			
		||||
	//   - maximum — 5000
 | 
			
		||||
	//   - minimum — 1
 | 
			
		||||
	Limit int64 `json:"limit"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GetAttributeDictionaryResponse struct {
 | 
			
		||||
	core.CommonResponse
 | 
			
		||||
 | 
			
		||||
	HasNext bool `json:"has_next"`
 | 
			
		||||
 | 
			
		||||
	// Method result
 | 
			
		||||
	Result []struct {
 | 
			
		||||
		Id      int64  `json:"id"`
 | 
			
		||||
		Info    string `json:"info"`
 | 
			
		||||
		Picture string `json:"picture"`
 | 
			
		||||
 | 
			
		||||
		// Product characteristic value
 | 
			
		||||
		Value string `json:"value"`
 | 
			
		||||
	} `json:"result"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// You can use the `/v3/category/attribute` method to check if an attribute has a nested directory.
 | 
			
		||||
// If there are directories, get them using this method
 | 
			
		||||
func (c Categories) AttributesDictionary(params *GetAttributeDictionaryParams) (*GetAttributeDictionaryResponse, error) {
 | 
			
		||||
	url := "/v2/category/attribute/values"
 | 
			
		||||
 | 
			
		||||
	resp := &GetAttributeDictionaryResponse{}
 | 
			
		||||
 | 
			
		||||
	response, err := c.client.Request(http.MethodPost, url, params, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	response.CopyCommonResponse(&resp.CommonResponse)
 | 
			
		||||
 | 
			
		||||
	return resp, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										217
									
								
								ozon/categories_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										217
									
								
								ozon/categories_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,217 @@
 | 
			
		||||
package ozon
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	core "github.com/diphantxm/ozon-api-client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestGetProductTree(t *testing.T) {
 | 
			
		||||
	t.Parallel()
 | 
			
		||||
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		statusCode int
 | 
			
		||||
		headers    map[string]string
 | 
			
		||||
		params     *GetProductTreeParams
 | 
			
		||||
		response   string
 | 
			
		||||
	}{
 | 
			
		||||
		// Test Ok
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusOK,
 | 
			
		||||
			map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
 | 
			
		||||
			&GetProductTreeParams{
 | 
			
		||||
				CategoryId: 17034410,
 | 
			
		||||
			},
 | 
			
		||||
			`{
 | 
			
		||||
				"result": [
 | 
			
		||||
				  {
 | 
			
		||||
					"category_id": 17034410,
 | 
			
		||||
					"title": "Развивающие игрушки",
 | 
			
		||||
					"children": []
 | 
			
		||||
				  }
 | 
			
		||||
				]
 | 
			
		||||
			}`,
 | 
			
		||||
		},
 | 
			
		||||
		// Test No Client-Id or Api-Key
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusUnauthorized,
 | 
			
		||||
			map[string]string{},
 | 
			
		||||
			&GetProductTreeParams{},
 | 
			
		||||
			`{
 | 
			
		||||
				"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))
 | 
			
		||||
 | 
			
		||||
		resp, err := c.Categories().Tree(test.params)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Error(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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) > 0 {
 | 
			
		||||
				if resp.Result[0].CategoryId != test.params.CategoryId {
 | 
			
		||||
					t.Errorf("First category ids in request and response are not equal")
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGetCategoryAttributes(t *testing.T) {
 | 
			
		||||
	t.Parallel()
 | 
			
		||||
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		statusCode int
 | 
			
		||||
		headers    map[string]string
 | 
			
		||||
		params     *GetCategoryAttributesParams
 | 
			
		||||
		response   string
 | 
			
		||||
	}{
 | 
			
		||||
		// Test Ok
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusOK,
 | 
			
		||||
			map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
 | 
			
		||||
			&GetCategoryAttributesParams{
 | 
			
		||||
				CategoryId: []int64{17034410},
 | 
			
		||||
			},
 | 
			
		||||
			`{
 | 
			
		||||
				"result": [
 | 
			
		||||
				  {
 | 
			
		||||
					"category_id": 17034410,
 | 
			
		||||
					"attributes": [
 | 
			
		||||
					  {
 | 
			
		||||
						"id": 85,
 | 
			
		||||
						"name": "Бренд",
 | 
			
		||||
						"description": "Укажите наименование бренда, под которым произведен товар. Если товар не имеет бренда, используйте значение \"Нет бренда\"",
 | 
			
		||||
						"type": "String",
 | 
			
		||||
						"is_collection": false,
 | 
			
		||||
						"is_required": true,
 | 
			
		||||
						"group_id": 0,
 | 
			
		||||
						"group_name": "",
 | 
			
		||||
						"dictionary_id": 28732849
 | 
			
		||||
					  }
 | 
			
		||||
					]
 | 
			
		||||
				  }
 | 
			
		||||
				]
 | 
			
		||||
			}`,
 | 
			
		||||
		},
 | 
			
		||||
		// Test No Client-Id or Api-Key
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusUnauthorized,
 | 
			
		||||
			map[string]string{},
 | 
			
		||||
			&GetCategoryAttributesParams{},
 | 
			
		||||
			`{
 | 
			
		||||
				"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))
 | 
			
		||||
 | 
			
		||||
		resp, err := c.Categories().Attributes(test.params)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Error(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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) != len(test.params.CategoryId) {
 | 
			
		||||
				t.Errorf("Length of categories in request and response are not equal")
 | 
			
		||||
			}
 | 
			
		||||
			if len(resp.Result) > 0 {
 | 
			
		||||
				if resp.Result[0].CategoryId != test.params.CategoryId[0] {
 | 
			
		||||
					t.Errorf("Category ids in request and response are not equal")
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGetAttributeDictionary(t *testing.T) {
 | 
			
		||||
	t.Parallel()
 | 
			
		||||
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		statusCode int
 | 
			
		||||
		headers    map[string]string
 | 
			
		||||
		params     *GetAttributeDictionaryParams
 | 
			
		||||
		response   string
 | 
			
		||||
	}{
 | 
			
		||||
		// Test Ok
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusOK,
 | 
			
		||||
			map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
 | 
			
		||||
			&GetAttributeDictionaryParams{
 | 
			
		||||
				AttributeId: 10096,
 | 
			
		||||
				CategoryId:  17028968,
 | 
			
		||||
				LastValueId: 0,
 | 
			
		||||
				Limit:       3,
 | 
			
		||||
			},
 | 
			
		||||
			`{
 | 
			
		||||
				"result": [
 | 
			
		||||
				  {
 | 
			
		||||
					"id": 61571,
 | 
			
		||||
					"value": "белый",
 | 
			
		||||
					"info": "",
 | 
			
		||||
					"picture": ""
 | 
			
		||||
				  },
 | 
			
		||||
				  {
 | 
			
		||||
					"id": 61572,
 | 
			
		||||
					"value": "прозрачный",
 | 
			
		||||
					"info": "",
 | 
			
		||||
					"picture": ""
 | 
			
		||||
				  },
 | 
			
		||||
				  {
 | 
			
		||||
					"id": 61573,
 | 
			
		||||
					"value": "бежевый",
 | 
			
		||||
					"info": "",
 | 
			
		||||
					"picture": ""
 | 
			
		||||
				  }
 | 
			
		||||
				],
 | 
			
		||||
				"has_next": true
 | 
			
		||||
			}`,
 | 
			
		||||
		},
 | 
			
		||||
		// Test No Client-Id or Api-Key
 | 
			
		||||
		{
 | 
			
		||||
			http.StatusUnauthorized,
 | 
			
		||||
			map[string]string{},
 | 
			
		||||
			&GetAttributeDictionaryParams{},
 | 
			
		||||
			`{
 | 
			
		||||
				"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))
 | 
			
		||||
 | 
			
		||||
		resp, err := c.Categories().AttributesDictionary(test.params)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Error(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -24,6 +24,7 @@ type Client struct {
 | 
			
		||||
	returns       *Returns
 | 
			
		||||
	reports       *Reports
 | 
			
		||||
	cancellations *Cancellations
 | 
			
		||||
	categories    *Categories
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c Client) Analytics() *Analytics {
 | 
			
		||||
@@ -70,6 +71,10 @@ func (c Client) Cancellations() *Cancellations {
 | 
			
		||||
	return c.cancellations
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c Client) Categories() *Categories {
 | 
			
		||||
	return c.categories
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewClient(clientId, apiKey string) *Client {
 | 
			
		||||
	coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{
 | 
			
		||||
		"Client-Id": clientId,
 | 
			
		||||
@@ -89,6 +94,7 @@ func NewClient(clientId, apiKey string) *Client {
 | 
			
		||||
		returns:       &Returns{client: coreClient},
 | 
			
		||||
		reports:       &Reports{client: coreClient},
 | 
			
		||||
		cancellations: &Cancellations{client: coreClient},
 | 
			
		||||
		categories:    &Categories{client: coreClient},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -108,5 +114,6 @@ func NewMockClient(handler http.HandlerFunc) *Client {
 | 
			
		||||
		returns:       &Returns{client: coreClient},
 | 
			
		||||
		reports:       &Reports{client: coreClient},
 | 
			
		||||
		cancellations: &Cancellations{client: coreClient},
 | 
			
		||||
		categories:    &Categories{client: coreClient},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user