@@ -2,6 +2,7 @@ package gotenberg
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"errors"
6
7
"fmt"
7
8
"io"
@@ -87,6 +88,13 @@ func (req *request) formValues() map[string]string {
87
88
// Post sends a request to the Gotenberg API
88
89
// and returns the response.
89
90
func (c * Client ) Post (req Request ) (* http.Response , error ) {
91
+ return c .PostContext (context .Background (), req )
92
+ }
93
+
94
+ // PostContext sends a request to the Gotenberg API
95
+ // and returns the response.
96
+ // The created HTTP request can be canceled by the passed context.
97
+ func (c * Client ) PostContext (ctx context.Context , req Request ) (* http.Response , error ) {
90
98
body , contentType , err := multipartForm (req )
91
99
if err != nil {
92
100
return nil , err
@@ -95,7 +103,7 @@ func (c *Client) Post(req Request) (*http.Response, error) {
95
103
c .HTTPClient = & http.Client {}
96
104
}
97
105
URL := fmt .Sprintf ("%s%s" , c .Hostname , req .postURL ())
98
- httpReq , err := http .NewRequest ( http .MethodPost , URL , body )
106
+ httpReq , err := http .NewRequestWithContext ( ctx , http .MethodPost , URL , body )
99
107
if err != nil {
100
108
return nil , err
101
109
}
@@ -112,10 +120,16 @@ func (c *Client) Post(req Request) (*http.Response, error) {
112
120
113
121
// Store creates the resulting PDF to given destination.
114
122
func (c * Client ) Store (req Request , dest string ) error {
123
+ return c .StoreContext (context .Background (), req , dest )
124
+ }
125
+
126
+ // StoreContext creates the resulting PDF to given destination.
127
+ // The created HTTP request can be canceled by the passed context.
128
+ func (c * Client ) StoreContext (ctx context.Context , req Request , dest string ) error {
115
129
if hasWebhook (req ) {
116
130
return errors .New ("cannot use Store method with a webhook" )
117
131
}
118
- resp , err := c .Post ( req )
132
+ resp , err := c .PostContext ( ctx , req )
119
133
if err != nil {
120
134
return err
121
135
}
0 commit comments