From 1ee6ac241a3988cb30624e44a603935b34d7fe3b Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Tue, 28 Mar 2023 13:10:52 -0400 Subject: [PATCH] Core: Move RequestBuilder into req.go --- internal/core/req.go | 34 ++++++++++++++++++++++++++++++++++ internal/core/url.go | 34 ---------------------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/internal/core/req.go b/internal/core/req.go index ec9d425..26cc39a 100644 --- a/internal/core/req.go +++ b/internal/core/req.go @@ -7,6 +7,7 @@ import ( "net/url" "github.com/carlmjohnson/requests/internal/minitrue" + "github.com/carlmjohnson/requests/internal/slicex" ) // NopCloser is like io.NopCloser(), @@ -25,6 +26,39 @@ func (NopCloser) Close() error { return nil } var _ io.ReadCloser = NopCloser{} +type BodyGetter = func() (io.ReadCloser, error) + +type RequestBuilder struct { + headers []multimap + cookies []kvpair + getBody BodyGetter + method string +} + +func (rb *RequestBuilder) Header(key string, values ...string) { + rb.headers = append(rb.headers, multimap{key, values}) +} + +func (rb *RequestBuilder) Cookie(name, value string) { + rb.cookies = append(rb.cookies, kvpair{name, value}) +} + +func (rb *RequestBuilder) Method(method string) { + rb.method = method +} + +func (rb *RequestBuilder) Body(src BodyGetter) { + rb.getBody = src +} + +// Clone creates a new Builder suitable for independent mutation. +func (rb *RequestBuilder) Clone() *RequestBuilder { + rb2 := *rb + slicex.Clip(&rb2.headers) + slicex.Clip(&rb2.cookies) + return &rb2 +} + // Request builds a new http.Request with its context set. func (rb *RequestBuilder) Request(ctx context.Context, u *url.URL) (req *http.Request, err error) { var body io.Reader diff --git a/internal/core/url.go b/internal/core/url.go index a5e97ca..d583181 100644 --- a/internal/core/url.go +++ b/internal/core/url.go @@ -3,7 +3,6 @@ package core import ( - "io" "net/url" "github.com/carlmjohnson/requests/internal/minitrue" @@ -78,36 +77,3 @@ func (ub *URLBuilder) URL() (u *url.URL, err error) { } return u, nil } - -type BodyGetter = func() (io.ReadCloser, error) - -type RequestBuilder struct { - headers []multimap - cookies []kvpair - getBody BodyGetter - method string -} - -func (rb *RequestBuilder) Header(key string, values ...string) { - rb.headers = append(rb.headers, multimap{key, values}) -} - -func (rb *RequestBuilder) Cookie(name, value string) { - rb.cookies = append(rb.cookies, kvpair{name, value}) -} - -func (rb *RequestBuilder) Method(method string) { - rb.method = method -} - -func (rb *RequestBuilder) Body(src BodyGetter) { - rb.getBody = src -} - -// Clone creates a new Builder suitable for independent mutation. -func (rb *RequestBuilder) Clone() *RequestBuilder { - rb2 := *rb - slicex.Clip(&rb2.headers) - slicex.Clip(&rb2.cookies) - return &rb2 -}