@@ -10,45 +10,14 @@ import (
1010 "os"
1111 "path/filepath"
1212 "runtime"
13+ "strconv"
1314)
1415
1516const (
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"
20+ webhookURLTimeout string = "webhookURLTimeout"
5221)
5322
5423// Client facilitates interacting with
@@ -61,29 +30,43 @@ type Client struct {
6130// form values and form files to
6231// the Gotenberg API.
6332type Request interface {
64- SetWebhookURL (webhookURL string )
65- getPostURL () string
66- getFormValues () map [string ]string
67- getFormFiles () map [string ]string
33+ postURL () string
34+ formValues () map [string ]string
35+ formFiles () map [string ]string
36+ }
37+
38+ type request struct {
39+ values map [string ]string
40+ }
41+
42+ func newRequest () * request {
43+ return & request {
44+ values : make (map [string ]string ),
45+ }
46+ }
47+
48+ // ResultFilename sets resultFilename form field.
49+ func (req * request ) ResultFilename (filename string ) {
50+ req .values [resultFilename ] = filename
51+ }
52+
53+ // WaitTiemout sets waitTimeout form field.
54+ func (req * request ) WaitTimeout (timeout float64 ) {
55+ req .values [waitTimeout ] = strconv .FormatFloat (timeout , 'f' , 2 , 64 )
56+ }
57+
58+ // WebhookURL sets webhookURL form field.
59+ func (req * request ) WebhookURL (url string ) {
60+ req .values [webhookURL ] = url
6861}
6962
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 )
63+ // WebhookURLTimeout sets webhookURLTimeout form field.
64+ func (req * request ) WebhookURLTimeout (timeout float64 ) {
65+ req .values [webhookURLTimeout ] = strconv .FormatFloat (timeout , 'f' , 2 , 64 )
8066}
8167
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 )
68+ func (req * request ) formValues () map [string ]string {
69+ return req .values
8770}
8871
8972// Post sends a request to the Gotenberg API
@@ -93,8 +76,8 @@ func (c *Client) Post(req Request) (*http.Response, error) {
9376 if err != nil {
9477 return nil , err
9578 }
96- URL := fmt .Sprintf ("%s%s" , c .Hostname , req .getPostURL ())
97- resp , err := http .Post (URL , contentType , body )
79+ URL := fmt .Sprintf ("%s%s" , c .Hostname , req .postURL ())
80+ resp , err := http .Post (URL , contentType , body ) /* #nosec */
9881 if err != nil {
9982 return nil , err
10083 }
@@ -110,15 +93,17 @@ func (c *Client) Store(req Request, dest string) error {
11093 if err != nil {
11194 return err
11295 }
113- // Check for 2XX Status Codes
96+ defer resp .Body .Close ()
97+
98+ // Check for 2XX Status Codes
11499 if resp .StatusCode < http .StatusOK || resp .StatusCode >= http .StatusMultipleChoices {
115100 return errors .New ("failed to generate the result PDF" )
116101 }
117102 return writeNewFile (dest , resp .Body )
118103}
119104
120105func hasWebhook (req Request ) bool {
121- webhookURL , ok := req .getFormValues ()[webhookURL ]
106+ webhookURL , ok := req .formValues ()[webhookURL ]
122107 if ! ok {
123108 return false
124109 }
@@ -133,7 +118,7 @@ func writeNewFile(fpath string, in io.Reader) error {
133118 if err != nil {
134119 return fmt .Errorf ("%s: creating new file: %v" , fpath , err )
135120 }
136- defer out .Close ()
121+ defer out .Close () // nolint: errcheck
137122 err = out .Chmod (0644 )
138123 if err != nil && runtime .GOOS != "windows" {
139124 return fmt .Errorf ("%s: changing file mode: %v" , fpath , err )
@@ -153,8 +138,8 @@ func fileExists(name string) bool {
153138func multipartForm (req Request ) (* bytes.Buffer , string , error ) {
154139 body := & bytes.Buffer {}
155140 writer := multipart .NewWriter (body )
156- defer writer .Close ()
157- for filename , fpath := range req .getFormFiles () {
141+ defer writer .Close () // nolint: errcheck
142+ for filename , fpath := range req .formFiles () {
158143 // https://github.com/thecodingmachine/gotenberg-go-client/issues/3
159144 if fpath == "" {
160145 continue
@@ -163,6 +148,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
163148 if err != nil {
164149 return nil , "" , fmt .Errorf ("%s: opening file: %v" , filename , err )
165150 }
151+ defer in .Close () // nolint: errcheck
166152 part , err := writer .CreateFormFile ("files" , filename )
167153 if err != nil {
168154 return nil , "" , fmt .Errorf ("%s: creating form file: %v" , filename , err )
@@ -172,7 +158,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
172158 return nil , "" , fmt .Errorf ("%s: copying file: %v" , filename , err )
173159 }
174160 }
175- for name , value := range req .getFormValues () {
161+ for name , value := range req .formValues () {
176162 if err := writer .WriteField (name , value ); err != nil {
177163 return nil , "" , fmt .Errorf ("%s: writing form field: %v" , name , err )
178164 }
0 commit comments