aboutsummaryrefslogtreecommitdiffstats
path: root/lulu.go
diff options
context:
space:
mode:
Diffstat (limited to 'lulu.go')
-rw-r--r--lulu.go51
1 files changed, 43 insertions, 8 deletions
diff --git a/lulu.go b/lulu.go
index a7a6cdd..197380d 100644
--- a/lulu.go
+++ b/lulu.go
@@ -24,7 +24,7 @@ const (
coverDimensionsPath = "/cover-dimensions"
validateCoverPath = "/validate-cover"
printJobCostPath = "/print-job-cost-calculations"
- printJobPath = " /print-jobs"
+ printJobsPath = "/print-jobs"
)
// ApiUrl is the location of the API server. It is set to the sandbox
@@ -180,28 +180,63 @@ func (c *Client) PrintJobCost(items []PrintJobCostLineItem, addr ShippingAddress
//
// https://api.lulu.com/docs/#tag/Print-Jobs/operation/Print-Jobs_list
func (c *Client) PrintJobs(queries ...PrintJobQuery) ([]PrintJob, error) {
- //q := parsePrintJobQueries(queries)
- //page := 1
+ var q printJobQueries
+ q.apply(queries...)
+ qvals := q.vals()
- // TODO
- return nil, fmt.Errorf("not implemented")
+ verify := func(v any) error {
+ resp := v.(*getPrintJobsResp)
+ if int(resp.Count) != len(resp.Results) {
+ return fmt.Errorf("count (%d) != len(results) (%d)", resp.Count, len(resp.Results))
+ }
+ return nil
+ }
+
+ var jobs []PrintJob
+ for page := 1; ; page++ {
+ qvals.Set("page", fmt.Sprint(page))
+ var resp getPrintJobsResp
+ if err := c.getQueryDecodeVerify(printJobsPath, qvals, &resp, verify); err != nil {
+ return jobs, pkgErr(err)
+ }
+ if len(resp.Results) > 0 {
+ jobs = append(jobs, resp.Results...)
+ } else {
+ return jobs, nil
+ }
+ }
}
// getDecode sends a GET request and unmarshals the response.
func (c *Client) getDecode(path string, v any) error {
+ verify := func(v any) error { return nil }
+ return c.getQueryDecodeVerify(path, nil, v, verify)
+}
+
+// getDecodeVerify sends a GET /path/?query request and unmarshals and
+// verifies the response.
+func (c *Client) getQueryDecodeVerify(path string, query url.Values, v any, verify func(any) error) error {
url, err := url.JoinPath(ApiUrl, path)
if err != nil {
return err
}
+ url += "?" + query.Encode()
+
resp, err := c.c.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- return errResp{resp}
+ return errRespStatus{resp}
}
- return decodeResponse(resp, v)
+ if err := decodeResponse(resp, v); err != nil {
+ return err
+ }
+ if err := verify(v); err != nil {
+ return errResp{resp, err}
+ }
+ return nil
}
// postDecode sends a POST request and unmarshals the response.
@@ -212,7 +247,7 @@ func (c *Client) postDecode(path string, payload any, wantStatus int, v any) err
}
defer resp.Body.Close()
if resp.StatusCode != wantStatus {
- return errResp{resp}
+ return errRespStatus{resp}
}
return decodeResponse(resp, v)
}