aboutsummaryrefslogtreecommitdiffstats
path: root/cost.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2026-05-12 15:35:06 -0400
committerSam Anthony <sam@samanthony.xyz>2026-05-12 15:37:29 -0400
commit9010ebe8a581fb9db7bc6e97d40ff062fb18495f (patch)
tree4301a455762a59d4951507c8a8781e99c6f91c6d /cost.go
parent329257be8d9fb05d3dcea49823acea0f878ed52c (diff)
downloadlulu-9010ebe8a581fb9db7bc6e97d40ff062fb18495f.zip
unmarshal GET /print-jobs response
Diffstat (limited to 'cost.go')
-rw-r--r--cost.go118
1 files changed, 46 insertions, 72 deletions
diff --git a/cost.go b/cost.go
index c0d340a..3d3c3dd 100644
--- a/cost.go
+++ b/cost.go
@@ -1,16 +1,6 @@
package lulu
-import (
- "encoding/json"
-
- "github.com/shopspring/decimal"
-)
-
-type printJobCostReq struct {
- LineItems []PrintJobCostLineItem `json:"line_items"`
- ShipAddr printJobCostReqShipAddr `json:"shipping_address"`
- ShipOpt ShippingLevel `json:"shipping_option"`
-}
+import "github.com/shopspring/decimal"
type PrintJobCostLineItem struct {
NPages uint `json:"page_count"`
@@ -18,32 +8,16 @@ type PrintJobCostLineItem struct {
Quantity uint `json:"quantity"`
}
-type printJobCostReqShipAddr struct {
- City string `json:"city"`
- Country string `json:"country_code"`
- PostCode string `json:"postcode"`
- State string `json:"state_code"`
- Street1 string `json:"street1"`
- Phone PhoneNumber `json:"phone_number"`
-}
-
-// PrintJobCost is the response from /print-job-cost-calculations/.
type PrintJobCost struct {
- Addr, SuggestedAddr ShippingAddress
- AddrWarnings []ShippingAddressWarning
- Fees []Fee
- LineItemCosts []LineItemCost
- ShipCost FulfillmentCost
- FulfillmentCost FulfillmentCost
- TotalTax, TotalCostExclTax, TotalCostInclTax, TotalDiscount decimal.Decimal
- Currency string
-}
-
-type ShippingAddressWarning struct {
- Type string `json:"type"` // eg "validation_warning"
- Path string `json:"path"` // eg "external"
- Code string `json:"code"` // eg "REPLACED"
- Msg string `json:"message"` // eg "street1: Holstenstr. 40 -> Holstenstraße 40"
+ Fees []Fee `json:"fees"`
+ LineItemCosts []LineItemCost `json:"line_item_costs"`
+ ShipCost FulfillmentCost `json:"shipping_cost"`
+ FulfillmentCost FulfillmentCost `json:"fulfillment_cost"`
+ TotalTax decimal.Decimal `json:"total_tax"`
+ TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
+ TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
+ TotalDiscount decimal.Decimal `json:"total_discount"`
+ Currency string `json:"currency"`
}
type Fee struct {
@@ -80,44 +54,44 @@ type Discount struct {
Description string `json:"description"`
}
+type printJobCostReq struct {
+ LineItems []PrintJobCostLineItem `json:"line_items"`
+ ShipAddr printJobCostReqShipAddr `json:"shipping_address"`
+ ShipOpt ShippingLevel `json:"shipping_option"`
+}
+
+type printJobCostReqShipAddr struct {
+ City string `json:"city"`
+ Country string `json:"country_code"`
+ PostCode string `json:"postcode"`
+ State string `json:"state_code"`
+ Street1 string `json:"street1"`
+ Phone PhoneNumber `json:"phone_number"`
+}
+
type printJobCostResp struct {
- Addr json.RawMessage `json:"shipping_address"`
- Fees []Fee `json:"fees"`
- LineItemCosts []LineItemCost `json:"line_item_costs"`
- ShipCost FulfillmentCost `json:"shipping_cost"`
- FulfillmentCost FulfillmentCost `json:"fulfillment_cost"`
- TotalTax decimal.Decimal `json:"total_tax"`
- TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
- TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
- TotalDiscount decimal.Decimal `json:"total_discount_amount"`
- Currency string `json:"currency"`
+ AddressValidation ShippingAddressValidation `json:"shipping_address"`
+ Fees []Fee `json:"fees"`
+ LineItemCosts []LineItemCost `json:"line_item_costs"`
+ ShipCost FulfillmentCost `json:"shipping_cost"`
+ FulfillmentCost FulfillmentCost `json:"fulfillment_cost"`
+ TotalTax decimal.Decimal `json:"total_tax"`
+ TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
+ TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
+ TotalDiscount decimal.Decimal `json:"total_discount_amount"`
+ Currency string `json:"currency"`
}
-func (c *PrintJobCost) UnmarshalJSON(data []byte) error {
- var resp printJobCostResp
- if err := json.Unmarshal(data, &resp); err != nil {
- return err
- }
- if err := json.Unmarshal(resp.Addr, &c.Addr); err != nil {
- return err
- }
- var warnsAndSugg struct {
- Warnings []ShippingAddressWarning `json:"warnings"`
- Suggested ShippingAddress `json:"suggested_address"`
- }
- if err := json.Unmarshal(resp.Addr, &warnsAndSugg); err != nil {
- return err
+func (resp printJobCostResp) cost() PrintJobCost {
+ return PrintJobCost{
+ Fees: resp.Fees,
+ LineItemCosts: resp.LineItemCosts,
+ ShipCost: resp.ShipCost,
+ FulfillmentCost: resp.FulfillmentCost,
+ TotalTax: resp.TotalTax,
+ TotalCostExclTax: resp.TotalCostExclTax,
+ TotalCostInclTax: resp.TotalCostInclTax,
+ TotalDiscount: resp.TotalDiscount,
+ Currency: resp.Currency,
}
- c.SuggestedAddr = warnsAndSugg.Suggested
- c.AddrWarnings = warnsAndSugg.Warnings
- c.Fees = resp.Fees
- c.LineItemCosts = resp.LineItemCosts
- c.ShipCost = resp.ShipCost
- c.FulfillmentCost = resp.FulfillmentCost
- c.TotalTax = resp.TotalTax
- c.TotalCostExclTax = resp.TotalCostExclTax
- c.TotalCostInclTax = resp.TotalCostInclTax
- c.TotalDiscount = resp.TotalDiscount
- c.Currency = resp.Currency
- return nil
}