Skip to content

Commit

Permalink
Merge pull request #177 from EasyPost/carrier_metadata_ga
Browse files Browse the repository at this point in the history
feat: carrier metadata GA
  • Loading branch information
Justintime50 authored May 31, 2023
2 parents 0ffac80 + 3c88f44 commit acbbff6
Show file tree
Hide file tree
Showing 8 changed files with 789 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Migrates Carrier Metadata to GA

## v2.18.0 (2023-05-02)

- Adds `GetShipmentEstimatedDeliveryDate` and `GetShipmentEstimatedDeliveryDateWithContext` functions
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ install:

## lint - Lint the project
lint:
golangci-lint run
golangci-lint run -e SA1019

## release - Cuts a release for the project on GitHub (requires GitHub CLI)
# tag = The associated tag title of the release
Expand Down
119 changes: 119 additions & 0 deletions beta_carrier_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package easypost

import (
"context"
"strings"
)

// BetaCarrierMetadata represents all the metadata for a carrier.
//
// Deprecated: Use CarrierMetadata instead
type BetaCarrierMetadata struct {
Name string `json:"name,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
ServiceLevels []*BetaMetadataServiceLevel `json:"service_levels,omitempty"`
PredefinedPackages []*BetaMetadataPredefinedPackage `json:"predefined_packages,omitempty"`
ShipmentOptions []*BetaMetadataShipmentOption `json:"shipment_options,omitempty"`
SupportedFeatures []*BetaMetadataSupportedFeature `json:"supported_features,omitempty"`
}

// BetaMetadataServiceLevel represents an available service level of a carrier.
//
// Deprecated: Use MetadataServiceLevel instead
type BetaMetadataServiceLevel struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Description string `json:"description,omitempty"`
Dimensions []string `json:"dimensions,omitempty"`
MaxWeight float64 `json:"max_weight,omitempty"`
}

// BetaMetadataPredefinedPackage represents an available predefined package of a carrier.
//
// Deprecated: Use MetadataPredefinedPackage instead
type BetaMetadataPredefinedPackage struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Description string `json:"description,omitempty"`
Dimensions []string `json:"dimensions,omitempty"`
MaxWeight float64 `json:"max_weight,omitempty"`
}

// BetaMetadataShipmentOption represents an available shipment option of a carrier.
//
// Deprecated: Use MetadataShipmentOption instead
type BetaMetadataShipmentOption struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Description string `json:"description,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Type string `json:"type,omitempty"`
}

// BetaMetadataSupportedFeature represents a supported feature of a carrier.
//
// Deprecated: Use BetaMetadataSupportedFeature instead
type BetaMetadataSupportedFeature struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
Description string `json:"description,omitempty"`
Supported bool `json:"supported,omitempty"`
}

// BetaGetCarrierMetadata retrieves all metadata for all carriers on the EasyPost platform.
//
// Deprecated: Use GetCarrierMetadata instead
func (c *Client) BetaGetCarrierMetadata() (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), nil, nil)
}

// BetaGetCarrierMetadataWithCarriers retrieves carrier metadata for a list of carriers.
//
// Deprecated: Use GetCarrierMetadataWithCarriers instead
func (c *Client) BetaGetCarrierMetadataWithCarriers(carriers []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), carriers, nil)
}

// BetaGetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers.
//
// Deprecated: Use GetCarrierMetadataWithTypes instead
func (c *Client) BetaGetCarrierMetadataWithTypes(types []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), nil, types)
}

// BetaGetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers and a list of types.
//
// Deprecated: Use GetCarrierMetadataWithCarriersAndTypes instead
func (c *Client) BetaGetCarrierMetadataWithCarriersAndTypes(carriers []string, types []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), carriers, types)
}

// BetaGetCarrierMetadataWithContext performs the same operation as
// BetaGetCarrierMetadata, but allows specifying a context that can interrupt the
// request.
//
// Deprecated: Use GetCarrierMetadataWithContext instead
func (c *Client) BetaGetCarrierMetadataWithContext(ctx context.Context, carriers []string, types []string) (out []*BetaCarrierMetadata, err error) {
url := "/beta/metadata"
if carriers != nil && types != nil {
url = url + "?"
}
if carriers != nil {
url = url + "carriers=" + strings.Join(carriers[:], ",")
}
if carriers != nil && types != nil {
url = url + "&"
}
if types != nil {
url = url + "types=" + strings.Join(types[:], ",")
}

res := struct {
BetaCarrierMetadata *[]*BetaCarrierMetadata `json:"carriers,omitempty"`
}{BetaCarrierMetadata: &out}
err = c.get(ctx, url, &res)
return
}
64 changes: 32 additions & 32 deletions carrier_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"strings"
)

// BetaCarrierMetadata represents all the metadata for a carrier.
type BetaCarrierMetadata struct {
// CarrierMetadata represents all the metadata for a carrier.
type CarrierMetadata struct {
Name string `json:"name,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
ServiceLevels []*BetaMetadataServiceLevel `json:"service_levels,omitempty"`
PredefinedPackages []*BetaMetadataPredefinedPackage `json:"predefined_packages,omitempty"`
ShipmentOptions []*BetaMetadataShipmentOption `json:"shipment_options,omitempty"`
SupportedFeatures []*BetaMetadataSupportedFeature `json:"supported_features,omitempty"`
ServiceLevels []*MetadataServiceLevel `json:"service_levels,omitempty"`
PredefinedPackages []*MetadataPredefinedPackage `json:"predefined_packages,omitempty"`
ShipmentOptions []*MetadataShipmentOption `json:"shipment_options,omitempty"`
SupportedFeatures []*MetadataSupportedFeature `json:"supported_features,omitempty"`
}

// BetaMetadataServiceLevel represents an available service level of a carrier.
type BetaMetadataServiceLevel struct {
// MetadataServiceLevel represents an available service level of a carrier.
type MetadataServiceLevel struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Expand All @@ -25,8 +25,8 @@ type BetaMetadataServiceLevel struct {
MaxWeight float64 `json:"max_weight,omitempty"`
}

// BetaMetadataPredefinedPackage represents an available predefined package of a carrier.
type BetaMetadataPredefinedPackage struct {
// MetadataPredefinedPackage represents an available predefined package of a carrier.
type MetadataPredefinedPackage struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Expand All @@ -35,8 +35,8 @@ type BetaMetadataPredefinedPackage struct {
MaxWeight float64 `json:"max_weight,omitempty"`
}

// BetaMetadataShipmentOption represents an available shipment option of a carrier.
type BetaMetadataShipmentOption struct {
// MetadataShipmentOption represents an available shipment option of a carrier.
type MetadataShipmentOption struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
HumanReadable string `json:"human_readable,omitempty"`
Expand All @@ -45,39 +45,39 @@ type BetaMetadataShipmentOption struct {
Type string `json:"type,omitempty"`
}

// BetaMetadataSupportedFeature represents a supported feature of a carrier.
type BetaMetadataSupportedFeature struct {
// MetadataSupportedFeature represents a supported feature of a carrier.
type MetadataSupportedFeature struct {
Name string `json:"name,omitempty"`
Carrier string `json:"carrier,omitempty"`
Description string `json:"description,omitempty"`
Supported bool `json:"supported,omitempty"`
}

// BetaGetCarrierMetadata retrieves all metadata for all carriers on the EasyPost platform.
func (c *Client) BetaGetCarrierMetadata() (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), nil, nil)
// GetCarrierMetadata retrieves all metadata for all carriers on the EasyPost platform.
func (c *Client) GetCarrierMetadata() (out []*CarrierMetadata, err error) {
return c.GetCarrierMetadataWithContext(context.Background(), nil, nil)
}

// BetaGetCarrierMetadataWithCarriers retrieves carrier metadata for a list of carriers.
func (c *Client) BetaGetCarrierMetadataWithCarriers(carriers []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), carriers, nil)
// GetCarrierMetadataWithCarriers retrieves carrier metadata for a list of carriers.
func (c *Client) GetCarrierMetadataWithCarriers(carriers []string) (out []*CarrierMetadata, err error) {
return c.GetCarrierMetadataWithContext(context.Background(), carriers, nil)
}

// BetaGetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers.
func (c *Client) BetaGetCarrierMetadataWithTypes(types []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), nil, types)
// GetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers.
func (c *Client) GetCarrierMetadataWithTypes(types []string) (out []*CarrierMetadata, err error) {
return c.GetCarrierMetadataWithContext(context.Background(), nil, types)
}

// BetaGetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers and a list of types.
func (c *Client) BetaGetCarrierMetadataWithCarriersAndTypes(carriers []string, types []string) (out []*BetaCarrierMetadata, err error) {
return c.BetaGetCarrierMetadataWithContext(context.Background(), carriers, types)
// GetCarrierMetadataWithTypes retrieves carrier metadata for a list of carriers and a list of types.
func (c *Client) GetCarrierMetadataWithCarriersAndTypes(carriers []string, types []string) (out []*CarrierMetadata, err error) {
return c.GetCarrierMetadataWithContext(context.Background(), carriers, types)
}

// BetaGetCarrierMetadataWithContext performs the same operation as
// BetaGetCarrierMetadata, but allows specifying a context that can interrupt the
// GetCarrierMetadataWithContext performs the same operation as
// GetCarrierMetadata, but allows specifying a context that can interrupt the
// request.
func (c *Client) BetaGetCarrierMetadataWithContext(ctx context.Context, carriers []string, types []string) (out []*BetaCarrierMetadata, err error) {
url := "/beta/metadata"
func (c *Client) GetCarrierMetadataWithContext(ctx context.Context, carriers []string, types []string) (out []*CarrierMetadata, err error) {
url := "/v2/metadata/carriers"
if carriers != nil && types != nil {
url = url + "?"
}
Expand All @@ -92,8 +92,8 @@ func (c *Client) BetaGetCarrierMetadataWithContext(ctx context.Context, carriers
}

res := struct {
BetaCarrierMetadata *[]*BetaCarrierMetadata `json:"carriers,omitempty"`
}{BetaCarrierMetadata: &out}
CarrierMetadata *[]*CarrierMetadata `json:"carriers,omitempty"`
}{CarrierMetadata: &out}
err = c.get(ctx, url, &res)
return
}
48 changes: 48 additions & 0 deletions tests/beta_carrier_metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package easypost_test

func (c *ClientTests) TestBetaGetCarrierMetadata() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()

carrierMetadata, err := client.BetaGetCarrierMetadata()
require.NoError(err)

// Assert we get multiple carriers
uspsFound := false
fedexFound := false
for _, carrier := range carrierMetadata {
if carrier.Name == "usps" {
uspsFound = true
}
if carrier.Name == "fedex" {
fedexFound = true
}
if uspsFound && fedexFound {
break
}
}
assert.True(uspsFound)
assert.True(fedexFound)
}

func (c *ClientTests) TestBetaGetCarrierMetadataWithCarriersAndTypes() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()

carrierMetadata, err := client.BetaGetCarrierMetadataWithCarriersAndTypes([]string{"usps"}, []string{"service_levels", "predefined_packages"})
require.NoError(err)

// Assert we get the single carrier we asked for and only the types we asked for
uspsFound := false
for _, carrier := range carrierMetadata {
if carrier.Name == "usps" {
uspsFound = true
break
}
}
assert.True(uspsFound)
assert.Equal(1, len(carrierMetadata))
assert.NotNil(carrierMetadata[0].ServiceLevels)
assert.NotNil(carrierMetadata[0].PredefinedPackages)
assert.Nil(carrierMetadata[0].SupportedFeatures)
}
8 changes: 4 additions & 4 deletions tests/carrier_metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package easypost_test

func (c *ClientTests) TestBetaGetCarrierMetadata() {
func (c *ClientTests) TestGetCarrierMetadata() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()

carrierMetadata, err := client.BetaGetCarrierMetadata()
carrierMetadata, err := client.GetCarrierMetadata()
require.NoError(err)

// Assert we get multiple carriers
Expand All @@ -25,11 +25,11 @@ func (c *ClientTests) TestBetaGetCarrierMetadata() {
assert.True(fedexFound)
}

func (c *ClientTests) TestBetaGetCarrierMetadataWithCarriersAndTypes() {
func (c *ClientTests) TestGetCarrierMetadataWithCarriersAndTypes() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()

carrierMetadata, err := client.BetaGetCarrierMetadataWithCarriersAndTypes([]string{"usps"}, []string{"service_levels", "predefined_packages"})
carrierMetadata, err := client.GetCarrierMetadataWithCarriersAndTypes([]string{"usps"}, []string{"service_levels", "predefined_packages"})
require.NoError(err)

// Assert we get the single carrier we asked for and only the types we asked for
Expand Down
Loading

0 comments on commit acbbff6

Please sign in to comment.