client

package
v0.0.0-...-b19e04c Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIErrorContent

func APIErrorContent(err error) string

APIErrorContent return the API error response, if the error is an APIError. Retrun the empty string if the error isn't an APIError.

func IsAuthError

func IsAuthError(err error) bool

IsAuthError return true if the error is an APIError due to authentication failure.

func IsBadRequest

func IsBadRequest(err error) bool

IsBadRequest return true if the error is an APIError due to 400.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound return true if the error is an APIError due to 404.

func IsServerError

func IsServerError(err error) bool

IsServerError return true if the error is an APIError due to 5xx.

func IsThrottleError

func IsThrottleError(err error) bool

IsThrottleError return true if the error is an APIError due to 429 - Too many request.

ThrottleDeadline could be used to get recommended retry deadline.

Types

type APIError

type APIError struct {
	StatusCode   int
	Content      string
	ContentType  string
	FinalURL     string
	UnmarshalErr error
	IsAuthError  bool
}

APIError are returned when HTTP request got a response but that response is an error (400 or 500).

func (APIError) Error

func (ae APIError) Error() string

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient is a wrapper around Bleemeo API. It is useful for authenticating seamlessly.

func NewClient

func NewClient(baseURL string, username string, password string, insecureTLS bool, reloadState types.BleemeoReloadState) (*HTTPClient, error)

NewClient return a client to talk with Bleemeo API.

It does the authentication (using OAuth currently) and may do rate-limiting/throtteling, so most functions may return a ThrottleError.

func (*HTTPClient) Do

func (c *HTTPClient) Do(ctx context.Context, method string, path string, params map[string]string, data interface{}, result interface{}) (statusCode int, err error)

Do perform the specified request.

Response is assumed to be JSON and will be decoded into result. If result is nil, response is not decoded

If submittedData is not-nil, it's the body content of the request.

func (*HTTPClient) DoTLSInsecure

func (c *HTTPClient) DoTLSInsecure(ctx context.Context, method string, path string, params map[string]string, data interface{}, result interface{}) (statusCode int, err error)

DoTLSInsecure perform the specified request, but without TLS verification. It is otherwise exactly similar to `DoUnauthenticated`. This method will NOT use authentication (since credentials should not be sent insecurely).

func (*HTTPClient) DoUnauthenticated

func (c *HTTPClient) DoUnauthenticated(ctx context.Context, method string, path string, params map[string]string, data interface{}, result interface{}) (statusCode int, err error)

DoUnauthenticated perform the specified request, but without the OAuth token used in `Do`. It is otherwise exactly similar to `Do`.

func (*HTTPClient) DoWithBody

func (c *HTTPClient) DoWithBody(ctx context.Context, path string, contentType string, body io.Reader) (int, error)

DoWithBody sends a POST request to the given path with the given body under the given content-type. It returns the status code of the response or any error that occurred.

func (*HTTPClient) GetToken

func (c *HTTPClient) GetToken(ctx context.Context) (*oauth2.Token, error)

GetToken for authentication with the Bleemeo API. The access token will be renewed using the refresh token or with the username and password if the refresh token has expired.

func (*HTTPClient) Iter

func (c *HTTPClient) Iter(ctx context.Context, resource string, params map[string]string) ([]json.RawMessage, error)

Iter read all page for given resource.

params may be modified.

func (*HTTPClient) PostAuth

func (c *HTTPClient) PostAuth(ctx context.Context, path string, data interface{}, username string, password string, result interface{}) (statusCode int, err error)

PostAuth perform the post on specified path. baseURL will be always be added.

func (*HTTPClient) RequestsCount

func (c *HTTPClient) RequestsCount() int

func (*HTTPClient) ThrottleDeadline

func (c *HTTPClient) ThrottleDeadline() time.Time

ThrottleDeadline return the time request should be retried.

func (*HTTPClient) VerifyAndGetToken

func (c *HTTPClient) VerifyAndGetToken(ctx context.Context, agentID string) (string, error)

VerifyAndGetToken is used to get a valid token. It differs from GetToken because the token is only renewed if necessary.

Jump to

Keyboard shortcuts

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