-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from EasyPost/carrier_metadata_ga
feat: carrier metadata GA
- Loading branch information
Showing
8 changed files
with
789 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.