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
authoredApr 14, 2019
Merge pull request #4 from thecodingmachine/5.0.0
updating for Gotenberg 5.0.0
2 parents 8fcda3b + e1bf4ab commit cbf5207

18 files changed

+254
-389
lines changed
 

‎Makefile

+1-1
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

+8-8
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

+2-2
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

+1-1
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

+106
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

+40-62
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

+2-2
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

+2
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=

‎html.go

+12-67
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,46 @@ package gotenberg
33
import (
44
"fmt"
55
"path/filepath"
6-
"strconv"
76
)
87

98
// HTMLRequest facilitates HTML conversion
109
// with the Gotenberg API.
1110
type HTMLRequest struct {
1211
indexFilePath string
13-
headerFilePath string
14-
footerFilePath string
1512
assetFilePaths []string
16-
values map[string]string
13+
14+
*chromeRequest
1715
}
1816

1917
// NewHTMLRequest create HTMLRequest.
2018
func NewHTMLRequest(indexFilePath string) (*HTMLRequest, error) {
2119
if !fileExists(indexFilePath) {
2220
return nil, fmt.Errorf("%s: index file does not exist", indexFilePath)
2321
}
24-
return &HTMLRequest{indexFilePath: indexFilePath, values: make(map[string]string)}, nil
25-
}
26-
27-
// SetWebhookURL sets webhookURL form field.
28-
func (html *HTMLRequest) SetWebhookURL(webhookURL string) {
29-
html.values[webhookURL] = webhookURL
30-
}
31-
32-
// SetHeader sets header form file.
33-
func (html *HTMLRequest) SetHeader(fpath string) error {
34-
if !fileExists(fpath) {
35-
return fmt.Errorf("%s: header file does not exist", fpath)
36-
}
37-
html.headerFilePath = fpath
38-
return nil
39-
}
40-
41-
// SetFooter sets footer form file.
42-
func (html *HTMLRequest) SetFooter(fpath string) error {
43-
if !fileExists(fpath) {
44-
return fmt.Errorf("%s: footer file does not exist", fpath)
45-
}
46-
html.footerFilePath = fpath
47-
return nil
22+
return &HTMLRequest{indexFilePath, nil, newChromeRequest()}, nil
4823
}
4924

50-
// SetAssets sets assets form files.
51-
func (html *HTMLRequest) SetAssets(fpaths ...string) error {
25+
// Assets sets assets form files.
26+
func (req *HTMLRequest) Assets(fpaths ...string) error {
5227
for _, fpath := range fpaths {
5328
if !fileExists(fpath) {
5429
return fmt.Errorf("%s: file does not exist", fpath)
5530
}
5631
}
57-
html.assetFilePaths = fpaths
32+
req.assetFilePaths = fpaths
5833
return nil
5934
}
6035

61-
// SetPaperSize sets paperWidth and paperHeight form fields.
62-
func (html *HTMLRequest) SetPaperSize(size [2]float64) {
63-
html.values[paperWidth] = fmt.Sprintf("%f", size[0])
64-
html.values[paperHeight] = fmt.Sprintf("%f", size[1])
65-
}
66-
67-
// SetMargins sets marginTop, marginBottom,
68-
// marginLeft and marginRight form fields.
69-
func (html *HTMLRequest) SetMargins(margins [4]float64) {
70-
html.values[marginTop] = fmt.Sprintf("%f", margins[0])
71-
html.values[marginBottom] = fmt.Sprintf("%f", margins[1])
72-
html.values[marginLeft] = fmt.Sprintf("%f", margins[2])
73-
html.values[marginRight] = fmt.Sprintf("%f", margins[3])
74-
}
75-
76-
// SetLandscape sets landscape form field.
77-
func (html *HTMLRequest) SetLandscape(isLandscape bool) {
78-
html.values[landscape] = strconv.FormatBool(isLandscape)
79-
}
80-
81-
// SetWebFontsTimeout sets webFontsTimeout form field.
82-
func (html *HTMLRequest) SetWebFontsTimeout(timeout int64) {
83-
html.values[webFontsTimeout] = strconv.FormatInt(timeout, 10)
84-
}
85-
86-
func (html *HTMLRequest) getPostURL() string {
36+
func (req *HTMLRequest) postURL() string {
8737
return "/convert/html"
8838
}
8939

90-
func (html *HTMLRequest) getFormValues() map[string]string {
91-
return html.values
92-
}
93-
94-
func (html *HTMLRequest) getFormFiles() map[string]string {
40+
func (req *HTMLRequest) formFiles() map[string]string {
9541
files := make(map[string]string)
96-
files["index.html"] = html.indexFilePath
97-
files["header.html"] = html.headerFilePath
98-
files["footer.html"] = html.footerFilePath
99-
for _, fpath := range html.assetFilePaths {
42+
files["index.html"] = req.indexFilePath
43+
files["header.html"] = req.headerFilePath
44+
files["footer.html"] = req.footerFilePath
45+
for _, fpath := range req.assetFilePaths {
10046
files[filepath.Base(fpath)] = fpath
10147
}
10248
return files
@@ -105,5 +51,4 @@ func (html *HTMLRequest) getFormFiles() map[string]string {
10551
// Compile-time checks to ensure type implements desired interfaces.
10652
var (
10753
_ = Request(new(HTMLRequest))
108-
_ = ChromeRequest(new(HTMLRequest))
10954
)

‎html_test.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"github.com/thecodingmachine/gotenberg-go-client/v4/test"
10+
"github.com/thecodingmachine/gotenberg-go-client/v5/test"
1111
)
1212

1313
func TestHTML(t *testing.T) {
1414
c := &Client{Hostname: "http://localhost:3000"}
1515
req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html"))
1616
require.Nil(t, err)
17-
err = req.SetHeader(test.HTMLTestFilePath(t, "header.html"))
17+
req.ResultFilename("foo.pdf")
18+
req.WaitTimeout(5)
19+
req.WaitDelay(1)
20+
err = req.Header(test.HTMLTestFilePath(t, "header.html"))
1821
require.Nil(t, err)
19-
err = req.SetFooter(test.HTMLTestFilePath(t, "footer.html"))
22+
err = req.Footer(test.HTMLTestFilePath(t, "footer.html"))
2023
require.Nil(t, err)
21-
err = req.SetAssets(
24+
err = req.Assets(
2225
test.HTMLTestFilePath(t, "font.woff"),
2326
test.HTMLTestFilePath(t, "img.gif"),
2427
test.HTMLTestFilePath(t, "style.css"),
2528
)
2629
require.Nil(t, err)
27-
req.SetPaperSize(A4)
28-
req.SetMargins(NormalMargins)
29-
req.SetLandscape(false)
30+
req.PaperSize(A4)
31+
req.Margins(NormalMargins)
32+
req.Landscape(false)
3033
dirPath, err := test.Rand()
3134
require.Nil(t, err)
3235
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -41,9 +44,9 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) {
4144
c := &Client{Hostname: "http://localhost:3000"}
4245
req, err := NewHTMLRequest(test.HTMLTestFilePath(t, "index.html"))
4346
require.Nil(t, err)
44-
req.SetPaperSize(A4)
45-
req.SetMargins(NormalMargins)
46-
req.SetLandscape(false)
47+
req.PaperSize(A4)
48+
req.Margins(NormalMargins)
49+
req.Landscape(false)
4750
dirPath, err := test.Rand()
4851
require.Nil(t, err)
4952
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -53,11 +56,3 @@ func TestHTMLWithoutHeaderFooter(t *testing.T) {
5356
err = os.RemoveAll(dirPath)
5457
assert.Nil(t, err)
5558
}
56-
57-
func TestConcurrentHTML(t *testing.T) {
58-
for i := 0; i < 10; i++ {
59-
go func() {
60-
TestHTML(t)
61-
}()
62-
}
63-
}

‎markdown.go

+13-72
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ package gotenberg
33
import (
44
"fmt"
55
"path/filepath"
6-
"strconv"
76
)
87

98
// MarkdownRequest facilitates Markdown conversion
109
// with the Gotenberg API.
1110
type MarkdownRequest struct {
1211
indexFilePath string
1312
markdownFilePaths []string
14-
headerFilePath string
15-
footerFilePath string
1613
assetFilePaths []string
17-
values map[string]string
14+
15+
*chromeRequest
1816
}
1917

2018
// NewMarkdownRequest create MarkdownRequest.
@@ -27,89 +25,33 @@ func NewMarkdownRequest(indexFilePath string, markdownFilePaths ...string) (*Mar
2725
return nil, fmt.Errorf("%s: markdown file does not exist", fpath)
2826
}
2927
}
30-
return &MarkdownRequest{
31-
indexFilePath: indexFilePath,
32-
markdownFilePaths: markdownFilePaths,
33-
values: make(map[string]string),
34-
}, nil
35-
}
36-
37-
// SetWebhookURL sets webhookURL form field.
38-
func (markdown *MarkdownRequest) SetWebhookURL(webhookURL string) {
39-
markdown.values[webhookURL] = webhookURL
40-
}
41-
42-
// SetHeader sets header form file.
43-
func (markdown *MarkdownRequest) SetHeader(fpath string) error {
44-
if !fileExists(fpath) {
45-
return fmt.Errorf("%s: header file does not exist", fpath)
46-
}
47-
markdown.headerFilePath = fpath
48-
return nil
49-
}
50-
51-
// SetFooter sets footer form file.
52-
func (markdown *MarkdownRequest) SetFooter(fpath string) error {
53-
if !fileExists(fpath) {
54-
return fmt.Errorf("%s: footer file does not exist", fpath)
55-
}
56-
markdown.footerFilePath = fpath
57-
return nil
28+
return &MarkdownRequest{indexFilePath, markdownFilePaths, nil, newChromeRequest()}, nil
5829
}
5930

60-
// SetAssets sets assets form files.
61-
func (markdown *MarkdownRequest) SetAssets(fpaths ...string) error {
31+
// Assets sets assets form files.
32+
func (req *MarkdownRequest) Assets(fpaths ...string) error {
6233
for _, fpath := range fpaths {
6334
if !fileExists(fpath) {
6435
return fmt.Errorf("%s: file does not exist", fpath)
6536
}
6637
}
67-
markdown.assetFilePaths = fpaths
38+
req.assetFilePaths = fpaths
6839
return nil
6940
}
7041

71-
// SetPaperSize sets paperWidth and paperHeight form fields.
72-
func (markdown *MarkdownRequest) SetPaperSize(size [2]float64) {
73-
markdown.values[paperWidth] = fmt.Sprintf("%f", size[0])
74-
markdown.values[paperHeight] = fmt.Sprintf("%f", size[1])
75-
}
76-
77-
// SetMargins sets marginTop, marginBottom,
78-
// marginLeft and marginRight form fields.
79-
func (markdown *MarkdownRequest) SetMargins(margins [4]float64) {
80-
markdown.values[marginTop] = fmt.Sprintf("%f", margins[0])
81-
markdown.values[marginBottom] = fmt.Sprintf("%f", margins[1])
82-
markdown.values[marginLeft] = fmt.Sprintf("%f", margins[2])
83-
markdown.values[marginRight] = fmt.Sprintf("%f", margins[3])
84-
}
85-
86-
// SetLandscape sets landscape form field.
87-
func (markdown *MarkdownRequest) SetLandscape(isLandscape bool) {
88-
markdown.values[landscape] = strconv.FormatBool(isLandscape)
89-
}
90-
91-
// SetWebFontsTimeout sets webFontsTimeout form field.
92-
func (markdown *MarkdownRequest) SetWebFontsTimeout(timeout int64) {
93-
markdown.values[webFontsTimeout] = strconv.FormatInt(timeout, 10)
94-
}
95-
96-
func (markdown *MarkdownRequest) getPostURL() string {
42+
func (req *MarkdownRequest) postURL() string {
9743
return "/convert/markdown"
9844
}
9945

100-
func (markdown *MarkdownRequest) getFormValues() map[string]string {
101-
return markdown.values
102-
}
103-
104-
func (markdown *MarkdownRequest) getFormFiles() map[string]string {
46+
func (req *MarkdownRequest) formFiles() map[string]string {
10547
files := make(map[string]string)
106-
files["index.html"] = markdown.indexFilePath
107-
files["header.html"] = markdown.headerFilePath
108-
files["footer.html"] = markdown.footerFilePath
109-
for _, fpath := range markdown.markdownFilePaths {
48+
files["index.html"] = req.indexFilePath
49+
files["header.html"] = req.headerFilePath
50+
files["footer.html"] = req.footerFilePath
51+
for _, fpath := range req.markdownFilePaths {
11052
files[filepath.Base(fpath)] = fpath
11153
}
112-
for _, fpath := range markdown.assetFilePaths {
54+
for _, fpath := range req.assetFilePaths {
11355
files[filepath.Base(fpath)] = fpath
11456
}
11557
return files
@@ -118,5 +60,4 @@ func (markdown *MarkdownRequest) getFormFiles() map[string]string {
11860
// Compile-time checks to ensure type implements desired interfaces.
11961
var (
12062
_ = Request(new(MarkdownRequest))
121-
_ = ChromeRequest(new(MarkdownRequest))
12263
)

‎markdown_test.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"github.com/thecodingmachine/gotenberg-go-client/v4/test"
10+
"github.com/thecodingmachine/gotenberg-go-client/v5/test"
1111
)
1212

1313
func TestMarkdown(t *testing.T) {
@@ -19,19 +19,22 @@ func TestMarkdown(t *testing.T) {
1919
test.MarkdownTestFilePath(t, "paragraph3.md"),
2020
)
2121
require.Nil(t, err)
22-
err = req.SetHeader(test.MarkdownTestFilePath(t, "header.html"))
22+
req.ResultFilename("foo.pdf")
23+
req.WaitTimeout(5)
24+
req.WaitDelay(1)
25+
err = req.Header(test.MarkdownTestFilePath(t, "header.html"))
2326
require.Nil(t, err)
24-
err = req.SetFooter(test.MarkdownTestFilePath(t, "footer.html"))
27+
err = req.Footer(test.MarkdownTestFilePath(t, "footer.html"))
2528
require.Nil(t, err)
26-
err = req.SetAssets(
29+
err = req.Assets(
2730
test.MarkdownTestFilePath(t, "font.woff"),
2831
test.MarkdownTestFilePath(t, "img.gif"),
2932
test.MarkdownTestFilePath(t, "style.css"),
3033
)
3134
require.Nil(t, err)
32-
req.SetPaperSize(A4)
33-
req.SetMargins(NormalMargins)
34-
req.SetLandscape(false)
35+
req.PaperSize(A4)
36+
req.Margins(NormalMargins)
37+
req.Landscape(false)
3538
dirPath, err := test.Rand()
3639
require.Nil(t, err)
3740
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -51,9 +54,9 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) {
5154
test.MarkdownTestFilePath(t, "paragraph3.md"),
5255
)
5356
require.Nil(t, err)
54-
req.SetPaperSize(A4)
55-
req.SetMargins(NormalMargins)
56-
req.SetLandscape(false)
57+
req.PaperSize(A4)
58+
req.Margins(NormalMargins)
59+
req.Landscape(false)
5760
dirPath, err := test.Rand()
5861
require.Nil(t, err)
5962
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -63,11 +66,3 @@ func TestMarkdownWithoutHeaderFooter(t *testing.T) {
6366
err = os.RemoveAll(dirPath)
6467
assert.Nil(t, err)
6568
}
66-
67-
func TestConcurrentMarkdown(t *testing.T) {
68-
for i := 0; i < 10; i++ {
69-
go func() {
70-
TestMarkdown(t)
71-
}()
72-
}
73-
}

‎merge.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
// with the Gotenberg API.
1010
type MergeRequest struct {
1111
filePaths []string
12-
values map[string]string
12+
13+
*request
1314
}
1415

1516
// NewMergeRequest create MergeRequest.
@@ -19,25 +20,16 @@ func NewMergeRequest(fpaths ...string) (*MergeRequest, error) {
1920
return nil, fmt.Errorf("%s: file does not exist", fpath)
2021
}
2122
}
22-
return &MergeRequest{filePaths: fpaths, values: make(map[string]string)}, nil
23-
}
24-
25-
// SetWebhookURL sets webhookURL form field.
26-
func (merge *MergeRequest) SetWebhookURL(webhookURL string) {
27-
merge.values[webhookURL] = webhookURL
28-
}
29-
30-
func (merge *MergeRequest) getPostURL() string {
31-
return "/merge"
23+
return &MergeRequest{fpaths, newRequest()}, nil
3224
}
3325

34-
func (merge *MergeRequest) getFormValues() map[string]string {
35-
return merge.values
26+
func (req *MergeRequest) postURL() string {
27+
return "/convert/merge"
3628
}
3729

38-
func (merge *MergeRequest) getFormFiles() map[string]string {
30+
func (req *MergeRequest) formFiles() map[string]string {
3931
files := make(map[string]string)
40-
for _, fpath := range merge.filePaths {
32+
for _, fpath := range req.filePaths {
4133
files[filepath.Base(fpath)] = fpath
4234
}
4335
return files

‎merge_test.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"github.com/thecodingmachine/gotenberg-go-client/v4/test"
10+
"github.com/thecodingmachine/gotenberg-go-client/v5/test"
1111
)
1212

1313
func TestMerge(t *testing.T) {
@@ -17,6 +17,8 @@ func TestMerge(t *testing.T) {
1717
test.PDFTestFilePath(t, "gotenberg.pdf"),
1818
)
1919
require.Nil(t, err)
20+
req.ResultFilename("foo.pdf")
21+
req.WaitTimeout(5)
2022
dirPath, err := test.Rand()
2123
require.Nil(t, err)
2224
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -26,11 +28,3 @@ func TestMerge(t *testing.T) {
2628
err = os.RemoveAll(dirPath)
2729
assert.Nil(t, err)
2830
}
29-
30-
func TestConcurrentMerge(t *testing.T) {
31-
for i := 0; i < 10; i++ {
32-
go func() {
33-
TestMerge(t)
34-
}()
35-
}
36-
}

‎office.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import (
66
"strconv"
77
)
88

9+
const landscapeOffice string = "landscape"
10+
911
// OfficeRequest facilitates Office documents
1012
// conversion with the Gotenberg API.
1113
type OfficeRequest struct {
1214
filePaths []string
13-
values map[string]string
15+
16+
*request
1417
}
1518

1619
// NewOfficeRequest create OfficeRequest.
@@ -20,30 +23,21 @@ func NewOfficeRequest(fpaths ...string) (*OfficeRequest, error) {
2023
return nil, fmt.Errorf("%s: file does not exist", fpath)
2124
}
2225
}
23-
return &OfficeRequest{filePaths: fpaths, values: make(map[string]string)}, nil
24-
}
25-
26-
// SetWebhookURL sets webhookURL form field.
27-
func (office *OfficeRequest) SetWebhookURL(webhookURL string) {
28-
office.values[webhookURL] = webhookURL
26+
return &OfficeRequest{fpaths, newRequest()}, nil
2927
}
3028

31-
// SetLandscape sets landscape form field.
32-
func (office *OfficeRequest) SetLandscape(isLandscape bool) {
33-
office.values[landscape] = strconv.FormatBool(isLandscape)
29+
// Landscape sets landscape form field.
30+
func (req *OfficeRequest) Landscape(isLandscape bool) {
31+
req.values[landscapeOffice] = strconv.FormatBool(isLandscape)
3432
}
3533

36-
func (office *OfficeRequest) getPostURL() string {
34+
func (req *OfficeRequest) postURL() string {
3735
return "/convert/office"
3836
}
3937

40-
func (office *OfficeRequest) getFormValues() map[string]string {
41-
return office.values
42-
}
43-
44-
func (office *OfficeRequest) getFormFiles() map[string]string {
38+
func (req *OfficeRequest) formFiles() map[string]string {
4539
files := make(map[string]string)
46-
for _, fpath := range office.filePaths {
40+
for _, fpath := range req.filePaths {
4741
files[filepath.Base(fpath)] = fpath
4842
}
4943
return files
@@ -52,5 +46,4 @@ func (office *OfficeRequest) getFormFiles() map[string]string {
5246
// Compile-time checks to ensure type implements desired interfaces.
5347
var (
5448
_ = Request(new(OfficeRequest))
55-
_ = UnoconvRequest(new(OfficeRequest))
5649
)

‎office_test.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"github.com/thecodingmachine/gotenberg-go-client/v4/test"
10+
"github.com/thecodingmachine/gotenberg-go-client/v5/test"
1111
)
1212

1313
func TestOffice(t *testing.T) {
1414
c := &Client{Hostname: "http://localhost:3000"}
1515
req, err := NewOfficeRequest(test.OfficeTestFilePath(t, "document.docx"))
1616
require.Nil(t, err)
17-
req.SetLandscape(false)
17+
req.ResultFilename("foo.pdf")
18+
req.WaitTimeout(5)
19+
req.Landscape(false)
1820
dirPath, err := test.Rand()
1921
require.Nil(t, err)
2022
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -24,11 +26,3 @@ func TestOffice(t *testing.T) {
2426
err = os.RemoveAll(dirPath)
2527
assert.Nil(t, err)
2628
}
27-
28-
func TestConcurrentOffice(t *testing.T) {
29-
for i := 0; i < 10; i++ {
30-
go func() {
31-
TestOffice(t)
32-
}()
33-
}
34-
}

‎url.go

+4-69
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,25 @@
11
package gotenberg
22

3-
import (
4-
"fmt"
5-
"strconv"
6-
)
3+
const remoteURL string = "remoteURL"
74

85
// URLRequest facilitates remote URL conversion
96
// with the Gotenberg API.
107
type URLRequest struct {
11-
headerFilePath string
12-
footerFilePath string
13-
values map[string]string
8+
*chromeRequest
149
}
1510

1611
// NewURLRequest create URLRequest.
1712
func NewURLRequest(url string) *URLRequest {
18-
req := &URLRequest{values: make(map[string]string)}
13+
req := &URLRequest{newChromeRequest()}
1914
req.values[remoteURL] = url
2015
return req
2116
}
2217

23-
// SetWebhookURL sets webhookURL form field.
24-
func (url *URLRequest) SetWebhookURL(webhookURL string) {
25-
url.values[webhookURL] = webhookURL
26-
}
27-
28-
// SetHeader sets header form file.
29-
func (url *URLRequest) SetHeader(fpath string) error {
30-
if !fileExists(fpath) {
31-
return fmt.Errorf("%s: header file does not exist", fpath)
32-
}
33-
url.headerFilePath = fpath
34-
return nil
35-
}
36-
37-
// SetFooter sets footer form file.
38-
func (url *URLRequest) SetFooter(fpath string) error {
39-
if !fileExists(fpath) {
40-
return fmt.Errorf("%s: footer file does not exist", fpath)
41-
}
42-
url.footerFilePath = fpath
43-
return nil
44-
}
45-
46-
// SetPaperSize sets paperWidth and paperHeight form fields.
47-
func (url *URLRequest) SetPaperSize(size [2]float64) {
48-
url.values[paperWidth] = fmt.Sprintf("%f", size[0])
49-
url.values[paperHeight] = fmt.Sprintf("%f", size[1])
50-
}
51-
52-
// SetMargins sets marginTop, marginBottom,
53-
// marginLeft and marginRight form fields.
54-
func (url *URLRequest) SetMargins(margins [4]float64) {
55-
url.values[marginTop] = fmt.Sprintf("%f", margins[0])
56-
url.values[marginBottom] = fmt.Sprintf("%f", margins[1])
57-
url.values[marginLeft] = fmt.Sprintf("%f", margins[2])
58-
url.values[marginRight] = fmt.Sprintf("%f", margins[3])
59-
}
60-
61-
// SetLandscape sets landscape form field.
62-
func (url *URLRequest) SetLandscape(isLandscape bool) {
63-
url.values[landscape] = strconv.FormatBool(isLandscape)
64-
}
65-
66-
// SetWebFontsTimeout sets webFontsTimeout form field.
67-
func (url *URLRequest) SetWebFontsTimeout(timeout int64) {
68-
url.values[webFontsTimeout] = strconv.FormatInt(timeout, 10)
69-
}
70-
71-
func (url *URLRequest) getPostURL() string {
18+
func (url *URLRequest) postURL() string {
7219
return "/convert/url"
7320
}
7421

75-
func (url *URLRequest) getFormValues() map[string]string {
76-
return url.values
77-
}
78-
79-
func (url *URLRequest) getFormFiles() map[string]string {
80-
files := make(map[string]string)
81-
files["header.html"] = url.headerFilePath
82-
files["footer.html"] = url.footerFilePath
83-
return files
84-
}
85-
8622
// Compile-time checks to ensure type implements desired interfaces.
8723
var (
8824
_ = Request(new(URLRequest))
89-
_ = ChromeRequest(new(URLRequest))
9025
)

‎url_test.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import (
77

88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"github.com/thecodingmachine/gotenberg-go-client/v4/test"
10+
"github.com/thecodingmachine/gotenberg-go-client/v5/test"
1111
)
1212

1313
func TestURL(t *testing.T) {
1414
c := &Client{Hostname: "http://localhost:3000"}
1515
req := NewURLRequest("http://google.com")
16-
err := req.SetHeader(test.URLTestFilePath(t, "header.html"))
16+
req.ResultFilename("foo.pdf")
17+
req.WaitTimeout(5)
18+
req.WaitDelay(1)
19+
err := req.Header(test.URLTestFilePath(t, "header.html"))
1720
require.Nil(t, err)
18-
err = req.SetFooter(test.URLTestFilePath(t, "footer.html"))
21+
err = req.Footer(test.URLTestFilePath(t, "footer.html"))
1922
require.Nil(t, err)
20-
req.SetPaperSize(A4)
21-
req.SetMargins(NormalMargins)
22-
req.SetLandscape(false)
23+
req.PaperSize(A4)
24+
req.Margins(NormalMargins)
25+
req.Landscape(false)
2326
dirPath, err := test.Rand()
2427
require.Nil(t, err)
2528
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -33,9 +36,9 @@ func TestURL(t *testing.T) {
3336
func TestURLWithoutHeaderFooter(t *testing.T) {
3437
c := &Client{Hostname: "http://localhost:3000"}
3538
req := NewURLRequest("http://google.com")
36-
req.SetPaperSize(A4)
37-
req.SetMargins(NormalMargins)
38-
req.SetLandscape(false)
39+
req.PaperSize(A4)
40+
req.Margins(NormalMargins)
41+
req.Landscape(false)
3942
dirPath, err := test.Rand()
4043
require.Nil(t, err)
4144
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
@@ -45,11 +48,3 @@ func TestURLWithoutHeaderFooter(t *testing.T) {
4548
err = os.RemoveAll(dirPath)
4649
assert.Nil(t, err)
4750
}
48-
49-
func TestConcurrentURL(t *testing.T) {
50-
for i := 0; i < 10; i++ {
51-
go func() {
52-
TestURL(t)
53-
}()
54-
}
55-
}

0 commit comments

Comments
 (0)
This repository has been archived.