diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-05-13 10:44:57 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-05-13 10:45:41 -0400 |
| commit | 56523fc6a304c09f252449342b10c52f7836c4aa (patch) | |
| tree | 07cd85a92ba3c1393805a938fbc27b8ad18162b5 /lulu.go | |
| parent | 520f392ca5207f5364bbed501615613e3485b84b (diff) | |
| download | lulu-56523fc6a304c09f252449342b10c52f7836c4aa.zip | |
create print job
Diffstat (limited to 'lulu.go')
| -rw-r--r-- | lulu.go | 66 |
1 files changed, 63 insertions, 3 deletions
@@ -11,6 +11,7 @@ import ( "net/http" "net/url" "os" + "time" "golang.org/x/oauth2/clientcredentials" ) @@ -175,11 +176,11 @@ func (c *Client) PrintJobCost(items []PrintJobCostLineItem, addr ShippingAddress return resp.cost(), resp.AddressValidation, nil } -// PrintJobs retrieves a list of all print jobs that have been created, -// filtered by a set of optional query parameters. +// GetPrintJobs retrieves a list of all print jobs that have been +// created, filtered by a set of optional query parameters. // // https://api.lulu.com/docs/#tag/Print-Jobs/operation/Print-Jobs_list -func (c *Client) PrintJobs(queries ...PrintJobQuery) ([]PrintJob, error) { +func (c *Client) GetPrintJobs(queries ...PrintJobQuery) ([]PrintJob, error) { var q printJobQueries q.apply(queries...) qvals := q.vals() @@ -207,6 +208,62 @@ func (c *Client) PrintJobs(queries ...PrintJobQuery) ([]PrintJob, error) { } } +// Print creates a new print job. +// +// contact: Email address that should be contacted if questions +// regarding the Print-Job arise. Lulu recommends to use the +// email of a person who is responsible for placing the Print-Job +// like a developer or business owner. +// +// externalId: Arbitrary string to identify and connect a print +// job to your systems. Set it to an order number, a purchase +// order or whatever else works for your particular use case. +// +// productionDelay: Delay before a newly created Print-Job is +// sent to production. Minimum is 60 minutes, maximum is 2880 +// minutes (=48 hours). As most cancellation requests occur right +// after an order has been placed, it makes sense to wait for +// some time before sending an order to production. Once +// production has started, orders cannot be canceled anymore. +// +// addr: The postal address of the customer that the order will +// be sent to. +// +// shipOpt: Shipping method. +// +// items: List of books to print. +// +// https://api.lulu.com/docs/#tag/Print-Jobs/operation/Print-Jobs_create +func (c *Client) Print(contact EmailAddress, externalId string, productionDelay time.Duration, addr ShippingAddress, shipOpt ShippingLevel, items []Printable) (PrintJob, error) { + if err := verifyProductionDelay(productionDelay); err != nil { + return PrintJob{}, pkgErr(err) + } + + req := printReq{ + Contact: contact, + ExternalId: externalId, + LineItems: items, + ProductionDelayMins: uint(productionDelay.Round(time.Minute).Minutes()), + ShipAddr: addr, + ShipOpt: shipOpt, + } + + var job PrintJob + err := c.postDecode(printJobsPath, req, http.StatusCreated, &job) + if err != nil { + return job, pkgErr(err) + } + return job, nil +} + +func verifyProductionDelay(delay time.Duration) error { + if delay < MinProductionDelay || delay > MaxProductionDelay { + return fmt.Errorf("production delay %s out of range: must be [%s, %s]", + delay, MinProductionDelay, MaxProductionDelay) + } + return 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 } @@ -261,6 +318,9 @@ func (c *Client) post(path string, payload any) (*http.Response, error) { if err != nil { return nil, err } + + debugf("POST %s request: `%s`\n", url, body) + return c.c.Post(url, "application/json", bytes.NewBuffer(body)) } |