Client

NewClient, the Config, the construction options, and the Client interface.

The Client is the main entry point. NewClient builds one from a Config and returns it ready to reuse across goroutines.

NewClient

func NewClient(config http.Config, opts ...http.Option) http.Client

Builds a Client from config and options. It defaults to http.DefaultClient and a silent logger when none are supplied, and seeds the identity headers from Config. The returned client is safe to share.

client := http.NewClient(http.Config{
	BaseURL:   "https://api.example.com",
	UserAgent: "demo/1.0.0",
	Platform:  "cli",
})

Config

type Config struct {
	BaseURL		string			`json:"base_url"`
	UserAgent	string			`json:"user_agent"`
	Platform	string			`json:"platform"`
	ServiceName	string			`json:"service_name"`
	AppVersion	string			`json:"app_version"`
	ClientID	string			`json:"client_id"`
	Headers		map[string]string	`json:"headers"`
}

The static, construction-time configuration. BaseURL is joined to each request's path. The remaining identity fields are stamped onto every request as their header constant, and Headers are sent on every request as well. See Configuring the client.

Options

Each construction option is an Option, a function that configures the client. Pass any number to NewClient.

type Option func(*httpClient)
func WithHTTPClient(client *http.Client) http.Option

Swaps the underlying *http.Client used for every request, for a custom timeout or transport or a stub in tests. Here *http.Client is the standard library's client. A nil client is ignored, so the default stays.

func WithLogger(logger http.Logger) http.Option

Injects the logger the client writes diagnostics to. Without it the client stays silent. A nil logger is ignored.

UserAgent

func UserAgent(app string, version string, os string, osVersion string, arch string) string

Formats a conventional user-agent string from the app name, version, and platform details, for the Config.UserAgent field.

ua := http.UserAgent("awee-cli", "1.4.0", runtime.GOOS, osVersion, runtime.GOARCH)
// "awee-cli/1.4.0 (darwin 14.5; arm64)"

Client

type Client interface {
	Get(ctx context.Context, req GetRequest) (*Response, error)
	GetStream(ctx context.Context, stream chan StreamResponse, req Request) error
	Post(ctx context.Context, req PostRequest) (*Response, error)
	PostStream(ctx context.Context, stream chan StreamResponse, req PostRequest) error
	Put(ctx context.Context, req PutRequest) (*Response, error)
	Patch(ctx context.Context, req PatchRequest) (*Response, error)
	Delete(ctx context.Context, req Request) (*Response, error)
}

One method per verb. The buffered verbs return a *Response, and the two streaming methods decode Server-Sent Events onto a channel you own. See Requests and responses for the argument and return types, and Streaming for the stream methods.