From e5867813ab0dccf7d530c38bc375740f048cec96 Mon Sep 17 00:00:00 2001 From: Zloy_Leshiy Date: Tue, 30 Apr 2024 13:03:08 +0300 Subject: [PATCH] Custom data type for time representation (#86) Co-authored-by: o.tyurin --- core.go | 31 +++++++++++++++++++++++++++++++ ozon/analytics.go | 5 ++--- ozon/analytics_test.go | 5 +++-- ozon/common.go | 4 +++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/core.go b/core.go index 7d5209c..a2ccd05 100644 --- a/core.go +++ b/core.go @@ -5,6 +5,7 @@ import ( "net/http" "reflect" "strconv" + "strings" "testing" "time" ) @@ -162,3 +163,33 @@ func TimeFromString(t *testing.T, format, datetime string) time.Time { } return dt } + +const LayoutRequestDateDefault = "2006-01-02" + +type RequestDate struct { + time.Time + layout string +} + +func NewRequestDate(t time.Time, layout string) *RequestDate { + return &RequestDate{ + Time: t, + layout: layout, + } +} + +func (rd *RequestDate) UnmarshalJSON(b []byte) (err error) { + s := strings.Trim(string(b), `"`) // remove quotes + if s == "null" { + return + } + rd.Time, err = time.Parse(rd.layout, s) + return +} + +func (rd *RequestDate) MarshalJSON() ([]byte, error) { + if rd.Time.IsZero() { + return nil, nil + } + return []byte(fmt.Sprintf(`"%s"`, rd.Time.Format(rd.layout))), nil +} diff --git a/ozon/analytics.go b/ozon/analytics.go index ddea2e7..600d11d 100644 --- a/ozon/analytics.go +++ b/ozon/analytics.go @@ -3,7 +3,6 @@ package ozon import ( "context" "net/http" - "time" core "github.com/diphantxm/ozon-api-client" ) @@ -14,10 +13,10 @@ type Analytics struct { type GetAnalyticsDataParams struct { // Date from which the data will be in the report - DateFrom time.Time `json:"date_from"` + DateFrom *core.RequestDate `json:"date_from"` // Date up to which the data will be in the report - DateTo time.Time `json:"date_to"` + DateTo *core.RequestDate `json:"date_to"` // Items Enum: "unknownDimension" "sku" "spu" "day" "week" "month" "year" "category1" "category2" "category3" "category4" "brand" "modelID" // Data grouping available to all sellers: diff --git a/ozon/analytics_test.go b/ozon/analytics_test.go index 40292db..a475261 100644 --- a/ozon/analytics_test.go +++ b/ozon/analytics_test.go @@ -4,6 +4,7 @@ import ( "context" "net/http" "testing" + "time" core "github.com/diphantxm/ozon-api-client" ) @@ -22,8 +23,8 @@ func TestGetAnalyticsData(t *testing.T) { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &GetAnalyticsDataParams{ - DateFrom: core.TimeFromString(t, "2006-01-02", "2020-09-01"), - DateTo: core.TimeFromString(t, "2006-01-02", "2021-10-15"), + DateFrom: core.NewRequestDate(time.Now().Add(time.Duration(30)*24*time.Hour), core.LayoutRequestDateDefault), + DateTo: core.NewRequestDate(time.Now(), core.LayoutRequestDateDefault), Dimension: []GetAnalyticsDataDimension{SKUDimension, DayDimension}, Metrics: []GetAnalyticsDataFilterMetric{HistViewPDP}, Sort: []GetAnalyticsDataSort{ diff --git a/ozon/common.go b/ozon/common.go index 29619c6..9f3ec70 100644 --- a/ozon/common.go +++ b/ozon/common.go @@ -1,6 +1,8 @@ package ozon -import "time" +import ( + "time" +) const ( testTimeout = 5 * time.Second