Add new UploadWithContext and DownloadWithContext methods to
jmap.Client to allow cancelling these requests.
Signed-off-by: Robin Jarry <robin@jarry.cc>
---
Hey Robin -
This is a good idea but I don't see where you are using the context to cancel
the request? I think each function should have the http.NewRequest call changed
to http.NewRequestWithContext.
Or am I missing something?
Hey Tim,
If I am not mistaken, all other requests are performed via client.Do()
which takes a jmap.Request object. That object has a Context field which
is nil by default but can be set before passing it down the chain.
client.Do passes the provided context (or context.Background) to
http.NewRequestWithContext.
https://git.sr.ht/~rockorager/go-jmap/tree/main/item/client.go#L126-129
The cancellation itself is checked by the net/http machinery. I have
checked it and it seems to work.
Thanks!
client.go | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/client.go b/client.go
index b88afb575535..24b880234bf7 100644
--- a/client.go+++ b/client.go
@@ -163,6 +163,19 @@ func (c *Client) Do(req *Request) (*Response, error) {
// - Blob ID may become invalid after some time if it is unused.
// - Blob ID is usable only by the uploader until it is used, even for shared accounts.
func (c *Client) Upload(accountID ID, blob io.Reader) (*UploadResponse, error) {
+ return c.UploadWithContext(context.Background(), accountID, blob)+}++// UploadWithContext sends binary data to the server and returns blob ID and+// some associated meta-data.+//+// There are some caveats to keep in mind:+// - Server may return the same blob ID for multiple uploads of the same blob.+// - Blob ID may become invalid after some time if it is unused.+// - Blob ID is usable only by the uploader until it is used, even for shared accounts.+func (c *Client) UploadWithContext(+ ctx context.Context, accountID ID, blob io.Reader,+) (*UploadResponse, error) { c.Lock()
if c.SessionEndpoint == "" {
c.Unlock()
@@ -210,6 +223,13 @@ func (c *Client) Upload(accountID ID, blob io.Reader) (*UploadResponse, error) {
// Download downloads binary data by its Blob ID from the server.
func (c *Client) Download(accountID ID, blobID ID) (io.ReadCloser, error) {
+ return c.DownloadWithContext(context.Background(), accountID, blobID)+}++// DownloadWithContext downloads binary data by its Blob ID from the server.+func (c *Client) DownloadWithContext(+ ctx context.Context, accountID ID, blobID ID,+) (io.ReadCloser, error) { c.Lock()
if c.SessionEndpoint == "" {
c.Unlock()
--
2.43.0