Skip to content

Commit

Permalink
client.Do option 2: Do(Sync|Async|Raw), simpler RequestOptions, no Re…
Browse files Browse the repository at this point in the history
…questResponse
  • Loading branch information
benhoyt committed Oct 5, 2023
1 parent 48939e9 commit b929bd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
53 changes: 22 additions & 31 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions cmd/clienttest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit b929bd3

Please sign in to comment.