From a5d8aeb0bb53420a6dc87dcdc72494fa3f44224f Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 1 May 2023 08:38:54 -0600 Subject: make spartan.Client.RoundTrip match the API of other clients. - Request.Meta is already used as an *io.LimitedReader in spartan servers, so by following this convention the RoundTrip method doesn't need anything more than the *Request. - Make a new public method for setting the body on a spartan request. --- spartan/request.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'spartan/request.go') diff --git a/spartan/request.go b/spartan/request.go index a9b2815..c056af0 100644 --- a/spartan/request.go +++ b/spartan/request.go @@ -2,6 +2,7 @@ package spartan import ( "bufio" + "bytes" "errors" "io" "net/url" @@ -80,3 +81,23 @@ func GetRequestBody(request *sr.Request) io.Reader { } return nil } + +// SetRequestBody adds an io.Reader as a request body. +// +// It is for use in clients, preparing the request to be sent. +// +// This function will read the entire contents into memory unless +// the reader is already an *io.LimitedReader. +func SetRequestBody(request *sr.Request, body io.Reader) error { + if rdr, ok := body.(*io.LimitedReader); ok { + request.Meta = rdr + return nil + } + + buf, err := io.ReadAll(body) + if err != nil { + return err + } + request.Meta = io.LimitReader(bytes.NewBuffer(buf), int64(len(buf))) + return nil +} -- cgit v1.2.3