Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit 6e52fa8

Browse files
author
Julien Neuhart
authored
Merge pull request #30 from domonda/master
Add context methods for canceling requests
2 parents 8bbbb1d + 9b0932a commit 6e52fa8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

client.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gotenberg
22

33
import (
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.
8990
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) {
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.
114122
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 {
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

Comments
 (0)