Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Use interface for RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed May 10, 2019
1 parent b9c8a2f commit 0534b7c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
10 changes: 5 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type HttpApi struct {
url string
httpcli gohttp.Client
Headers http.Header
applyGlobal func(*RequestBuilder)
applyGlobal func(*requestBuilder)
}

// NewLocalApi tries to construct new HttpApi instance communicating with local
Expand Down Expand Up @@ -117,7 +117,7 @@ func NewURLApiWithClient(url string, c *gohttp.Client) (*HttpApi, error) {
url: url,
httpcli: *c,
Headers: make(map[string][]string),
applyGlobal: func(*RequestBuilder) {},
applyGlobal: func(*requestBuilder) {},
}

// We don't support redirects.
Expand All @@ -134,7 +134,7 @@ func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error)
}

subApi := *api
subApi.applyGlobal = func(req *RequestBuilder) {
subApi.applyGlobal = func(req *requestBuilder) {
if options.Offline {
req.Option("offline", options.Offline)
}
Expand All @@ -143,14 +143,14 @@ func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error)
return &subApi, nil
}

func (api *HttpApi) Request(command string, args ...string) *RequestBuilder {
func (api *HttpApi) Request(command string, args ...string) RequestBuilder {
headers := make(map[string]string)
if api.Headers != nil {
for k := range api.Headers {
headers[k] = api.Headers.Get(k)
}
}
return &RequestBuilder{
return &requestBuilder{
command: command,
args: args,
shell: api,
Expand Down
36 changes: 25 additions & 11 deletions requestbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ import (
"github.com/ipfs/go-ipfs-files"
)

// RequestBuilder is an IPFS commands request builder.
type RequestBuilder struct {
type RequestBuilder interface {
Arguments(args ...string) RequestBuilder
BodyString(body string) RequestBuilder
BodyBytes(body []byte) RequestBuilder
Body(body io.Reader) RequestBuilder
FileBody(body io.Reader) RequestBuilder
Option(key string, value interface{}) RequestBuilder
Header(name, value string) RequestBuilder
Send(ctx context.Context) (*Response, error)
Exec(ctx context.Context, res interface{}) error
}

// requestBuilder is an IPFS commands request builder.
type requestBuilder struct {
command string
args []string
opts map[string]string
Expand All @@ -24,29 +36,29 @@ type RequestBuilder struct {
}

// Arguments adds the arguments to the args.
func (r *RequestBuilder) Arguments(args ...string) *RequestBuilder {
func (r *requestBuilder) Arguments(args ...string) RequestBuilder {
r.args = append(r.args, args...)
return r
}

// BodyString sets the request body to the given string.
func (r *RequestBuilder) BodyString(body string) *RequestBuilder {
func (r *requestBuilder) BodyString(body string) RequestBuilder {
return r.Body(strings.NewReader(body))
}

// BodyBytes sets the request body to the given buffer.
func (r *RequestBuilder) BodyBytes(body []byte) *RequestBuilder {
func (r *requestBuilder) BodyBytes(body []byte) RequestBuilder {
return r.Body(bytes.NewReader(body))
}

// Body sets the request body to the given reader.
func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder {
func (r *requestBuilder) Body(body io.Reader) RequestBuilder {
r.body = body
return r
}

// FileBody sets the request body to the given reader wrapped into multipartreader.
func (r *RequestBuilder) FileBody(body io.Reader) *RequestBuilder {
func (r *requestBuilder) FileBody(body io.Reader) RequestBuilder {
pr, _ := files.NewReaderPathFile("/dev/stdin", ioutil.NopCloser(body), nil)
d := files.NewMapDirectory(map[string]files.Node{"": pr})
r.body = files.NewMultiFileReader(d, false)
Expand All @@ -55,7 +67,7 @@ func (r *RequestBuilder) FileBody(body io.Reader) *RequestBuilder {
}

// Option sets the given option.
func (r *RequestBuilder) Option(key string, value interface{}) *RequestBuilder {
func (r *requestBuilder) Option(key string, value interface{}) RequestBuilder {
var s string
switch v := value.(type) {
case bool:
Expand All @@ -76,7 +88,7 @@ func (r *RequestBuilder) Option(key string, value interface{}) *RequestBuilder {
}

// Header sets the given header.
func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
func (r *requestBuilder) Header(name, value string) RequestBuilder {
if r.headers == nil {
r.headers = make(map[string]string, 1)
}
Expand All @@ -85,7 +97,7 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
}

// Send sends the request and return the response.
func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
func (r *requestBuilder) Send(ctx context.Context) (*Response, error) {
r.shell.applyGlobal(r)

req := NewRequest(ctx, r.shell.url, r.command, r.args...)
Expand All @@ -96,7 +108,7 @@ func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
}

// Exec sends the request a request and decodes the response.
func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error {
func (r *requestBuilder) Exec(ctx context.Context, res interface{}) error {
httpRes, err := r.Send(ctx)
if err != nil {
return err
Expand All @@ -112,3 +124,5 @@ func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error {

return httpRes.decode(res)
}

var _ RequestBuilder = &requestBuilder{}
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *Request) Send(c *http.Client) (*Response, error) {

req = req.WithContext(r.Ctx)

// Add any headers that were supplied via the RequestBuilder.
// Add any headers that were supplied via the requestBuilder.
for k, v := range r.Headers {
req.Header.Add(k, v)
}
Expand Down

0 comments on commit 0534b7c

Please sign in to comment.