Skip to content

Commit

Permalink
Core: Move RequestBuilder into req.go
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Mar 30, 2023
1 parent 9dca126 commit 1ee6ac2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
34 changes: 34 additions & 0 deletions internal/core/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"

"github.com/carlmjohnson/requests/internal/minitrue"
"github.com/carlmjohnson/requests/internal/slicex"
)

// NopCloser is like io.NopCloser(),
Expand All @@ -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
Expand Down
34 changes: 0 additions & 34 deletions internal/core/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package core

import (
"io"
"net/url"

"github.com/carlmjohnson/requests/internal/minitrue"
Expand Down Expand Up @@ -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
}

0 comments on commit 1ee6ac2

Please sign in to comment.