response: option to disable output draining

This commit was moved from ipfs/go-ipfs-http-client@9f3d9635fa
This commit is contained in:
Łukasz Magiera 2019-02-18 17:27:12 +01:00
parent b6ace8dd40
commit 7be8d01ee7
6 changed files with 21 additions and 5 deletions

View File

@ -131,6 +131,7 @@ func (api *HttpApi) request(command string, args ...string) *RequestBuilder {
command: command,
args: args,
shell: api,
drainOut: true,
}
}

View File

@ -57,7 +57,7 @@ func (f *apiFile) reset() error {
if f.r != nil {
f.r.Close()
}
req := f.core.request("cat", f.path.String())
req := f.core.request("cat", f.path.String()).NoDrain()
if f.at != 0 {
req.Option("offset", f.at)
}

View File

@ -113,8 +113,8 @@ func (api *PubsubAPI) Subscribe(ctx context.Context, topic string, opts ...caopt
}
resp, err := api.core().request("pubsub/sub", topic).
Option("discover", options.Discover).
Send(ctx)
Option("discover", options.Discover).NoDrain().Send(ctx)
if err != nil {
return nil, err
}

View File

@ -13,6 +13,7 @@ type Request struct {
Opts map[string]string
Body io.Reader
Headers map[string]string
DrainOut bool // if set, resp.Close will read all remaining data
}
func NewRequest(ctx context.Context, url, command string, args ...string) *Request {
@ -30,5 +31,6 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque
Args: args,
Opts: opts,
Headers: make(map[string]string),
DrainOut: true,
}
}

View File

@ -19,6 +19,7 @@ type RequestBuilder struct {
opts map[string]string
headers map[string]string
body io.Reader
drainOut bool
shell *HttpApi
}
@ -84,6 +85,12 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
return r
}
// NoDrain disables output draining in response closer
func (r *RequestBuilder) NoDrain() *RequestBuilder {
r.drainOut = false
return r
}
// Send sends the request and return the response.
func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
r.shell.applyGlobal(r)

View File

@ -35,13 +35,18 @@ func (r *trailerReader) Close() error {
type Response struct {
Output io.ReadCloser
Error *Error
drainOutput bool
}
func (r *Response) Close() error {
if r.Output != nil {
// always drain output (response body) //TODO: make optional for things like cat
_, err1 := io.Copy(ioutil.Discard, r.Output)
// always drain output (response body)
var err1 error
if r.drainOutput {
_, err1 = io.Copy(ioutil.Discard, r.Output)
}
err2 := r.Output.Close()
if err1 != nil {
return err1
@ -114,6 +119,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
nresp := new(Response)
nresp.drainOutput = r.DrainOut
nresp.Output = &trailerReader{resp}
if resp.StatusCode >= http.StatusBadRequest {
e := &Error{