diff --git a/client/client.go b/client/client.go index 1ac55734..7f56c44a 100644 --- a/client/client.go +++ b/client/client.go @@ -317,7 +317,7 @@ func (client *Client) do(method, path string, query url.Values, headers map[stri if err != nil { return err } - err = client.decodeHook(bodyBytes, &RequestOptions{Method: method, Path: path /* TODO */}) + err = client.decodeHook(bodyBytes, method, path, &RequestOptions{ /* TODO */ }) if err != nil { return err } @@ -536,44 +536,35 @@ func (client *Client) DebugGet(action string, result interface{}, params map[str return err } -// RequestOptions allows setting up a specific request. +// RequestOptions provides options arguments for a request. type RequestOptions struct { - Method string - Path string - Query url.Values - Headers map[string]string - Body io.Reader - Async bool - ReturnBody bool + Query url.Values + Headers map[string]string + Body io.Reader } -// RequestResponse defines a common response associated with requests. -type RequestResponse struct { - StatusCode int - ChangeID string - Body io.ReadCloser -} +// NOTE: these are NOT the real implementation, just to get things to work at a basic level -// NOTE: not the real implementation, just to get things to work at a basic level -func (client *Client) Do(ctx context.Context, opts *RequestOptions, result interface{}) (*RequestResponse, error) { - if opts.ReturnBody { - panic("not yet implemented") - } - if opts.Async { - changeID, err := client.doAsync(opts.Method, opts.Path, opts.Query, opts.Headers, opts.Body) - if err != nil { - return nil, err - } - return &RequestResponse{ChangeID: changeID}, nil +func (client *Client) DoSync(ctx context.Context, method, path string, opts *RequestOptions, result interface{}) error { + if opts == nil { + opts = &RequestOptions{} } - _, err := client.doSync(opts.Method, opts.Path, opts.Query, opts.Headers, opts.Body, result) - if err != nil { - return nil, err + _, err := client.doSync(method, path, opts.Query, opts.Headers, opts.Body, result) + return err +} + +func (client *Client) DoAsync(ctx context.Context, method, path string, opts *RequestOptions) (changeID string, err error) { + if opts == nil { + opts = &RequestOptions{} } - return &RequestResponse{}, nil + return client.doAsync(method, path, opts.Query, opts.Headers, opts.Body) +} + +func (client *Client) DoRaw(ctx context.Context, method, path string, opts *RequestOptions) (io.ReadCloser, error) { + panic("not yet implemented") } -type DecodeHookFunc func(data []byte, opts *RequestOptions) error +type DecodeHookFunc func(data []byte, method, path string, opts *RequestOptions) error func (client *Client) SetDecodeHook(hook DecodeHookFunc) { client.decodeHook = hook diff --git a/cmd/clienttest/main.go b/cmd/clienttest/main.go index 3ef73e84..558fcb54 100644 --- a/cmd/clienttest/main.go +++ b/cmd/clienttest/main.go @@ -46,9 +46,9 @@ func NewMyClient(pebble *client.Client) *MyClient { return c } -func (c *MyClient) decodeHook(data []byte, opts *client.RequestOptions) error { +func (c *MyClient) decodeHook(data []byte, method, path string, opts *client.RequestOptions) error { // Demonstrate use of opts: only do custom decode on v1 requests. - if !strings.HasPrefix(opts.Path, "/v1/") { + if !strings.HasPrefix(path, "/v1/") { return nil } var frame struct { @@ -66,10 +66,7 @@ func (c *MyClient) decodeHook(data []byte, opts *client.RequestOptions) error { func (c *MyClient) UpperServices() ([]UpperServiceInfo, error) { var infos []UpperServiceInfo - _, err := c.pebble.Do(context.Background(), &client.RequestOptions{ - Method: "GET", - Path: "/v1/services", - }, &infos) + err := c.pebble.DoSync(context.Background(), "GET", "/v1/services", nil, &infos) if err != nil { return nil, err }