Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): expose a default Requester interface (#310)
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