Skip to content

Commit

Permalink
feat(client): expose a default Requester interface (#310)
Browse files Browse the repository at this point in the history
The patch adds a public Requester interface which allow derived projects to use the Pebble instantiated client and extend the available commands. The extended client commands will use client.Requester().Do for HTTP requests.

Derived project (imports Pebble):

// exClient.go:
type ExClient struct {
	*client.Client
}

func (ex *ExClient) Reboot(opts *RebootOptions) error {
	payload := &devicePayload{
		Action: "reboot",
	}
	:
	if _, err := ex.Requester().Do(...); err != nil {
		return err
	}
	:
}

// cmd_reboot.go
type cmdReboot struct {
	client *exClient.ExClient
}

func init() {
	cli.AddCommand(&cli.CmdInfo{
		Name:        "reboot",
		Summary:     cmdRebootSummary,
		Description: cmdRebootDescription,
		New: func(opts *cli.CmdOptions) flags.Commander {
			return &cmdReboot{client: &exClient.ExClient{opts.Client}}
		},
	})
}

func (cmd *cmdReboot) Execute(args []string) error {
	if len(args) > 0 {
		return cli.ErrExtraArgs
	}

	return cmd.client.Reboot(&exClient.RebootOptions{})
}
The changes made in the patch has been done in a way to produce as small
as possible diffs (we keep doSync/doAsyc wrappers). The default interface
has been implemented in the client.go file to allow reviewers to easily
identify which code was added, and which is unchanged.

The following changes are made:

- The ResultInfo type previously returned by doSync and doAsync private
function are removed. Although this is a publicly exposed type, the return
value as of today has always been discarded, and the struct is currently
empty.

- ResultInfo has been replaced by RequestResponse.

- The client Hijack mechanism which allowed the doer to be replaced has been
removed. This was originally added in snapd for an arguably slightly obscure use
case. We now have a new Requester interface, so any future need
for this will instead be built in top of the Requester interface.

- The logs client request now uses the same retry logic on GET failure,
as any other GET request. This is previously not possible because the
retry logic and response unmarshall code was bundled, not allow raw access
to the HTTP body.
  • Loading branch information
flotter authored Oct 12, 2023
1 parent d3a4c78 commit e686993
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 160 deletions.
Loading

0 comments on commit e686993

Please sign in to comment.