@@ -14,34 +14,39 @@ import (
1414)
1515
1616const (
17- resultFilename string = "resultFilename"
18- waitTimeout string = "waitTimeout"
19- webhookURL string = "webhookURL"
20- webhookURLTimeout string = "webhookURLTimeout"
17+ resultFilename string = "resultFilename"
18+ waitTimeout string = "waitTimeout"
19+ webhookURL string = "webhookURL"
20+ webhookURLTimeout string = "webhookURLTimeout"
21+ webhookURLBaseHTTPHeaderKey string = "Gotenberg-Webhookurl-"
2122)
2223
2324// Client facilitates interacting with
2425// the Gotenberg API.
2526type Client struct {
26- Hostname string
27+ Hostname string
28+ HTTPClient * http.Client
2729}
2830
2931// Request is a type for sending
3032// form values and form files to
3133// the Gotenberg API.
3234type Request interface {
3335 postURL () string
36+ customHTTPHeaders () map [string ]string
3437 formValues () map [string ]string
35- formFiles () map [string ]string
38+ formFiles () map [string ]Document
3639}
3740
3841type request struct {
39- values map [string ]string
42+ httpHeaders map [string ]string
43+ values map [string ]string
4044}
4145
4246func newRequest () * request {
4347 return & request {
44- values : make (map [string ]string ),
48+ httpHeaders : make (map [string ]string ),
49+ values : make (map [string ]string ),
4550 }
4651}
4752
@@ -50,7 +55,7 @@ func (req *request) ResultFilename(filename string) {
5055 req .values [resultFilename ] = filename
5156}
5257
53- // WaitTiemout sets waitTimeout form field.
58+ // WaitTimeout sets waitTimeout form field.
5459func (req * request ) WaitTimeout (timeout float64 ) {
5560 req .values [waitTimeout ] = strconv .FormatFloat (timeout , 'f' , 2 , 64 )
5661}
@@ -65,6 +70,16 @@ func (req *request) WebhookURLTimeout(timeout float64) {
6570 req .values [webhookURLTimeout ] = strconv .FormatFloat (timeout , 'f' , 2 , 64 )
6671}
6772
73+ // AddWebhookURLHTTPHeader add a webhook custom HTTP header.
74+ func (req * request ) AddWebhookURLHTTPHeader (key , value string ) {
75+ key = fmt .Sprintf ("%s%s" , webhookURLBaseHTTPHeaderKey , key )
76+ req .httpHeaders [key ] = value
77+ }
78+
79+ func (req * request ) customHTTPHeaders () map [string ]string {
80+ return req .httpHeaders
81+ }
82+
6883func (req * request ) formValues () map [string ]string {
6984 return req .values
7085}
@@ -76,8 +91,19 @@ func (c *Client) Post(req Request) (*http.Response, error) {
7691 if err != nil {
7792 return nil , err
7893 }
94+ if c .HTTPClient == nil {
95+ c .HTTPClient = & http.Client {}
96+ }
7997 URL := fmt .Sprintf ("%s%s" , c .Hostname , req .postURL ())
80- resp , err := http .Post (URL , contentType , body ) /* #nosec */
98+ httpReq , err := http .NewRequest (http .MethodPost , URL , body )
99+ if err != nil {
100+ return nil , err
101+ }
102+ httpReq .Header .Set ("Content-Type" , contentType )
103+ for key , value := range req .customHTTPHeaders () {
104+ httpReq .Header .Set (key , value )
105+ }
106+ resp , err := c .HTTPClient .Do (httpReq ) /* #nosec */
81107 if err != nil {
82108 return nil , err
83109 }
@@ -138,14 +164,10 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
138164 body := & bytes.Buffer {}
139165 writer := multipart .NewWriter (body )
140166 defer writer .Close () // nolint: errcheck
141- for filename , fpath := range req .formFiles () {
142- // https://github.com/thecodingmachine/gotenberg-go-client/issues/3
143- if fpath == "" {
144- continue
145- }
146- in , err := os .Open (fpath )
167+ for filename , document := range req .formFiles () {
168+ in , err := document .Reader ()
147169 if err != nil {
148- return nil , "" , fmt .Errorf ("%s: opening file : %v" , filename , err )
170+ return nil , "" , fmt .Errorf ("%s: creating reader : %v" , filename , err )
149171 }
150172 defer in .Close () // nolint: errcheck
151173 part , err := writer .CreateFormFile ("files" , filename )
@@ -154,7 +176,7 @@ func multipartForm(req Request) (*bytes.Buffer, string, error) {
154176 }
155177 _ , err = io .Copy (part , in )
156178 if err != nil {
157- return nil , "" , fmt .Errorf ("%s: copying file : %v" , filename , err )
179+ return nil , "" , fmt .Errorf ("%s: copying data : %v" , filename , err )
158180 }
159181 }
160182 for name , value := range req .formValues () {
0 commit comments