diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-05-11 16:16:24 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-05-11 16:16:24 -0400 |
| commit | af2de318402df1fd8d33192d71613c21c4ee96bf (patch) | |
| tree | 473c33981032bc73f5a5f4282ef8b1292333ce28 /lulu.go | |
| parent | fcfe73600c8548d45820176d58f1c8cfc2327810 (diff) | |
| download | lulu-af2de318402df1fd8d33192d71613c21c4ee96bf.zip | |
implement POST /print-job-cost-calculations
Diffstat (limited to 'lulu.go')
| -rw-r--r-- | lulu.go | 50 |
1 files changed, 45 insertions, 5 deletions
@@ -7,8 +7,10 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/url" + "os" "golang.org/x/oauth2/clientcredentials" ) @@ -21,6 +23,7 @@ const ( validateInteriorPath = "/validate-interior" coverDimensionsPath = "/cover-dimensions" validateCoverPath = "/validate-cover" + printJobCostPath = "/print-job-cost-calculations" ) // ApiUrl is the location of the API server. It is set to the sandbox @@ -28,6 +31,11 @@ const ( // you are ready to deploy. var ApiUrl = SandboxUrl +var ( + Debug = false // print debug info to stdout + debugLog = log.New(os.Stderr, "DEBUG ", log.LstdFlags|log.Llongfile) +) + type Client struct { c *http.Client } @@ -143,6 +151,29 @@ func (c *Client) GetCoverValidation(id uint) (CoverValidationRecord, error) { return rec, nil } +// PrintJobCost calculates the cost of a hypothetical print order without +// actually creating a print job. +// +// https://api.lulu.com/docs/#tag/Print-Job-Cost-Calculations/operation/Print-Job-cost-calculations_create +func (c *Client) PrintJobCost(items []PrintJobCostLineItem, addr ShippingAddress, shipOpt ShippingLevel) (PrintJobCost, error) { + reqAddr := printJobCostReqShipAddr{ + City: addr.City, + Country: addr.Country, + PostCode: addr.PostCode, + State: addr.State, + Street1: addr.Street1, + Phone: addr.Phone, + } + payload := printJobCostReq{items, reqAddr, shipOpt} + var cost PrintJobCost + err := c.postDecode(printJobCostPath, payload, http.StatusCreated, &cost) + if err != nil { + return cost, pkgErr(err) + } + return cost, nil +} + +// getDecode sends a GET request and unmarshals the response. func (c *Client) getDecode(path string, v any) error { url, err := url.JoinPath(ApiUrl, path) if err != nil { @@ -159,6 +190,7 @@ func (c *Client) getDecode(path string, v any) error { return decodeResponse(resp, v) } +// postDecode sends a POST request and unmarshals the response. func (c *Client) postDecode(path string, payload any, wantStatus int, v any) error { resp, err := c.post(path, payload) if err != nil { @@ -184,13 +216,21 @@ func (c *Client) post(path string, payload any) (*http.Response, error) { } func decodeResponse(resp *http.Response, v any) error { - buf := new(bytes.Buffer) - if _, err := io.Copy(buf, resp.Body); err != nil { + body, err := io.ReadAll(resp.Body) + if err != nil { return errReadResp{resp, err} } - dec := json.NewDecoder(buf) - if err := dec.Decode(v); err != nil { - return errDecResp{resp, buf.Bytes(), err} + + debugf("%s %s response: `%s`\n", resp.Request.Method, resp.Request.URL, body) + + if err := json.Unmarshal(body, v); err != nil { + return errDecResp{resp, body, err} } return nil } + +func debugf(format string, a ...any) { + if Debug { + debugLog.Printf(format, a...) + } +} |