Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit cbf5207

Browse files
author
Julien Neuhart
authored
Merge pull request #4 from thecodingmachine/5.0.0
updating for Gotenberg 5.0.0
2 parents 8fcda3b + e1bf4ab commit cbf5207

File tree

18 files changed

+254
-389
lines changed

18 files changed

+254
-389
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GOLANG_VERSION=1.12
2-
GOTENBERG_VERSION=4.2.1
2+
GOTENBERG_VERSION=5.0.0
33
VERSION=snapshot
44

55
# gofmt and goimports all go files.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ A simple Go client for interacting with a Gotenberg API.
55
## Install
66

77
```bash
8-
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v4
8+
$ go get -u github.com/thecodingmachine/gotenberg-go-client/v5
99
```
1010

1111
## Usage
1212

1313
```golang
14-
import "github.com/thecodingmachine/gotenberg-go-client/v4"
14+
import "github.com/thecodingmachine/gotenberg-go-client/v5"
1515

1616
func main() {
1717
// HTML conversion example.
1818
c := &gotenberg.Client{Hostname: "http://localhost:3000"}
1919
req, _ := gotenberg.NewHTMLRequest("index.html")
20-
req.SetHeader("header.html")
21-
req.SetFooter("footer.html")
22-
req.SetAssets(
20+
req.Header("header.html")
21+
req.Footer("footer.html")
22+
req.Assets(
2323
"font.woff",
2424
"img.gif",
2525
"style.css",
2626
)
27-
req.SetPaperSize(gotenberg.A4)
28-
req.SetMargins(gotenberg.NormalMargins)
29-
req.SetLandscape(false)
27+
req.PaperSize(gotenberg.A4)
28+
req.Margins(gotenberg.NormalMargins)
29+
req.Landscape(false)
3030
dest := "foo.pdf"
3131
c.Store(req, dest)
3232
}

build/lint/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ FROM golang:${GOLANG_VERSION}-stretch
1010
# | than gometalinter.
1111
# |
1212

13-
ENV GOLANGCI_LINT_VERSION 1.15.0
13+
ENV GOLANGCI_LINT_VERSION 1.16.0
1414

1515
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b /usr/local/bin v${GOLANGCI_LINT_VERSION} &&\
1616
golangci-lint --version
@@ -32,4 +32,4 @@ COPY go.sum .
3232
# Install module dependencies.
3333
RUN go mod download
3434

35-
CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl", "--disable=lll", "--disable=errcheck", "--disable=gosec", "--disable=gochecknoglobals", "--disable=gochecknoinits" ]
35+
CMD [ "golangci-lint", "run" ,"--tests=false", "--enable-all", "--disable=dupl" ]

build/tests/docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ set -xe
55
# Testing Go client.
66
gotenberg &
77
sleep 10
8-
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v4
8+
go test -race -cover -covermode=atomic github.com/thecodingmachine/gotenberg-go-client/v5
99
sleep 10 # allows Gotenberg to remove generated files (concurrent requests).

chrome.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package gotenberg
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
const (
9+
waitDelay string = "waitDelay"
10+
paperWidth string = "paperWidth"
11+
paperHeight string = "paperHeight"
12+
marginTop string = "marginTop"
13+
marginBottom string = "marginBottom"
14+
marginLeft string = "marginLeft"
15+
marginRight string = "marginRight"
16+
landscapeChrome string = "landscape"
17+
)
18+
19+
// nolint: gochecknoglobals
20+
var (
21+
// A3 paper size.
22+
A3 = [2]float64{11.7, 16.5}
23+
// A4 paper size.
24+
A4 = [2]float64{8.27, 11.7}
25+
// A5 paper size.
26+
A5 = [2]float64{5.8, 8.3}
27+
// A6 paper size.
28+
A6 = [2]float64{4.1, 5.8}
29+
// Letter paper size.
30+
Letter = [2]float64{8.5, 11}
31+
// Legal paper size.
32+
Legal = [2]float64{8.5, 14}
33+
// Tabloid paper size.
34+
Tabloid = [2]float64{11, 17}
35+
)
36+
37+
// nolint: gochecknoglobals
38+
var (
39+
// NoMargins removes margins.
40+
NoMargins = [4]float64{0, 0, 0, 0}
41+
// NormalMargins uses 1 inche margins.
42+
NormalMargins = [4]float64{1, 1, 1, 1}
43+
// LargeMargins uses 2 inche margins.
44+
LargeMargins = [4]float64{2, 2, 2, 2}
45+
)
46+
47+
type chromeRequest struct {
48+
headerFilePath string
49+
footerFilePath string
50+
51+
*request
52+
}
53+
54+
func newChromeRequest() *chromeRequest {
55+
return &chromeRequest{"", "", newRequest()}
56+
}
57+
58+
// WaitDelay sets waitDelay form field.
59+
func (req *chromeRequest) WaitDelay(delay float64) {
60+
req.values[waitDelay] = strconv.FormatFloat(delay, 'f', 2, 64)
61+
}
62+
63+
// Header sets header form file.
64+
func (req *chromeRequest) Header(fpath string) error {
65+
if !fileExists(fpath) {
66+
return fmt.Errorf("%s: header file does not exist", fpath)
67+
}
68+
req.headerFilePath = fpath
69+
return nil
70+
}
71+
72+
// Footer sets footer form file.
73+
func (req *chromeRequest) Footer(fpath string) error {
74+
if !fileExists(fpath) {
75+
return fmt.Errorf("%s: footer file does not exist", fpath)
76+
}
77+
req.footerFilePath = fpath
78+
return nil
79+
}
80+
81+
// PaperSize sets paperWidth and paperHeight form fields.
82+
func (req *chromeRequest) PaperSize(size [2]float64) {
83+
req.values[paperWidth] = fmt.Sprintf("%f", size[0])
84+
req.values[paperHeight] = fmt.Sprintf("%f", size[1])
85+
}
86+
87+
// Margins sets marginTop, marginBottom,
88+
// marginLeft and marginRight form fields.
89+
func (req *chromeRequest) Margins(margins [4]float64) {
90+
req.values[marginTop] = fmt.Sprintf("%f", margins[0])
91+
req.values[marginBottom] = fmt.Sprintf("%f", margins[1])
92+
req.values[marginLeft] = fmt.Sprintf("%f", margins[2])
93+
req.values[marginRight] = fmt.Sprintf("%f", margins[3])
94+
}
95+
96+
// Landscape sets landscape form field.
97+
func (req *chromeRequest) Landscape(isLandscape bool) {
98+
req.values[landscapeChrome] = strconv.FormatBool(isLandscape)
99+
}
100+
101+
func (req *chromeRequest) formFiles() map[string]string {
102+
files := make(map[string]string)
103+
files["header.html"] = req.headerFilePath
104+
files["footer.html"] = req.footerFilePath
105+
return files
106+
}

client.go

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,13 @@ import (
1010
"os"
1111
"path/filepath"
1212
"runtime"
13+
"strconv"
1314
)
1415

1516
const (
16-
remoteURL string = "remoteURL"
17-
webhookURL string = "webhookURL"
18-
paperWidth string = "paperWidth"
19-
paperHeight string = "paperHeight"
20-
marginTop string = "marginTop"
21-
marginBottom string = "marginBottom"
22-
marginLeft string = "marginLeft"
23-
marginRight string = "marginRight"
24-
landscape string = "landscape"
25-
webFontsTimeout string = "webFontsTimeout"
26-
)
27-
28-
var (
29-
// A3 paper size.
30-
A3 = [2]float64{11.7, 16.5}
31-
// A4 paper size.
32-
A4 = [2]float64{8.27, 11.7}
33-
// A5 paper size.
34-
A5 = [2]float64{5.8, 8.3}
35-
// A6 paper size.
36-
A6 = [2]float64{4.1, 5.8}
37-
// Letter paper size.
38-
Letter = [2]float64{8.5, 11}
39-
// Legal paper size.
40-
Legal = [2]float64{8.5, 14}
41-
// Tabloid paper size.
42-
Tabloid = [2]float64{11, 17}
43-
)
44-
45-
var (
46-
// NoMargins removes margins.
47-
NoMargins = [4]float64{0, 0, 0, 0}
48-
// NormalMargins uses 1 inche margins.
49-
NormalMargins = [4]float64{1, 1, 1, 1}
50-
// LargeMargins uses 2 inche margins.
51-
LargeMargins = [4]float64{2, 2, 2, 2}
17+
resultFilename string = "resultFilename"
18+
waitTimeout string = "waitTimeout"
19+
webhookURL string = "webhookURL"
5220
)
5321

5422
// Client facilitates interacting with
@@ -61,29 +29,38 @@ type Client struct {
6129
// form values and form files to
6230
// the Gotenberg API.
6331
type Request interface {
64-
SetWebhookURL(webhookURL string)
65-
getPostURL() string
66-
getFormValues() map[string]string
67-
getFormFiles() map[string]string
32+
postURL() string
33+
formValues() map[string]string
34+
formFiles() map[string]string
35+
}
36+
37+
type request struct {
38+
values map[string]string
39+
}
40+
41+
func newRequest() *request {
42+
return &request{
43+
values: make(map[string]string),
44+
}
45+
}
46+
47+
// ResultFilename sets resultFilename form field.
48+
func (req *request) ResultFilename(filename string) {
49+
req.values[resultFilename] = filename
50+
}
51+
52+
// WaitTiemout sets waitTimeout form field.
53+
func (req *request) WaitTimeout(timeout float64) {
54+
req.values[waitTimeout] = strconv.FormatFloat(timeout, 'f', 2, 64)
6855
}
6956

70-
// ChromeRequest is a type for sending
71-
// conversion requests which will be
72-
// handle by Google Chrome.
73-
type ChromeRequest interface {
74-
SetHeader(fpath string) error
75-
SetFooter(fpath string) error
76-
SetPaperSize(size [2]float64)
77-
SetMargins(margins [4]float64)
78-
SetLandscape(isLandscape bool)
79-
SetWebFontsTimeout(timeout int64)
57+
// WebhookURL sets webhookURL form field.
58+
func (req *request) WebhookURL(url string) {
59+
req.values[webhookURL] = url
8060
}
8161

82-
// UnoconvRequest is a type for sending
83-
// conversion requests which will be
84-
// handle by unoconv.
85-
type UnoconvRequest interface {
86-
SetLandscape(landscape bool)
62+
func (req *request) formValues() map[string]string {
63+
return req.values
8764
}
8865

8966
// Post sends a request to the Gotenberg API
@@ -93,8 +70,8 @@ func (c *Client) Post(req Request) (*http.Response, error) {
9370
if err != nil {
9471
return nil, err
9572
}
96-
URL := fmt.Sprintf("%s%s", c.Hostname, req.getPostURL())
97-
resp, err := http.Post(URL, contentType, body)
73+
URL := fmt.Sprintf("%s%s", c.Hostname, req.postURL())
74+
resp, err := http.Post(URL, contentType, body) /* #nosec */
9875
if err != nil {
9976
return nil, err
10077
}
@@ -114,7 +91,7 @@ func (c *Client) Store(req Request, dest string) error {
11491
}
11592

11693
func hasWebhook(req Request) bool {
117-
webhookURL, ok := req.getFormValues()[webhookURL]
94+
webhookURL, ok := req.formValues()[webhookURL]
11895
if !ok {
11996
return false
12097
}
@@ -129,7 +106,7 @@ func writeNewFile(fpath string, in io.Reader) error {
129106
if err != nil {
130107
return fmt.Errorf("%s: creating new file: %v", fpath, err)
131108
}
132-
defer out.Close()
109+
defer out.Close() // nolint: errcheck
133110
err = out.Chmod(0644)
134111
if err != nil && runtime.GOOS != "windows" {
135112
return fmt.Errorf("%s: changing file mode: %v", fpath, err)
@@ -149,8 +126,8 @@ func fileExists(name string) bool {
149126
func multipartForm(req Request) (*bytes.Buffer, string, error) {
150127
body := &bytes.Buffer{}
151128
writer := multipart.NewWriter(body)
152-
defer writer.Close()
153-
for filename, fpath := range req.getFormFiles() {
129+
defer writer.Close() // nolint: errcheck
130+
for filename, fpath := range req.formFiles() {
154131
// https://github.com/thecodingmachine/gotenberg-go-client/issues/3
155132
if fpath == "" {
156133
continue
@@ -159,6 +136,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
159136
if err != nil {
160137
return nil, "", fmt.Errorf("%s: opening file: %v", filename, err)
161138
}
139+
defer in.Close() // nolint: errcheck
162140
part, err := writer.CreateFormFile("files", filename)
163141
if err != nil {
164142
return nil, "", fmt.Errorf("%s: creating form file: %v", filename, err)
@@ -168,7 +146,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
168146
return nil, "", fmt.Errorf("%s: copying file: %v", filename, err)
169147
}
170148
}
171-
for name, value := range req.getFormValues() {
149+
for name, value := range req.formValues() {
172150
if err := writer.WriteField(name, value); err != nil {
173151
return nil, "", fmt.Errorf("%s: writing form field: %v", name, err)
174152
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
module github.com/thecodingmachine/gotenberg-go-client/v4
1+
module github.com/thecodingmachine/gotenberg-go-client/v5
22

33
go 1.12
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
76
github.com/stretchr/testify v1.3.0
7+
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1
88
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
66
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
77
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
88
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
9+
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1 h1:jHoid7JbIIOHflhCIZzGpnU5YD9VHnXyeZh39s3uSKc=
10+
github.com/thecodingmachine/gotenberg-go-client/v4 v4.3.1/go.mod h1:D3CcQY/dW6i38DL/hMiUMFwYESvXL4bs5zyyJYuHahw=

0 commit comments

Comments
 (0)