diff --git a/CHANGELOG.md b/CHANGELOG.md index 038915a..413db84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Next Release + +- Fix `DateTime`-type parameters not being included in GET requests + ## v4.1.0 (2024-01-08) - Add `ListChildUsers` and `GetNextChildUserPage` functions to `User` service diff --git a/datetime.go b/datetime.go index 14ec8de..07c5a70 100644 --- a/datetime.go +++ b/datetime.go @@ -1,6 +1,9 @@ package easypost -import "time" +import ( + "net/url" + "time" +) type DateTime time.Time @@ -93,16 +96,22 @@ func (dt *DateTime) UnmarshalJSON(b []byte) (err error) { return nil } -func (dt DateTime) MarshalJSON() ([]byte, error) { - return time.Time(dt).MarshalJSON() +func (dt *DateTime) MarshalJSON() ([]byte, error) { + return time.Time(*dt).MarshalJSON() } -func (dt DateTime) String() string { - return time.Time(dt).String() +func (dt *DateTime) String() string { + return time.Time(*dt).String() } -func (dt DateTime) AsTime() time.Time { - return time.Time(dt) +// EncodeValues implements the query.Encoder interface of how to encode the DateTime for a URL query string +func (dt *DateTime) EncodeValues(key string, values *url.Values) error { + values.Set(key, time.Time(*dt).Format(time.RFC3339)) // RFC3339 is the default format for time.Time + return nil +} + +func (dt *DateTime) AsTime() time.Time { + return time.Time(*dt) } // NewDateTime returns the DateTime corresponding to diff --git a/tests/datetime_test.go b/tests/datetime_test.go index 5a6ad84..42b50a2 100644 --- a/tests/datetime_test.go +++ b/tests/datetime_test.go @@ -4,6 +4,8 @@ import ( "github.com/EasyPost/easypost-go/v4" "reflect" "time" + + "github.com/google/go-querystring/query" ) func (c *ClientTests) TestDateTime() { @@ -59,3 +61,26 @@ func (c *ClientTests) TestDateTimeJSON() { assert.NotNil(maxDatetimeString) assert.NotNil(minDatetimeString) } + +// / TestDateTimeUrlQueryStringParameterInclusion tests that DateTime-type parameters are encoded properly (RFC3339) for use in HTTP payloads +func (c *ClientTests) TestDateTimeUrlQueryStringParameterInclusion() { + assert := c.Assert() + + now := time.Now().UTC() + past := now.AddDate(0, -4, 0) + + start := easypost.DateTimeFromTime(past) + end := easypost.DateTimeFromTime(now) + + options := easypost.ListOptions{ + StartDateTime: &start, + EndDateTime: &end, + PageSize: 100, + } + + // This is the internals of the `convertOptsToURLValues` method in the `Client` struct (can't be used directly because it's private) + values, _ := query.Values(options) + + assert.Equal(past.Format(time.RFC3339), values.Get("start_datetime")) + assert.Equal(now.Format(time.RFC3339), values.Get("end_datetime")) +}