context.Context, and errors work with errors.Is and errors.As.
- Package:
github.com/jaapieaapie1/zpl/clients/goon pkg.go.dev - Source:
clients/go
Install
caretly. Import it with an explicit name so the path reads clearly:
Quickstart
Create a test key in the console and set it in your environment:nil options to use the server defaults: a PDF at 8 dots per millimetre.
Use a context
The context covers the whole call, including the waits between retries. Use it to set a deadline or cancel a render:context.DeadlineExceeded. The client’s own timeout, set with WithTimeout, limits each attempt separately.
Render a page as PNG
A PNG holds one page. Choose it withPage, starting at 1:
client.RenderPDF(ctx, zpl) is the matching shorthand for a PDF.
Render several files
RenderFiles sends the files as multipart/form-data. Each file starts from a fresh printer state, and pages follow the order of the files:
Render options
RenderOptions fields left at their zero value aren’t sent, so the server defaults apply:
Invalid values fail before any request with an error that matches
caretly.ErrInvalidOptions. An empty file list does too.
Read the result
*RenderResult has these fields:
Handle errors
An error status returns an*caretly.APIError with StatusCode, Message (the server’s plain-text message) and RetryAfter (zero when absent). It matches a sentinel per status:
A transport failure returns a
*caretly.TransportError. It matches caretly.ErrTimeout when an attempt ran over the client timeout, and caretly.ErrConnection otherwise.
Retries and idempotency
The client retries429, 503 and connection errors up to WithMaxRetries times, 2 by default. It waits Retry-After when the server sends it. Otherwise it backs off 0.5 s, 1 s, 2 s, up to 8 s. Timeouts and other statuses aren’t retried.
When retries are on and you set no IdempotencyKey, the client generates a UUID once per call and sends it on every attempt, so a retried request is never billed twice.
Set your own key, such as an order number, to make retries across calls or processes safe too: