From 8d0ddc0473069b49850b2486477c4a83e2752343 Mon Sep 17 00:00:00 2001 From: optman Date: Thu, 4 Apr 2019 15:03:59 +0800 Subject: [PATCH 1/2] http api support cancel with context --- request.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/request.go b/request.go index a87d8a3e6..741056f47 100644 --- a/request.go +++ b/request.go @@ -16,6 +16,7 @@ import ( ) type Request struct { + Ctx context.Context ApiBase string Command string Args []string @@ -34,6 +35,7 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque "stream-channels": "true", } return &Request{ + Ctx: ctx, ApiBase: url + "/api/v0", Command: command, Args: args, @@ -108,11 +110,13 @@ func (e *Error) Error() string { func (r *Request) Send(c *http.Client) (*Response, error) { url := r.getURL() - req, err := http.NewRequest("POST", url, r.Body) + req0, err := http.NewRequest("POST", url, r.Body) if err != nil { return nil, err } + req := req0.WithContext(r.Ctx) + // Add any headers that were supplied via the RequestBuilder. for k, v := range r.Headers { req.Header.Add(k, v) From 4ecd1faf656fc2374811801a9253a7311fd6b919 Mon Sep 17 00:00:00 2001 From: optman Date: Thu, 4 Apr 2019 15:15:35 +0800 Subject: [PATCH 2/2] remove req0 --- request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index 741056f47..7d3a72fd0 100644 --- a/request.go +++ b/request.go @@ -110,12 +110,12 @@ func (e *Error) Error() string { func (r *Request) Send(c *http.Client) (*Response, error) { url := r.getURL() - req0, err := http.NewRequest("POST", url, r.Body) + req, err := http.NewRequest("POST", url, r.Body) if err != nil { return nil, err } - req := req0.WithContext(r.Ctx) + req = req.WithContext(r.Ctx) // Add any headers that were supplied via the RequestBuilder. for k, v := range r.Headers {