diff --git a/.github/workflows/go.yml b/.github/workflows/tests.yml similarity index 100% rename from .github/workflows/go.yml rename to .github/workflows/tests.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa92975 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.out \ No newline at end of file diff --git a/README.md b/README.md index 22ab16a..cf5320d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ -# Ozon API Client -A Ozon API client written in Golang +# Ozon Seller API Client +A Ozon Seller API client written in Golang + +[![Coverage Status](https://coveralls.io/repos/github/diPhantxm/ozon-api-client/badge.svg?branch=master)](https://coveralls.io/github/diPhantxm/ozon-api-client?branch=master) +![example workflow](https://github.com/diPhantxm/ozon-api-client/actions/workflows/tests.yml/badge.svg) [Ozon](https://ozon.ru) is a marketplace for small and medium enterprises to launch and grow their businesses in Russia. @@ -14,7 +17,7 @@ go get github.com/diphantxm/ozon-api-client ``` A simple example on how to use this library: ```Golang -package integrations +package main import ( "fmt" @@ -24,7 +27,7 @@ import ( "github.com/diphantxm/ozon-api-client/ozon" ) -func t() { +func main() { // Create a client with your Client-Id and Api-Key // [Documentation]: https://docs.ozon.ru/api/seller/en/#tag/Auth client := ozon.NewClient("my-client-id", "my-api-key") @@ -42,5 +45,9 @@ func t() { fmt.Printf("Barcode %s\n", d) } } +``` -``` \ No newline at end of file +## Contribution +If you need some endpoints ASAP, create an issue and list all the endpoints. I will add them to library soon. + +Or you can implement them and contribute to the project. Contribution to the project is welcome. \ No newline at end of file diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..95f8382 --- /dev/null +++ b/client_test.go @@ -0,0 +1,68 @@ +package core + +import ( + "net/http" + "testing" +) + +type TestRequestRequest struct { + FirstField string `json:"first_field"` + SecondField int32 `json:"second_field"` +} + +type TestRequestResponse struct { + FirstField string `json:"first_header"` + SecondField struct { + Key int32 `json:"key"` + } `json:"second_header"` + ThirdField []struct { + FirstElement string `json:"first_element"` + } `json:"third_header"` +} + +func TestRequest(t *testing.T) { + tests := []struct { + statusCode int + headers map[string]string + params *TestRequestRequest + response string + }{ + { + http.StatusOK, + map[string]string{ + "first_header": "first-value", + "second_header": "second-value", + }, + &TestRequestRequest{ + FirstField: "test", + SecondField: 123, + }, + `{ + "first_field": "first-value", + "second_field": { + "key": 12 + }, + "third_field": [ + { + "first_element": "first-element-value" + } + ] + }`, + }, + } + + for _, test := range tests { + c := NewMockClient(NewMockHttpHandler(test.statusCode, test.response, test.headers)) + + respStruct := &TestRequestResponse{} + resp, err := c.Request(http.MethodPost, "/", test.params, respStruct) + + 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) + } + } +} diff --git a/ozon/analytics_test.go b/ozon/analytics_test.go index aa7acf9..9f0a058 100644 --- a/ozon/analytics_test.go +++ b/ozon/analytics_test.go @@ -14,6 +14,7 @@ func TestGetAnalyticsData(t *testing.T) { params *GetAnalyticsDataParams response string }{ + // Test ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -41,6 +42,16 @@ func TestGetAnalyticsData(t *testing.T) { "timestamp": "2021-11-25 15:19:21" }`, }, + { + // Test No Client-Id or Api-Key + http.StatusUnauthorized, + map[string]string{}, + &GetAnalyticsDataParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -64,6 +75,7 @@ func TestGetStocksOnWarehouses(t *testing.T) { params *GetStocksOnWarehousesParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -87,6 +99,16 @@ func TestGetStocksOnWarehouses(t *testing.T) { ] } }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetStocksOnWarehousesParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, }, } diff --git a/ozon/fbo_test.go b/ozon/fbo_test.go index 6df1078..46661a9 100644 --- a/ozon/fbo_test.go +++ b/ozon/fbo_test.go @@ -14,6 +14,7 @@ func TestGetFBOShipmentsList(t *testing.T) { params *GetFBOShipmentsListParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -115,6 +116,16 @@ func TestGetFBOShipmentsList(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetFBOShipmentsListParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { diff --git a/ozon/fbs_test.go b/ozon/fbs_test.go index ec549d9..0874ce2 100644 --- a/ozon/fbs_test.go +++ b/ozon/fbs_test.go @@ -14,6 +14,7 @@ func TestListUnprocessedShipments(t *testing.T) { params *ListUnprocessedShipmentsParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -149,6 +150,16 @@ func TestListUnprocessedShipments(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &ListUnprocessedShipmentsParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -172,6 +183,7 @@ func TestGetFBSShipmentsList(t *testing.T) { params *GetFBSShipmentsListParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -247,6 +259,16 @@ func TestGetFBSShipmentsList(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetFBSShipmentsListParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -270,6 +292,7 @@ func TestPackOrder(t *testing.T) { params *PackOrderParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -295,6 +318,16 @@ func TestPackOrder(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &PackOrderParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { diff --git a/ozon/products_test.go b/ozon/products_test.go index 7788f07..8126b7b 100644 --- a/ozon/products_test.go +++ b/ozon/products_test.go @@ -14,6 +14,7 @@ func TestGetStocksInfo(t *testing.T) { params *GetStocksInfoParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -51,27 +52,14 @@ func TestGetStocksInfo(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key { - http.StatusBadRequest, - map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, - &GetStocksInfoParams{ - Limit: 100, - LastId: "", - Filter: GetStocksInfoFilter{ - OfferId: "136834", - ProductId: 214887921, - Visibility: "ALL", - }, - }, + http.StatusUnauthorized, + map[string]string{}, + &GetStocksInfoParams{}, `{ - "code": 0, - "details": [ - { - "typeUrl": "string", - "value": "string" - } - ], - "message": "string" + "code": 16, + "message": "Client-Id and Api-Key headers are required" }`, }, } @@ -97,6 +85,7 @@ func TestGetProductDetails(t *testing.T) { params *GetProductDetailsParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -227,21 +216,14 @@ func TestGetProductDetails(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key { - http.StatusBadRequest, - map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, - &GetProductDetailsParams{ - ProductId: 137208233, - }, + http.StatusUnauthorized, + map[string]string{}, + &GetProductDetailsParams{}, `{ - "code": 0, - "details": [ - { - "typeUrl": "string", - "value": "string" - } - ], - "message": "string" + "code": 16, + "message": "Client-Id and Api-Key headers are required" }`, }, } @@ -267,6 +249,7 @@ func TestUpdateStocks(t *testing.T) { params *UpdateStocksParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -290,6 +273,16 @@ func TestUpdateStocks(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &UpdateStocksParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -313,6 +306,7 @@ func TestStocksInSellersWarehouse(t *testing.T) { params *StocksInSellersWarehouseParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -332,6 +326,16 @@ func TestStocksInSellersWarehouse(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &StocksInSellersWarehouseParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -355,6 +359,7 @@ func TestUpdatePrices(t *testing.T) { params *UpdatePricesParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -381,6 +386,16 @@ func TestUpdatePrices(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &UpdatePricesParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -404,6 +419,7 @@ func TestUpdateQuantityStockProducts(t *testing.T) { params *UpdateQuantityStockProductsParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -429,6 +445,16 @@ func TestUpdateQuantityStockProducts(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &UpdateQuantityStockProductsParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -452,6 +478,7 @@ func TestCreateOrUpdateProduct(t *testing.T) { params *CreateOrUpdateProductParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -533,6 +560,16 @@ func TestCreateOrUpdateProduct(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &CreateOrUpdateProductParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -556,6 +593,7 @@ func TestGetListOfProducts(t *testing.T) { params *GetListOfProductsParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -581,6 +619,16 @@ func TestGetListOfProducts(t *testing.T) { } }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetListOfProductsParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { diff --git a/ozon/promotions_test.go b/ozon/promotions_test.go index b75c369..35b98b8 100644 --- a/ozon/promotions_test.go +++ b/ozon/promotions_test.go @@ -13,6 +13,7 @@ func TestGetAvailablePromotions(t *testing.T) { headers map[string]string response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -39,6 +40,15 @@ func TestGetAvailablePromotions(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { diff --git a/ozon/rating_test.go b/ozon/rating_test.go index 360d8d9..da0a1e1 100644 --- a/ozon/rating_test.go +++ b/ozon/rating_test.go @@ -13,6 +13,7 @@ func TestGetCurrentRatingInfo(t *testing.T) { headers map[string]string response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -41,6 +42,15 @@ func TestGetCurrentRatingInfo(t *testing.T) { "premium": true }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { @@ -64,6 +74,7 @@ func TestGetRatingInfoForPeriod(t *testing.T) { params *GetSellerRatingInfoForPeriodParams response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -108,6 +119,16 @@ func TestGetRatingInfoForPeriod(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetSellerRatingInfoForPeriodParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests { diff --git a/ozon/warehouses_test.go b/ozon/warehouses_test.go index 7c652d5..b90b8b8 100644 --- a/ozon/warehouses_test.go +++ b/ozon/warehouses_test.go @@ -13,6 +13,7 @@ func TestGetListOfWarehouses(t *testing.T) { headers map[string]string response string }{ + // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, @@ -41,6 +42,15 @@ func TestGetListOfWarehouses(t *testing.T) { ] }`, }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, } for _, test := range tests {