apiclient

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthBasic

type AuthBasic struct {
	Username string
	Password string
}

type AuthToken

type AuthToken struct {
	Header string
	Data   string
}

type Client

type Client struct {
	Client  IHTTPClient
	Limiter *rate.Limiter
	Trace   *httptrace.ClientTrace
	// contains filtered or unexported fields
}

API Client

Like a finely-crafted sword, it can be wielded with skill if instructions are followed.

1) Create your config:

	conf := &apiclient.Config{
    RequestsPerSecond: 5,    // 5 requests per second.
		Timeout:           5,    // 5 seconds.
	}

2) Create your client

api := apiclient.NewClient(conf)

3) ???

params := &Params{
	Url: "http://www.example.com/underpants",
}

4) Profit

data, code, err := api.Get(params)
// check `err` and `code` here.
// `data` will need to be converted from `[]byte`.

func NewClient

func NewClient(config *Config, logger logger.ILogger) *Client

Create a new API client with the given configuration.

func (*Client) Get

func (c *Client) Get(data *Params) ([]byte, int, error)

Perform a HTTP GET using the given API parameters.

Returns the response body as an array of bytes, the HTTP status code, and an error if one is triggered.

You will need to remember to check the error *and* the status code.

func (*Client) Post

func (c *Client) Post(data *Params) ([]byte, int, error)

Perform a HTTP POST using the given API parameters.

Returns the response body as an array of bytes, the HTTP status code, and an error if one is triggered.

You will need to remember to check the error *and* the status code.

type Config

type Config struct {
	RequestsPerSecond int `json:"requests_per_second"`
	Timeout           int `json:"timeout"`
}

API client configuration.

`RequstsPerSecond` is the number of requests per second rate limiting. `Timeout` is obvious.

func NewConfig

func NewConfig(ReqsPerSec, Timeout int) *Config

Create a new API client configuration.

func NewDefaultConfig

func NewDefaultConfig() *Config

Return a new default API client configuration.

type ContentType

type ContentType struct {
	Accept string
	Type   string
}

type IApiClient

type IApiClient interface {
	Get(data *Params) ([]byte, int, error)
	Post(data *Params) ([]byte, int, error)
}

API client interface.

type IHTTPClient

type IHTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTP client interface.

This is to allow mocking of `net/http`'s `http.Client` in unit tests. You could also, if drunk enough, provide your own HTTP client, as long as it conforms to the interface. But you wouldn't want to do that, would you.

type MockCallbackFn added in v0.1.1

type MockCallbackFn func(data *Params) ([]byte, int, error)

type MockClient added in v0.1.1

type MockClient struct {
	GetFn  MockCallbackFn
	PostFn MockCallbackFn
	// contains filtered or unexported fields
}

func NewMockClient added in v0.1.1

func NewMockClient(config *Config, lgr logger.ILogger) *MockClient

func (*MockClient) Get added in v0.1.1

func (c *MockClient) Get(data *Params) ([]byte, int, error)

func (*MockClient) Post added in v0.1.1

func (c *MockClient) Post(data *Params) ([]byte, int, error)

type Params

type Params struct {
	Url string // API URL.

	UseBasic bool
	UseToken bool

	Content ContentType
	Token   AuthToken
	Basic   AuthBasic

	Queries []*QueryParam
}

API client request parameters

func NewParams

func NewParams() *Params

Create a new API parameters object,

func (*Params) AddQueryParam

func (p *Params) AddQueryParam(name, content string) *QueryParam

Add a new query parameter.

func (*Params) ClearQueryParams

func (p *Params) ClearQueryParams()

Clear all query parameters.

func (*Params) SetUseBasic

func (p *Params) SetUseBasic(val bool)

Enable/disable basic authentication.

func (*Params) SetUseToken

func (p *Params) SetUseToken(val bool)

Enable/disable authentication token.

type QueryParam

type QueryParam struct {
	Name    string
	Content string
}

API URL query parameter.

func NewQueryParam

func NewQueryParam(name, content string) *QueryParam

Create a new query parameter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL