refactoring. put methods to individual structures

This commit is contained in:
diPhantxm
2023-03-15 21:38:17 +03:00
parent 2e60a17c56
commit 2566f85da3
17 changed files with 148 additions and 55 deletions

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Analytics struct {
client *core.Client
}
type GetAnalyticsDataParams struct {
// Date from which the data will be in the report
DateFrom time.Time `json:"date_from"`
@@ -90,7 +94,7 @@ type GetAnalyticsDataResponse struct {
}
// Specify the period and metrics that are required. The response will contain analytical data grouped by the `dimensions` parameter.
func (c Client) GetAnalyticsData(params *GetAnalyticsDataParams) (*GetAnalyticsDataResponse, error) {
func (c Analytics) GetAnalyticsData(params *GetAnalyticsDataParams) (*GetAnalyticsDataResponse, error) {
url := "/v1/analytics/data"
resp := &GetAnalyticsDataResponse{}
@@ -152,7 +156,7 @@ type GetStocksOnWarehousesResponse struct {
}
// Report on stocks and products movement at Ozon warehouses
func (c Client) GetStocksOnWarehouses(params *GetStocksOnWarehousesParams) (*GetStocksOnWarehousesResponse, error) {
func (c Analytics) GetStocksOnWarehouses(params *GetStocksOnWarehousesParams) (*GetStocksOnWarehousesResponse, error) {
url := "/v2/analytics/stock_on_warehouses"
resp := &GetStocksOnWarehousesResponse{}

View File

@@ -57,7 +57,7 @@ func TestGetAnalyticsData(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetAnalyticsData(test.params)
resp, err := c.Analytics().GetAnalyticsData(test.params)
if err != nil {
t.Error(err)
}
@@ -115,7 +115,7 @@ func TestGetStocksOnWarehouses(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetStocksOnWarehouses(test.params)
resp, err := c.Analytics().GetStocksOnWarehouses(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type FBO struct {
client *core.Client
}
type GetFBOShipmentsListParams struct {
// Sorting direction
Direction string `json:"dir"`
@@ -151,7 +155,7 @@ type GetFBOShipmentsListResponse struct {
}
// Returns a list of shipments for a specified period of time. You can additionally filter the shipments by their status
func (c Client) GetFBOShipmentsList(params *GetFBOShipmentsListParams) (*GetFBOShipmentsListResponse, error) {
func (c FBO) GetFBOShipmentsList(params *GetFBOShipmentsListParams) (*GetFBOShipmentsListResponse, error) {
url := "/v2/posting/fbo/list"
resp := &GetFBOShipmentsListResponse{}

View File

@@ -131,7 +131,7 @@ func TestGetFBOShipmentsList(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetFBOShipmentsList(test.params)
resp, err := c.FBO().GetFBOShipmentsList(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type FBS struct {
client *core.Client
}
type ListUnprocessedShipmentsParams struct {
Direction string `json:"dir"`
Filter ListUnprocessedShipmentsFilter `json:"filter"`
@@ -190,7 +194,7 @@ type FinancialDataProduct struct {
TotalDiscountValue float64 `json:"total_discount_value"`
}
func (c Client) ListUnprocessedShipments(params *ListUnprocessedShipmentsParams) (*ListUnprocessedShipmentsResponse, error) {
func (c FBS) ListUnprocessedShipments(params *ListUnprocessedShipmentsParams) (*ListUnprocessedShipmentsResponse, error) {
url := "/v3/posting/fbs/unfulfilled/list"
resp := &ListUnprocessedShipmentsResponse{}
@@ -289,7 +293,7 @@ type GetFBSShipmentsListResponse struct {
// You can filter shipments by their status. The list of available statuses is specified in the description of the filter.status parameter.
//
// The true value of the has_next parameter in the response means there is not the entire array of shipments in the response. To get information on the remaining shipments, make a new request with a different offset value.
func (c Client) GetFBSShipmentsList(params *GetFBSShipmentsListParams) (*GetFBSShipmentsListResponse, error) {
func (c FBS) GetFBSShipmentsList(params *GetFBSShipmentsListParams) (*GetFBSShipmentsListResponse, error) {
url := "/v3/posting/fbs/list"
resp := &GetFBSShipmentsListResponse{}
@@ -358,7 +362,7 @@ type PackOrderResponse struct {
// Differs from /v2/posting/fbs/ship by the presence of the field exemplar_info in the request.
//
// If necessary, specify the number of the cargo customs declaration in the gtd parameter. If it is missing, pass the value is_gtd_absent = true
func (c Client) PackOrder(params *PackOrderParams) (*PackOrderResponse, error) {
func (c FBS) PackOrder(params *PackOrderParams) (*PackOrderResponse, error) {
url := "/v4/posting/fbs/ship"
resp := &PackOrderResponse{}

View File

@@ -165,7 +165,7 @@ func TestListUnprocessedShipments(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.ListUnprocessedShipments(test.params)
resp, err := c.FBS().ListUnprocessedShipments(test.params)
if err != nil {
t.Error(err)
}
@@ -274,7 +274,7 @@ func TestGetFBSShipmentsList(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetFBSShipmentsList(test.params)
resp, err := c.FBS().GetFBSShipmentsList(test.params)
if err != nil {
t.Error(err)
}
@@ -333,7 +333,7 @@ func TestPackOrder(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.PackOrder(test.params)
resp, err := c.FBS().PackOrder(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Finance struct {
client *core.Client
}
type ReportOnSoldProductsParams struct {
// Time period in the `YYYY-MM` format
Date string `json:"date"`
@@ -141,7 +145,7 @@ type ReportOnSoldProductsResponse struct {
// Returns information on products sold and returned within a month. Canceled or non-purchased products are not included.
//
// Report is returned no later than the 5th day of the next month
func (c Client) ReportOnSoldProducts(params *ReportOnSoldProductsParams) (*ReportOnSoldProductsResponse, error) {
func (c Finance) ReportOnSoldProducts(params *ReportOnSoldProductsParams) (*ReportOnSoldProductsResponse, error) {
url := "/v1/finance/realization"
resp := &ReportOnSoldProductsResponse{}
@@ -229,7 +233,7 @@ type GetTotalTransactionsSumResponse struct {
}
// Returns total sums for transactions for specified period
func (c Client) GetTotalTransactionsSum(params *GetTotalTransactionsSumParams) (*GetTotalTransactionsSumResponse, error) {
func (c Finance) GetTotalTransactionsSum(params *GetTotalTransactionsSumParams) (*GetTotalTransactionsSumResponse, error) {
url := "/v3/finance/transaction/totals"
resp := &GetTotalTransactionsSumResponse{}

View File

@@ -88,7 +88,7 @@ func TestReportOnSoldProducts(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.ReportOnSoldProducts(test.params)
resp, err := c.Finance().ReportOnSoldProducts(test.params)
if err != nil {
t.Error(err)
}
@@ -154,7 +154,7 @@ func TestGetTotalTransactionsSum(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetTotalTransactionsSum(test.params)
resp, err := c.Finance().GetTotalTransactionsSum(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -12,19 +12,80 @@ const (
type Client struct {
client *core.Client
analytics *Analytics
fbo *FBO
fbs *FBS
finance *Finance
products *Products
promotions *Promotions
rating *Rating
warehouses *Warehouses
}
func (c Client) Analytics() *Analytics {
return c.analytics
}
func (c Client) FBO() *FBO {
return c.fbo
}
func (c Client) FBS() *FBS {
return c.fbs
}
func (c Client) Finance() *Finance {
return c.finance
}
func (c Client) Products() *Products {
return c.products
}
func (c Client) Promotions() *Promotions {
return c.promotions
}
func (c Client) Rating() *Rating {
return c.rating
}
func (c Client) Warehouses() *Warehouses {
return c.warehouses
}
func NewClient(clientId, apiKey string) *Client {
coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{
"Client-Id": clientId,
"Api-Key": apiKey,
})
return &Client{
client: core.NewClient(DefaultAPIBaseUrl, map[string]string{
"Client-Id": clientId,
"Api-Key": apiKey,
}),
client: coreClient,
analytics: &Analytics{client: coreClient},
fbo: &FBO{client: coreClient},
fbs: &FBS{client: coreClient},
finance: &Finance{client: coreClient},
products: &Products{client: coreClient},
promotions: &Promotions{client: coreClient},
rating: &Rating{client: coreClient},
warehouses: &Warehouses{client: coreClient},
}
}
func NewMockClient(handler http.HandlerFunc) *Client {
coreClient := core.NewMockClient(handler)
return &Client{
client: core.NewMockClient(handler),
client: coreClient,
analytics: &Analytics{client: coreClient},
fbo: &FBO{client: coreClient},
fbs: &FBS{client: coreClient},
finance: &Finance{client: coreClient},
products: &Products{client: coreClient},
promotions: &Promotions{client: coreClient},
rating: &Rating{client: coreClient},
warehouses: &Warehouses{client: coreClient},
}
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Products struct {
client *core.Client
}
type GetStocksInfoParams struct {
// Identifier of the last value on the page. Leave this field blank in the first request.
//
@@ -72,7 +76,7 @@ type GetStocksInfoResponse struct {
// * how many items are available,
//
// * how many are reserved by customers.
func (c Client) GetStocksInfo(params *GetStocksInfoParams) (*GetStocksInfoResponse, error) {
func (c Products) GetStocksInfo(params *GetStocksInfoParams) (*GetStocksInfoResponse, error) {
url := "/v3/product/info/stocks"
resp := &GetStocksInfoResponse{}
@@ -350,7 +354,7 @@ type GetProductDetailsResponseItemError struct {
}
// Get product details
func (c Client) GetProductDetails(params *GetProductDetailsParams) (*GetProductDetailsResponse, error) {
func (c Products) GetProductDetails(params *GetProductDetailsParams) (*GetProductDetailsResponse, error) {
url := "/v2/product/info"
resp := &GetProductDetailsResponse{}
@@ -411,7 +415,7 @@ type UpdateStocksResponse struct {
// With one request you can change the availability for 100 products. You can send up to 80 requests in a minute.
//
// Availability can only be set after the product status has been changed to processed.
func (c Client) UpdateStocks(params *UpdateStocksParams) (*UpdateStocksResponse, error) {
func (c Products) UpdateStocks(params *UpdateStocksParams) (*UpdateStocksResponse, error) {
url := "/v1/product/import/stocks"
resp := &UpdateStocksResponse{}
@@ -481,7 +485,7 @@ type UpdateQuantityStockProductsResponse struct {
// Availability can only be set after the product status has been changed to processed.
//
// Bulky products stock can only be updated in the warehouses for bulky products.
func (c Client) UpdateQuantityStockProducts(params *UpdateQuantityStockProductsParams) (*UpdateQuantityStockProductsResponse, error) {
func (c Products) UpdateQuantityStockProducts(params *UpdateQuantityStockProductsParams) (*UpdateQuantityStockProductsResponse, error) {
url := "/v2/products/stocks"
resp := &UpdateQuantityStockProductsResponse{}
@@ -530,7 +534,7 @@ type StocksInSellersWarehouseResponse struct {
}
// Get stocks in seller's warehouse
func (c Client) StocksInSellersWarehouse(params *StocksInSellersWarehouseParams) (*StocksInSellersWarehouseResponse, error) {
func (c Products) StocksInSellersWarehouse(params *StocksInSellersWarehouseParams) (*StocksInSellersWarehouseResponse, error) {
url := "/v1/product/info/stocks-by-warehouse/fbs"
resp := &StocksInSellersWarehouseResponse{}
@@ -603,7 +607,7 @@ type UpdatePricesResponse struct {
// To reset old_price or premium_price set these parameters to 0.
//
// A new price must differ from the old one by at least 5%.
func (c Client) UpdatePrices(params *UpdatePricesParams) (*UpdatePricesResponse, error) {
func (c Products) UpdatePrices(params *UpdatePricesParams) (*UpdatePricesResponse, error) {
url := "/v1/product/import/prices"
resp := &UpdatePricesResponse{}
@@ -776,7 +780,7 @@ type CreateOrUpdateProductResponse struct {
}
// This method allows you to create products and update their details
func (c Client) CreateOrUpdateProduct(params *CreateOrUpdateProductParams) (*CreateOrUpdateProductResponse, error) {
func (c Products) CreateOrUpdateProduct(params *CreateOrUpdateProductParams) (*CreateOrUpdateProductResponse, error) {
url := "/v2/product/import"
resp := &CreateOrUpdateProductResponse{}
@@ -838,7 +842,7 @@ type GetListOfProductsResponse struct {
} `json:"result"`
}
func (c Client) GetListOfProducts(params *GetListOfProductsParams) (*GetListOfProductsResponse, error) {
func (c Products) GetListOfProducts(params *GetListOfProductsParams) (*GetListOfProductsResponse, error) {
url := "/v2/product/list"
resp := &GetListOfProductsResponse{}
@@ -861,7 +865,7 @@ type GetProductsRatingBySKUResponse struct {
core.CommonResponse
// Products' content rating
Products []struct{
Products []struct {
// Product identifier in the Ozon system, SKU
SKU int64 `json:"sku"`
@@ -869,9 +873,9 @@ type GetProductsRatingBySKUResponse struct {
Rating float64 `json:"rating"`
// Groups of characteristics that make up the content rating
Groups []struct{
Groups []struct {
// List of conditions that increase the product content rating
Conditions []struct{
Conditions []struct {
// Number of content rating points that the condition gives
Cost float64 `json:"cost"`
@@ -889,7 +893,7 @@ type GetProductsRatingBySKUResponse struct {
ImproveAtLeast int32 `json:"improve_at_least"`
// List of attributes that can increase the product content rating
ImproveAttributes []struct{
ImproveAttributes []struct {
// Attribute identifier
Id int64 `json:"id"`
@@ -913,7 +917,7 @@ type GetProductsRatingBySKUResponse struct {
}
// Method for getting products' content rating and recommendations on how to increase it
func (c Client) GetProductsRatingBySKU(params *GetProductsRatingBySKUParams) (*GetProductsRatingBySKUResponse, error) {
func (c Products) GetProductsRatingBySKU(params *GetProductsRatingBySKUParams) (*GetProductsRatingBySKUResponse, error) {
url := "/v1/product/rating-by-sku"
resp := &GetProductsRatingBySKUResponse{}

View File

@@ -67,7 +67,7 @@ func TestGetStocksInfo(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetStocksInfo(test.params)
resp, err := c.Products().GetStocksInfo(test.params)
if err != nil {
t.Error(err)
}
@@ -231,7 +231,7 @@ func TestGetProductDetails(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetProductDetails(test.params)
resp, err := c.Products().GetProductDetails(test.params)
if err != nil {
t.Error(err)
}
@@ -288,7 +288,7 @@ func TestUpdateStocks(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.UpdateStocks(test.params)
resp, err := c.Products().UpdateStocks(test.params)
if err != nil {
t.Error(err)
}
@@ -341,7 +341,7 @@ func TestStocksInSellersWarehouse(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.StocksInSellersWarehouse(test.params)
resp, err := c.Products().StocksInSellersWarehouse(test.params)
if err != nil {
t.Error(err)
}
@@ -401,7 +401,7 @@ func TestUpdatePrices(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.UpdatePrices(test.params)
resp, err := c.Products().UpdatePrices(test.params)
if err != nil {
t.Error(err)
}
@@ -460,7 +460,7 @@ func TestUpdateQuantityStockProducts(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.UpdateQuantityStockProducts(test.params)
resp, err := c.Products().UpdateQuantityStockProducts(test.params)
if err != nil {
t.Error(err)
}
@@ -575,7 +575,7 @@ func TestCreateOrUpdateProduct(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.CreateOrUpdateProduct(test.params)
resp, err := c.Products().CreateOrUpdateProduct(test.params)
if err != nil {
t.Error(err)
}
@@ -634,7 +634,7 @@ func TestGetListOfProducts(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetListOfProducts(test.params)
resp, err := c.Products().GetListOfProducts(test.params)
if err != nil {
t.Error(err)
}
@@ -861,7 +861,7 @@ func TestGetProductsRatingBySKU(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetProductsRatingBySKU(test.params)
resp, err := c.Products().GetProductsRatingBySKU(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -6,6 +6,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Promotions struct {
client *core.Client
}
type GetAvailablePromotionsResponse struct {
core.CommonResponse
@@ -65,7 +69,7 @@ type GetAvailablePromotionsResponse struct {
} `json:"result"`
}
func (c Client) GetAvailablePromotions() (*GetAvailablePromotionsResponse, error) {
func (c Promotions) GetAvailablePromotions() (*GetAvailablePromotionsResponse, error) {
url := "/v1/actions"
resp := &GetAvailablePromotionsResponse{}

View File

@@ -54,7 +54,7 @@ func TestGetAvailablePromotions(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetAvailablePromotions()
resp, err := c.Promotions().GetAvailablePromotions()
if err != nil {
t.Error(err)
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Rating struct {
client *core.Client
}
type GetCurrentSellerRatingInfoResponse struct {
core.CommonResponse
@@ -80,7 +84,7 @@ type GetCurrentSellerRatingInfoResponse struct {
} `json:"groups"`
}
func (c Client) GetCurrentSellerRatingInfo() (*GetCurrentSellerRatingInfoResponse, error) {
func (c Rating) GetCurrentSellerRatingInfo() (*GetCurrentSellerRatingInfoResponse, error) {
url := "/v1/rating/summary"
resp := &GetCurrentSellerRatingInfoResponse{}
@@ -169,7 +173,7 @@ type GetSellerRatingInfoPeriodResponse struct {
} `json:"ratings"`
}
func (c Client) GetSellerRatingInfoForPeriod(params *GetSellerRatingInfoForPeriodParams) (*GetSellerRatingInfoPeriodResponse, error) {
func (c Rating) GetSellerRatingInfoForPeriod(params *GetSellerRatingInfoForPeriodParams) (*GetSellerRatingInfoPeriodResponse, error) {
url := "/v1/rating/history"
resp := &GetSellerRatingInfoPeriodResponse{}

View File

@@ -56,7 +56,7 @@ func TestGetCurrentRatingInfo(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetCurrentSellerRatingInfo()
resp, err := c.Rating().GetCurrentSellerRatingInfo()
if err != nil {
t.Error(err)
}
@@ -134,7 +134,7 @@ func TestGetRatingInfoForPeriod(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetSellerRatingInfoForPeriod(test.params)
resp, err := c.Rating().GetSellerRatingInfoForPeriod(test.params)
if err != nil {
t.Error(err)
}

View File

@@ -7,6 +7,10 @@ import (
core "github.com/diphantxm/ozon-api-client"
)
type Warehouses struct {
client *core.Client
}
type GetListOfWarehousesResponse struct {
core.CommonResponse
@@ -76,8 +80,8 @@ type GetListOfWarehousesResponse struct {
} `json:"resulCommonResponse"`
}
// You do not need to specify any parameters in the request. Your company will be identified by the Client ID
func (c Client) GetListOfWarehouses() (*GetListOfWarehousesResponse, error) {
// You do not need to specify any parameters in the request. Your company will be identified by the Warehouses ID
func (c Warehouses) GetListOfWarehouses() (*GetListOfWarehousesResponse, error) {
url := "/v1/warehouse/list"
resp := &GetListOfWarehousesResponse{}
@@ -165,7 +169,7 @@ type GetListOfDeliveryMethodsResponse struct {
}
// This methods allows you to get list of all delivery methods that can be applied for this warehouse
func (c Client) GetListOfDeliveryMethods(params *GetListOfDeliveryMethodsParams) (*GetListOfDeliveryMethodsResponse, error) {
func (c Warehouses) GetListOfDeliveryMethods(params *GetListOfDeliveryMethodsParams) (*GetListOfDeliveryMethodsResponse, error) {
url := "/v1/delivery-method/list"
resp := &GetListOfDeliveryMethodsResponse{}

View File

@@ -56,7 +56,7 @@ func TestGetListOfWarehouses(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetListOfWarehouses()
resp, err := c.Warehouses().GetListOfWarehouses()
if err != nil {
t.Error(err)
}
@@ -118,7 +118,7 @@ func TestGetListOfDeliveryMethods(t *testing.T) {
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.GetListOfDeliveryMethods(test.params)
resp, err := c.Warehouses().GetListOfDeliveryMethods(test.params)
if err != nil {
t.Error(err)
}