aboutsummaryrefslogtreecommitdiffstats
path: root/lulu.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2026-05-11 09:18:39 -0400
committerSam Anthony <sam@samanthony.xyz>2026-05-11 09:18:39 -0400
commitfcfe73600c8548d45820176d58f1c8cfc2327810 (patch)
tree33e0fa58d05eaf8dff546dca028aa78db456aef7 /lulu.go
parent5319e2af4c4a11c90323db7359e8edced6edc1fc (diff)
downloadlulu-fcfe73600c8548d45820176d58f1c8cfc2327810.zip
refactor
Diffstat (limited to 'lulu.go')
-rw-r--r--lulu.go86
1 files changed, 33 insertions, 53 deletions
diff --git a/lulu.go b/lulu.go
index 25f8c9c..9e567a7 100644
--- a/lulu.go
+++ b/lulu.go
@@ -75,18 +75,8 @@ func (c *Client) ValidateInteriorBasic(srcUrl string) (uint, error) {
}
func (c *Client) validateInterior(payload any) (uint, error) {
- resp, err := c.post(validateInteriorPath, payload)
- if err != nil {
- return 0, err
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusCreated {
- return 0, errResp{resp}
- }
-
var rec InteriorValidationRecord
- err = decodeResponse(resp, &rec)
+ err := c.postDecode(validateInteriorPath, payload, http.StatusCreated, &rec)
return rec.Id, err
}
@@ -95,22 +85,12 @@ func (c *Client) validateInterior(payload any) (uint, error) {
//
// https://api.lulu.com/docs/#tag/Files-validation/operation/Validate-Interior_read
func (c *Client) GetInteriorValidation(id uint) (InteriorValidationRecord, error) {
- url, err := url.JoinPath(ApiUrl, validateInteriorPath, fmt.Sprint(id))
+ path, err := url.JoinPath(validateInteriorPath, fmt.Sprint(id))
if err != nil {
return InteriorValidationRecord{}, pkgErr(err)
}
- resp, err := c.c.Get(url)
- if err != nil {
- return InteriorValidationRecord{}, pkgErr(err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return InteriorValidationRecord{}, pkgErr(errResp{resp})
- }
-
var rec InteriorValidationRecord
- if err := decodeResponse(resp, &rec); err != nil {
+ if err := c.getDecode(path, &rec); err != nil {
return InteriorValidationRecord{}, pkgErr(err)
}
return rec, nil
@@ -123,18 +103,9 @@ func (c *Client) GetInteriorValidation(id uint) (InteriorValidationRecord, error
// https://api.lulu.com/docs/#tag/Files-validation/operation/Cover-Dimensions_create
func (c *Client) CoverDimensions(mfg PkgId, npages uint, unit Unit) (CoverDimensions, error) {
payload := coverDimensionsReq{mfg, npages, unit}
- resp, err := c.post(coverDimensionsPath, payload)
- if err != nil {
- return CoverDimensions{}, pkgErr(err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusCreated {
- return CoverDimensions{}, errResp{resp}
- }
-
var dims CoverDimensions
- if err := decodeResponse(resp, &dims); err != nil {
+ err := c.postDecode(coverDimensionsPath, payload, http.StatusCreated, &dims)
+ if err != nil {
return CoverDimensions{}, pkgErr(err)
}
return dims, nil
@@ -148,18 +119,9 @@ func (c *Client) CoverDimensions(mfg PkgId, npages uint, unit Unit) (CoverDimens
// https://api.lulu.com/docs/#tag/Files-validation/operation/Validate-Cover_create
func (c *Client) ValidateCover(srcUrl string, mfg PkgId, npages uint) (uint, error) {
payload := validateCoverReq{srcUrl, mfg, npages}
- resp, err := c.post(validateCoverPath, payload)
- if err != nil {
- return 0, pkgErr(err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusCreated {
- return 0, errResp{resp}
- }
-
var rec CoverValidationRecord
- if err := decodeResponse(resp, &rec); err != nil {
+ err := c.postDecode(validateCoverPath, payload, http.StatusCreated, &rec)
+ if err != nil {
return 0, pkgErr(err)
}
return rec.Id, nil
@@ -170,25 +132,43 @@ func (c *Client) ValidateCover(srcUrl string, mfg PkgId, npages uint) (uint, err
//
// https://api.lulu.com/docs/#tag/Files-validation/operation/Validate-Cover_read
func (c *Client) GetCoverValidation(id uint) (CoverValidationRecord, error) {
- url, err := url.JoinPath(ApiUrl, validateCoverPath, fmt.Sprint(id))
+ path, err := url.JoinPath(validateCoverPath, fmt.Sprint(id))
if err != nil {
return CoverValidationRecord{}, pkgErr(err)
}
+ var rec CoverValidationRecord
+ if err := c.getDecode(path, &rec); err != nil {
+ return CoverValidationRecord{}, pkgErr(err)
+ }
+ return rec, nil
+}
+
+func (c *Client) getDecode(path string, v any) error {
+ url, err := url.JoinPath(ApiUrl, path)
+ if err != nil {
+ return err
+ }
resp, err := c.c.Get(url)
if err != nil {
- return CoverValidationRecord{}, pkgErr(err)
+ return err
}
defer resp.Body.Close()
-
if resp.StatusCode != http.StatusOK {
- return CoverValidationRecord{}, pkgErr(errResp{resp})
+ return errResp{resp}
}
+ return decodeResponse(resp, v)
+}
- var rec CoverValidationRecord
- if err := decodeResponse(resp, &rec); err != nil {
- return CoverValidationRecord{}, pkgErr(err)
+func (c *Client) postDecode(path string, payload any, wantStatus int, v any) error {
+ resp, err := c.post(path, payload)
+ if err != nil {
+ return err
}
- return rec, nil
+ defer resp.Body.Close()
+ if resp.StatusCode != wantStatus {
+ return errResp{resp}
+ }
+ return decodeResponse(resp, v)
}
func (c *Client) post(path string, payload any) (*http.Response, error) {