httpclient

package module
v0.0.0-...-7ea7ca6 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2020 License: BSD-3-Clause, MIT Imports: 14 Imported by: 0

README

httpclient

Simple HTTP client for Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v bool) *bool

Bool is a helper routine that allocates a new bool value to store v and returns a pointer to it.

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

func Int

func Int(v int) *int

Int is a helper routine that allocates a new int32 value to store v and returns a pointer to it, but unlike Int32 its argument value is an int.

func StreamToString

func StreamToString(stream io.Reader) string

StreamToString converts a reader to a string

func String

func String(v string) *string

String is a helper routine that allocates a new string value to store v and returns a pointer to it.

func Stringify

func Stringify(message interface{}) string

Stringify attempts to create a string representation of DigitalOcean types

Types

type ArgError

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

ArgError is an error that represents an error with an input to godo. It identifies the argument and the cause (if possible).

func NewArgError

func NewArgError(arg, reason string) *ArgError

NewArgError creates an InputError.

func (*ArgError) Error

func (e *ArgError) Error() string

type ClientOpt

type ClientOpt func(*HttpClient) error

ClientOpt are options for New.

func SetBaseURL

func SetBaseURL(bu string) ClientOpt

SetBaseURL is a client option for setting the base URL.

func SetUserAgent

func SetUserAgent(ua string) ClientOpt

SetUserAgent is a client option for setting the user agent.

type ErrorResponse

type ErrorResponse struct {
	// HTTP response that caused this error
	Response *http.Response
	// Raw error response in case error doesn't fit known structure
	Raw string
	// Type is the error group name (auth_error, request_error, api_error, external_error vs..)
	Type string `json:"type"`
	// Code is the error short name (invalid_id, user_not_found)
	Code string `json:"code"`
	// Message is the human readable form of the error message
	Message string `json:"message"`

	// RequestID returned from the API, useful to contact support.
	RequestID string `json:"request_id"`
}

An ErrorResponse reports the error caused by an API request

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type HttpClient

type HttpClient struct {
	BaseURL *url.URL

	UserAgent string

	Rate Rate
	// contains filtered or unexported fields
}

func New

func New(base string, insecureSkipVerify bool, opts ...ClientOpt) (*HttpClient, error)

New returns a new HTTP client instance.

func NewHttpClient

func NewHttpClient(baseURL *url.URL, insecureSkipVerify bool) *HttpClient

func (*HttpClient) Delete

func (c *HttpClient) Delete(ctx context.Context, path string, opts ...RequestOpt) (*Response, error)

helper function for making an http DELETE request.

func (*HttpClient) Do

func (c *HttpClient) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response will be written to v, without attempting to decode it.

func (*HttpClient) Get

func (c *HttpClient) Get(ctx context.Context, path string, out interface{}, opts ...RequestOpt) (*Response, error)

helper function for making an http GET request.

func (*HttpClient) NewRequest

func (c *HttpClient) NewRequest(ctx context.Context, method string, urlStr string, body interface{}, opts ...RequestOpt) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the BaseURL of the Client. Relative URLS should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included in as the request body.

func (*HttpClient) OnRequestCompleted

func (c *HttpClient) OnRequestCompleted(rc RequestCompletionCallback)

OnRequestCompleted sets the API request completion callback

func (*HttpClient) Patch

func (c *HttpClient) Patch(ctx context.Context, path string, in, out interface{}, opts ...RequestOpt) (*Response, error)

helper function for making an http PATCH request.

func (*HttpClient) Post

func (c *HttpClient) Post(ctx context.Context, path string, in, out interface{}, opts ...RequestOpt) (*Response, error)

helper function for making an http POST request.

func (*HttpClient) Put

func (c *HttpClient) Put(ctx context.Context, path string, in, out interface{}, opts ...RequestOpt) (*Response, error)

helper function for making an http PUT request.

type Rate

type Rate struct {
	// The number of request per hour the client is currently limited to.
	Limit int `json:"limit"`

	// The number of remaining requests the client can make this hour.
	Remaining int `json:"remaining"`

	// The time at which the current rate limit will reset.
	Reset Timestamp `json:"reset"`
}

Rate contains the rate limit for the current client.

func (Rate) String

func (r Rate) String() string

type RequestCompletionCallback

type RequestCompletionCallback func(*http.Request, *http.Response)

RequestCompletionCallback defines the type of the request callback function

type RequestOpt

type RequestOpt func(*http.Request) error

RequestOpt are options for New.

func SetBasicAuthentication

func SetBasicAuthentication(username string, password string) RequestOpt

func SetBearerToken

func SetBearerToken(token string) RequestOpt

func SetHeader

func SetHeader(key string, value string) RequestOpt

SetHeader option for setting the a header in the request

type Response

type Response struct {
	*http.Response

	Rate
}

Response is an API response. This wraps the standard http.Response returned from the API.

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp represents a time that can be unmarshalled from a JSON string formatted as either an RFC3339 or Unix timestamp. All exported methods of time.Time can be called on Timestamp.

func (Timestamp) Equal

func (t Timestamp) Equal(u Timestamp) bool

Equal reports whether t and u are equal based on time.Equal

func (Timestamp) String

func (t Timestamp) String() string

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Time is expected in RFC3339 or Unix format.

Jump to

Keyboard shortcuts

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