mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 12:27:43 +08:00
Merge pull request ipfs/go-ipfs-http-client#18 from ipfs/feat/iface-reqbuilder
Use interface for RequestBuilder This commit was moved from ipfs/go-ipfs-http-client@555a5b3ccc
This commit is contained in:
commit
ee2dec18f8
@ -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
|
||||
@ -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.
|
||||
@ -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)
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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
|
||||
@ -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)
|
||||
@ -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:
|
||||
@ -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)
|
||||
}
|
||||
@ -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...)
|
||||
@ -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
|
||||
@ -112,3 +124,5 @@ func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error {
|
||||
|
||||
return httpRes.decode(res)
|
||||
}
|
||||
|
||||
var _ RequestBuilder = &requestBuilder{}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user