@@ -2,6 +2,7 @@ package gotenberg
22
33import (
44 "bytes"
5+ "context"
56 "errors"
67 "fmt"
78 "io"
@@ -87,6 +88,13 @@ func (req *request) formValues() map[string]string {
8788// Post sends a request to the Gotenberg API
8889// and returns the response.
8990func (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 ) {
9098 body , contentType , err := multipartForm (req )
9199 if err != nil {
92100 return nil , err
@@ -95,7 +103,7 @@ func (c *Client) Post(req Request) (*http.Response, error) {
95103 c .HTTPClient = & http.Client {}
96104 }
97105 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 )
99107 if err != nil {
100108 return nil , err
101109 }
@@ -112,10 +120,16 @@ func (c *Client) Post(req Request) (*http.Response, error) {
112120
113121// Store creates the resulting PDF to given destination.
114122func (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 {
115129 if hasWebhook (req ) {
116130 return errors .New ("cannot use Store method with a webhook" )
117131 }
118- resp , err := c .Post ( req )
132+ resp , err := c .PostContext ( ctx , req )
119133 if err != nil {
120134 return err
121135 }
0 commit comments