diff --git a/ENDPOINTS.md b/ENDPOINTS.md index cfcf147..febd5fa 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -128,8 +128,8 @@ - [ ] ETGB customs declarations ## Returns -- [ ] Get information about FBO returns (version 3) -- [ ] Get information about FBS returns +- [x] Get information about FBO returns (version 3) +- [x] Get information about FBS returns ## Cancellations - [ ] Get information about a rFBS cancellation request @@ -153,7 +153,7 @@ ## Reports - [ ] Report details -- [ ] Reports list +- [x] Reports list - [ ] Products report - [ ] Prices report - [ ] Stocks report diff --git a/ozon/ozon.go b/ozon/ozon.go index 07c12b1..6f24866 100644 --- a/ozon/ozon.go +++ b/ozon/ozon.go @@ -22,6 +22,7 @@ type Client struct { rating *Rating warehouses *Warehouses returns *Returns + reports *Reports } func (c Client) Analytics() *Analytics { @@ -60,6 +61,10 @@ func (c Client) Returns() *Returns { return c.returns } +func (c Client) Reports() *Reports { + return c.reports +} + func NewClient(clientId, apiKey string) *Client { coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{ "Client-Id": clientId, @@ -77,6 +82,7 @@ func NewClient(clientId, apiKey string) *Client { rating: &Rating{client: coreClient}, warehouses: &Warehouses{client: coreClient}, returns: &Returns{client: coreClient}, + reports: &Reports{client: coreClient}, } } @@ -94,5 +100,6 @@ func NewMockClient(handler http.HandlerFunc) *Client { rating: &Rating{client: coreClient}, warehouses: &Warehouses{client: coreClient}, returns: &Returns{client: coreClient}, + reports: &Reports{client: coreClient}, } } diff --git a/ozon/reports.go b/ozon/reports.go new file mode 100644 index 0000000..d8128c0 --- /dev/null +++ b/ozon/reports.go @@ -0,0 +1,92 @@ +package ozon + +import ( + "net/http" + "time" + + core "github.com/diphantxm/ozon-api-client" +) + +type Reports struct { + client *core.Client +} + +type GetReportsListParams struct { + // Page number + Page int32 `json:"page"` + + // The number of values on the page: + // - default value is 100, + // - maximum value is 1000 + PageSize int32 `json:"page_size"` + + // Default: "ALL" + // Report type: + // - ALL — all reports, + // - SELLER_PRODUCTS — products report,, + // - SELLER_TRANSACTIONS — transactions report, + // - SELLER_PRODUCT_PRICES — product prices report, + // - SELLER_STOCK — stocks report, + // - SELLER_PRODUCT_MOVEMENT — products movement report, + // - SELLER_RETURNS — returns report, + // - SELLER_POSTINGS — shipments report, + // - SELLER_FINANCE — financial report + ReportType string `json:"report_type"` +} + +type GetReportsListResponse struct { + core.CommonResponse + + // Method result + Result struct { + // Unique report identifier + Code string `json:"code"` + + // Report creation date + CreatedAt time.Time `json:"created_at"` + + // Error code when generating the report + Error string `json:"error"` + + // Link to CSV file + File string `json:"file"` + + // Array with the filters specified when the seller created the report + Params struct { + } `json:"params"` + + // Report type: + // - SELLER_PRODUCTS — products report, + // - SELLER_TRANSACTIONS — transactions report, + // - SELLER_PRODUCT_PRICES — product prices report, + // - SELLER_STOCK — stocks report, + // - SELLER_PRODUCT_MOVEMENT — products movement report, + // - SELLER_RETURNS — returns report, + // - SELLER_POSTINGS — shipments report, + // - SELLER_FINANCE — financial report + ReportType string `json:"report_type"` + + // Report generation status + // - `success` + // - `failed` + Status string `json:"status"` + } `json:"result"` + + // Total number of reports + Total int32 `json:"total"` +} + +// Returns the list of reports that have been generated before +func (c Reports) GetList(params *GetReportsListParams) (*GetReportsListResponse, error) { + url := "/v1/report/list" + + resp := &GetReportsListResponse{} + + response, err := c.client.Request(http.MethodPost, url, params, resp) + if err != nil { + return nil, err + } + response.CopyCommonResponse(&resp.CommonResponse) + + return resp, nil +} diff --git a/ozon/reports_test.go b/ozon/reports_test.go new file mode 100644 index 0000000..d4e71e7 --- /dev/null +++ b/ozon/reports_test.go @@ -0,0 +1,79 @@ +package ozon + +import ( + "net/http" + "testing" + + core "github.com/diphantxm/ozon-api-client" +) + +func TestGetList(t *testing.T) { + tests := []struct { + statusCode int + headers map[string]string + params *GetReportsListParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &GetReportsListParams{ + ReportType: "ALL", + PageSize: 1000, + }, + `{ + "result": { + "reports": [ + { + "code": "cd3f2c76-2b9a-4e77-a5a9-7ab52678b3bf", + "status": "success", + "error": "", + "file": "https://minio-production.s3.s.o3.ru:8000/sc-temporary/89/0e/890ef6e360a6396f.csv", + "report_type": "seller_products", + "params": { + "visibility": "3" + }, + "created_at": "2019-02-06T12:09:47.258062Z" + }, + { + "code": "c39f5fe4-c00b-4e95-a487-6ad34c1e34a3", + "status": "success", + "error": "", + "file": "https://minio-production.s3.s.o3.ru:8000/reports/a7/48/a7481a083873e164.csv", + "report_type": "seller_products", + "params": { + "visibility": "3" + }, + "created_at": "2019-02-15T08:34:32.267178Z" + } + ], + "total": 2 + } + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetReportsListParams{}, + `{ + "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.Reports().GetList(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) + } + } +}