client

package
v4.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	DefaultAPIVersion = "1.3.6"
	DefaultAPIBaseURL = "https://api.upcloud.com"

	// The default timeout (in seconds)
	DefaultTimeout = 60

	EnvDebugAPIBaseURL            string = "UPCLOUD_DEBUG_API_BASE_URL"
	EnvDebugSkipCertificateVerify string = "UPCLOUD_DEBUG_SKIP_CERTIFICATE_VERIFY"
)

Constants

View Source
const (
	ErrorTypeError = iota
	ErrorTypeProblem
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	UserAgent string
	// contains filtered or unexported fields
}

Client represents an API client

func New

func New(userName, password string) *Client

New creates ands returns a new client configured with the specified user and password

Example
client := New(os.Getenv("UPCLOUD_USERNAME"), os.Getenv("UPCLOUD_PASSWORD"))
client.SetTimeout(10 * time.Second)
Output:

func NewWithHTTPClient

func NewWithHTTPClient(userName string, password string, httpClient *http.Client) *Client

NewWithHTTPClient creates ands returns a new client configured with the specified user and password and using a supplied `http.Client`.

Example
client := NewWithHTTPClient(os.Getenv("UPCLOUD_USERNAME"), os.Getenv("UPCLOUD_PASSWORD"), &http.Client{
	// setup custom HTTP client
})
client.SetTimeout(10 * time.Second)
Output:

func (*Client) AddRequestHeaders

func (c *Client) AddRequestHeaders(request *http.Request) *http.Request

Adds common headers to the specified request

func (*Client) CreateRequestURL

func (c *Client) CreateRequestURL(location string) string

CreateRequestURL creates and returns a complete request URL for the specified API location using a newer API version

func (*Client) GetTimeout

func (c *Client) GetTimeout() time.Duration

GetTimeout returns current timeout

func (*Client) PerformJSONDeleteRequest

func (c *Client) PerformJSONDeleteRequest(url string) error

PerformJSONDeleteRequest performs a DELETE request to the specified URL and returns eventual errors

func (*Client) PerformJSONDeleteRequestWithResponseBody

func (c *Client) PerformJSONDeleteRequestWithResponseBody(url string) ([]byte, error)

PerformJSONDeleteRequestWithResponseBody performs a DELETE request to the specified URL and returns the response body and eventual errors

func (*Client) PerformJSONGetRequest

func (c *Client) PerformJSONGetRequest(url string) ([]byte, error)

PerformJSONGetRequest performs a GET request to the specified URL and returns the response body and eventual errors

func (*Client) PerformJSONPatchRequest

func (c *Client) PerformJSONPatchRequest(url string, requestBody []byte) ([]byte, error)

PerformJSONPatchRequest performs a PATCH request to the specified URL and returns the response body and eventual errors

func (*Client) PerformJSONPostRequest

func (c *Client) PerformJSONPostRequest(url string, requestBody []byte) ([]byte, error)

PerformJSONPostRequest performs a POST request to the specified URL and returns the response body and eventual errors

func (*Client) PerformJSONPutRequest

func (c *Client) PerformJSONPutRequest(url string, requestBody []byte) ([]byte, error)

PerformJSONPutRequest performs a PUT request to the specified URL and returns the response body and eventual errors

func (*Client) PerformJSONPutUploadRequest

func (c *Client) PerformJSONPutUploadRequest(url string, requestBody io.Reader) ([]byte, error)

PerformJSONPutUploadRequest performs a PUT request to the specified URL with an io.Reader and returns the response body and eventual errors

func (*Client) PerformRequest

func (c *Client) PerformRequest(request *http.Request) ([]byte, error)

Performs the specified HTTP request and returns the response through handleResponse()

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout sets the client timeout to the specified amount of seconds

type ClientContext added in v4.7.0

type ClientContext struct {
	*Client
}

ClientContext represents an API client with context support

func NewWithContext added in v4.7.0

func NewWithContext(userName, password string) *ClientContext

NewWithContext creates ands returns a new client with context support configured with the specified user and password

func NewWithHTTPClientContext added in v4.7.0

func NewWithHTTPClientContext(userName string, password string, httpClient *http.Client) *ClientContext

NewWithHTTPClientContext creates ands returns a new client with context support configured with the specified user and password and using a supplied `http.Client`.

func (*ClientContext) PerformJSONDeleteRequest added in v4.7.0

func (c *ClientContext) PerformJSONDeleteRequest(ctx context.Context, url string) error

PerformJSONDeleteRequest performs a DELETE request to the specified URL and returns eventual errors

func (*ClientContext) PerformJSONDeleteRequestWithResponseBody added in v4.7.0

func (c *ClientContext) PerformJSONDeleteRequestWithResponseBody(ctx context.Context, url string) ([]byte, error)

PerformJSONDeleteRequestWithResponseBody performs a DELETE request to the specified URL and returns the response body and eventual errors

func (*ClientContext) PerformJSONGetRequest added in v4.7.0

func (c *ClientContext) PerformJSONGetRequest(ctx context.Context, url string) ([]byte, error)

PerformJSONGetRequest performs a GET request to the specified URL and returns the response body and eventual errors

func (*ClientContext) PerformJSONPatchRequest added in v4.7.0

func (c *ClientContext) PerformJSONPatchRequest(ctx context.Context, url string, requestBody []byte) ([]byte, error)

PerformJSONPatchRequest performs a PATCH request to the specified URL and returns the response body and eventual errors

func (*ClientContext) PerformJSONPostRequest added in v4.7.0

func (c *ClientContext) PerformJSONPostRequest(ctx context.Context, url string, requestBody []byte) ([]byte, error)

PerformJSONPostRequest performs a POST request to the specified URL and returns the response body and eventual errors

func (*ClientContext) PerformJSONPutRequest added in v4.7.0

func (c *ClientContext) PerformJSONPutRequest(ctx context.Context, url string, requestBody []byte) ([]byte, error)

PerformJSONPutRequest performs a PUT request to the specified URL and returns the response body and eventual errors

func (*ClientContext) PerformJSONPutUploadRequest added in v4.7.0

func (c *ClientContext) PerformJSONPutUploadRequest(ctx context.Context, url string, requestBody io.Reader) ([]byte, error)

PerformJSONPutUploadRequest performs a PUT request to the specified URL with an io.Reader and returns the response body and eventual errors

func (*ClientContext) PerformJSONRequest added in v4.7.0

func (c *ClientContext) PerformJSONRequest(ctx context.Context, method, url string, body io.Reader) ([]byte, error)

Performs the specified HTTP request with context and returns the response through handleResponse()

type Error

type Error struct {
	ErrorCode    int
	ErrorMessage string
	ResponseBody []byte
	Type         ErrorType
}

Error represents an error returned from the client. Errors are thrown when requests don't have a successful status code

func (*Error) Error

func (e *Error) Error() string

Error implements the Error interface

type ErrorType added in v4.4.0

type ErrorType int

Jump to

Keyboard shortcuts

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