Documentation ¶
Index ¶
- func APIErrorContent(err error) string
- func IsAuthError(err error) bool
- func IsBadRequest(err error) bool
- func IsNotFound(err error) bool
- func IsServerError(err error) bool
- func IsThrottleError(err error) bool
- type APIError
- type HTTPClient
- func (c *HTTPClient) Do(ctx context.Context, method string, path string, params map[string]string, ...) (statusCode int, err error)
- func (c *HTTPClient) DoTLSInsecure(ctx context.Context, method string, path string, params map[string]string, ...) (statusCode int, err error)
- func (c *HTTPClient) DoUnauthenticated(ctx context.Context, method string, path string, params map[string]string, ...) (statusCode int, err error)
- func (c *HTTPClient) DoWithBody(ctx context.Context, path string, contentType string, body io.Reader) (int, error)
- func (c *HTTPClient) GetToken(ctx context.Context) (*oauth2.Token, error)
- func (c *HTTPClient) Iter(ctx context.Context, resource string, params map[string]string) ([]json.RawMessage, error)
- func (c *HTTPClient) PostAuth(ctx context.Context, path string, data interface{}, username string, ...) (statusCode int, err error)
- func (c *HTTPClient) RequestsCount() int
- func (c *HTTPClient) ThrottleDeadline() time.Time
- func (c *HTTPClient) VerifyAndGetToken(ctx context.Context, agentID string) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIErrorContent ¶
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 ¶
IsAuthError return true if the error is an APIError due to authentication failure.
func IsBadRequest ¶
IsBadRequest return true if the error is an APIError due to 400.
func IsNotFound ¶
IsNotFound return true if the error is an APIError due to 404.
func IsServerError ¶
IsServerError return true if the error is an APIError due to 5xx.
func IsThrottleError ¶
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).
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 ¶
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 ¶
VerifyAndGetToken is used to get a valid token. It differs from GetToken because the token is only renewed if necessary.