http

package
v0.59.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CompressorMinimumSize is the required size in bytes that a body exceeds before it is compressed
	CompressorMinimumSize = 150
)

Variables

View Source
var (
	// ErrNotFound is returned when the resource was not found in New Relic.
	ErrNotFound = errors.New("newrelic: Resource not found")
)

Functions

func RetryPolicy

func RetryPolicy(ctx context.Context, resp *http.Response, err error) (bool, error)

RetryPolicy provides a callback for retryablehttp's CheckRetry, which will retry on connection errors and server errors.

Types

type ClassicV2Authorizer added in v0.11.0

type ClassicV2Authorizer struct{}

ClassicV2Authorizer authorizes V2 endpoints that cannot use a personal API key.

func (*ClassicV2Authorizer) AuthorizeRequest added in v0.11.0

func (a *ClassicV2Authorizer) AuthorizeRequest(r *Request, c *config.Config)

AuthorizeRequest is responsible for setting up auth for a request.

type Client added in v0.17.0

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

Client represents a client for communicating with the New Relic APIs.

func NewClient

func NewClient(cfg config.Config) Client

NewClient is used to create a new instance of Client.

func NewTestAPIClient

func NewTestAPIClient(t *testing.T, handler http.Handler) Client

NewTestAPIClient returns a test Client instance that is configured to communicate with a mock server.

func (*Client) Delete added in v0.17.0

func (c *Client) Delete(
	url string,
	queryParams interface{},
	respBody interface{},
) (*http.Response, error)

Delete represents an HTTP DELETE request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) DeleteWithContext added in v0.51.0

func (c *Client) DeleteWithContext(
	ctx context.Context,
	url string,
	queryParams interface{},
	respBody interface{},
) (*http.Response, error)

DeleteWithContext represents an HTTP DELETE request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) Do added in v0.17.0

func (c *Client) Do(req *Request) (*http.Response, error)

Do initiates an HTTP request as configured by the passed Request struct.

func (*Client) Get added in v0.17.0

func (c *Client) Get(
	url string,
	queryParams interface{},
	respBody interface{},
) (*http.Response, error)

Get represents an HTTP GET request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) GetWithContext added in v0.51.0

func (c *Client) GetWithContext(
	ctx context.Context,
	url string,
	queryParams interface{},
	respBody interface{},
) (*http.Response, error)

GetWithContext represents an HTTP GET request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) NerdGraphQuery added in v0.20.0

func (c *Client) NerdGraphQuery(query string, vars map[string]interface{}, respBody interface{}) error

NerdGraphQuery runs a Nerdgraph query.

func (*Client) NerdGraphQueryWithContext added in v0.51.0

func (c *Client) NerdGraphQueryWithContext(ctx context.Context, query string, vars map[string]interface{}, respBody interface{}) error

NerdGraphQueryWithContext runs a Nerdgraph query.

func (*Client) NewNerdGraphRequest added in v0.20.0

func (c *Client) NewNerdGraphRequest(query string, vars map[string]interface{}, respBody interface{}) (*Request, error)

NewNerdGraphRequest runs a Nerdgraph request object.

func (*Client) NewRequest added in v0.20.0

func (c *Client) NewRequest(method string, url string, params interface{}, reqBody interface{}, value interface{}) (*Request, error)

NewRequest creates a new Request struct.

func (*Client) Post added in v0.17.0

func (c *Client) Post(
	url string,
	queryParams interface{},
	reqBody interface{},
	respBody interface{},
) (*http.Response, error)

Post represents an HTTP POST request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The reqBody argument will be marshaled to JSON from the type provided and included in the request body. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) PostWithContext added in v0.51.0

func (c *Client) PostWithContext(
	ctx context.Context,
	url string,
	queryParams interface{},
	reqBody interface{},
	respBody interface{},
) (*http.Response, error)

PostWithContext represents an HTTP POST request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The reqBody argument will be marshaled to JSON from the type provided and included in the request body. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) Put added in v0.17.0

func (c *Client) Put(
	url string,
	queryParams interface{},
	reqBody interface{},
	respBody interface{},
) (*http.Response, error)

Put represents an HTTP PUT request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The reqBody argument will be marshaled to JSON from the type provided and included in the request body. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) PutWithContext added in v0.51.0

func (c *Client) PutWithContext(
	ctx context.Context,
	url string,
	queryParams interface{},
	reqBody interface{},
	respBody interface{},
) (*http.Response, error)

PutWithContext represents an HTTP PUT request to a New Relic API. The queryParams argument can be used to add query string parameters to the requested URL. The reqBody argument will be marshaled to JSON from the type provided and included in the request body. The respBody argument will be unmarshaled from JSON in the response body to the type provided. If respBody is not nil and the response body cannot be unmarshaled to the type provided, an error will be returned.

func (*Client) SetAuthStrategy added in v0.17.0

func (c *Client) SetAuthStrategy(da RequestAuthorizer)

SetAuthStrategy is used to set the default auth strategy for this client which can be overridden per request

func (*Client) SetErrorValue added in v0.17.0

func (c *Client) SetErrorValue(v ErrorResponse) *Client

SetErrorValue is used to unmarshal error body responses in JSON format.

func (*Client) SetRequestCompressor added in v0.26.0

func (c *Client) SetRequestCompressor(compressor RequestCompressor)

SetRequestCompressor is used to enable compression on the request using the RequestCompressor specified

type DefaultErrorResponse

type DefaultErrorResponse struct {
	ErrorDetail ErrorDetail `json:"error"`
}

DefaultErrorResponse represents the default error response from New Relic.

func (*DefaultErrorResponse) Error

func (e *DefaultErrorResponse) Error() string

func (*DefaultErrorResponse) IsNotFound added in v0.21.1

func (e *DefaultErrorResponse) IsNotFound() bool

func (*DefaultErrorResponse) IsRetryableError added in v0.48.1

func (e *DefaultErrorResponse) IsRetryableError() bool

func (*DefaultErrorResponse) IsUnauthorized added in v0.55.2

func (e *DefaultErrorResponse) IsUnauthorized(resp *http.Response) bool

IsUnauthorized checks a response for a 401 Unauthorize HTTP status code.

func (*DefaultErrorResponse) New added in v0.13.0

New creates a new instance of the DefaultErrorResponse struct.

type ErrorDetail

type ErrorDetail struct {
	Title    string   `json:"title"`
	Messages []string `json:"messages"`
}

ErrorDetail represents a New Relic response error detail.

type ErrorResponse

type ErrorResponse interface {
	IsNotFound() bool
	IsRetryableError() bool
	IsUnauthorized(resp *http.Response) bool
	Error() string
	New() ErrorResponse
}

ErrorResponse provides an interface for obtaining a single error message from an error response object.

type GraphQLDownstreamResponse added in v0.21.1

type GraphQLDownstreamResponse struct {
	Extensions struct {
		Code             string `json:"code,omitempty"`
		ValidationErrors []struct {
			Name   string `json:"name,omitempty"`
			Reason string `json:"reason,omitempty"`
		} `json:"validationErrors,omitempty"`
	} `json:"extensions,omitempty"`
	Message string `json:"message,omitempty"`
}

GraphQLDownstreamResponse represents an error's downstream response.

type GraphQLError added in v0.21.1

type GraphQLError struct {
	Message    string   `json:"message,omitempty"`
	Path       []string `json:"path,omitempty"`
	Extensions struct {
		ErrorClass string `json:"errorClass,omitempty"`
		ErrorCode  string `json:"error_code,omitempty"`
	} `json:"extensions,omitempty"`
	DownstreamResponse []GraphQLDownstreamResponse `json:"downstreamResponse,omitempty"`
}

GraphQLError represents a single error.

type GraphQLErrorResponse added in v0.21.1

type GraphQLErrorResponse struct {
	Errors []GraphQLError `json:"errors"`
}

GraphQLErrorResponse represents a default error response body.

func (*GraphQLErrorResponse) Error added in v0.21.1

func (r *GraphQLErrorResponse) Error() string

func (*GraphQLErrorResponse) IsNotFound added in v0.21.1

func (r *GraphQLErrorResponse) IsNotFound() bool

IsNotFound determines if the error is due to a missing resource.

func (*GraphQLErrorResponse) IsRetryableError added in v0.48.1

func (r *GraphQLErrorResponse) IsRetryableError() bool

IsRetryableError determines if the error is due to a server timeout, or another error that we might want to retry.

func (*GraphQLErrorResponse) IsUnauthorized added in v0.55.2

func (r *GraphQLErrorResponse) IsUnauthorized(resp *http.Response) bool

IsUnauthorized checks a NerdGraph response for a 401 Unauthorize HTTP status code, then falls back to check the nested extensions error_code field for `BAD_API_KEY`.

func (*GraphQLErrorResponse) New added in v0.21.1

New creates a new instance of GraphQLErrorRepsonse.

type GzipCompressor added in v0.26.0

type GzipCompressor struct{}

GzipCompressor compresses the body with gzip

func (*GzipCompressor) Compress added in v0.26.0

func (c *GzipCompressor) Compress(r *Request, body []byte) (io.Reader, error)

CompressRequest gzips the body, sets the content encoding header, and returns a reader to the caller

type InsightsInsertKeyAuthorizer added in v0.26.0

type InsightsInsertKeyAuthorizer struct{}

InsightsInsertKeyAuthorizer authorizes sending custom events to New Relic.

func (*InsightsInsertKeyAuthorizer) AuthorizeRequest added in v0.26.0

func (a *InsightsInsertKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config)

type LicenseKeyAuthorizer added in v0.38.0

type LicenseKeyAuthorizer struct{}

func (*LicenseKeyAuthorizer) AuthorizeRequest added in v0.38.0

func (a *LicenseKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config)

type LinkHeaderPager

type LinkHeaderPager struct{}

LinkHeaderPager represents a pagination implementation that adheres to RFC 5988.

func (*LinkHeaderPager) Parse

func (l *LinkHeaderPager) Parse(resp *http.Response) Paging

Parse is used to parse a pagination context from an HTTP response.

type LogsInsertKeyAuthorizer added in v0.41.0

type LogsInsertKeyAuthorizer struct{}

func (*LogsInsertKeyAuthorizer) AuthorizeRequest added in v0.41.0

func (a *LogsInsertKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config)

type NerdGraphAuthorizer added in v0.11.0

type NerdGraphAuthorizer struct{}

NerdGraphAuthorizer authorizes calls to NerdGraph.

func (*NerdGraphAuthorizer) AuthorizeRequest added in v0.11.0

func (a *NerdGraphAuthorizer) AuthorizeRequest(r *Request, c *config.Config)

AuthorizeRequest is responsible for setting up auth for a request.

type NoneCompressor added in v0.26.0

type NoneCompressor struct{}

NoneCompressor does not compress the request at all.

func (*NoneCompressor) Compress added in v0.26.0

func (c *NoneCompressor) Compress(r *Request, body []byte) (io.Reader, error)

CompressRequest returns a reader for the body to the caller

type Pager

type Pager interface {
	Parse(res *http.Response) Paging
}

Pager represents a pagination implementation.

type Paging

type Paging struct {
	Next string
}

Paging represents the pagination context returned from the Pager implementation.

type PersonalAPIKeyCapableV2Authorizer added in v0.11.0

type PersonalAPIKeyCapableV2Authorizer struct{}

PersonalAPIKeyCapableV2Authorizer authorizes V2 endpoints that can use a personal API key.

func (*PersonalAPIKeyCapableV2Authorizer) AuthorizeRequest added in v0.11.0

func (a *PersonalAPIKeyCapableV2Authorizer) AuthorizeRequest(r *Request, c *config.Config)

AuthorizeRequest is responsible for setting up auth for a request.

type Request added in v0.13.0

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

Request represents a configurable HTTP request.

func (*Request) DelHeader added in v0.26.0

func (r *Request) DelHeader(key string)

DelHeader deletes the specified header if it exists

func (*Request) GetHeader added in v0.26.0

func (r *Request) GetHeader(key string) string

GetHeader returns the value of the header requested

func (*Request) SetAuthStrategy added in v0.13.0

func (r *Request) SetAuthStrategy(ra RequestAuthorizer)

SetAuthStrategy sets the authentication strategy for the request.

func (*Request) SetErrorValue added in v0.20.0

func (r *Request) SetErrorValue(e ErrorResponse)

SetErrorValue sets the error object for the request.

func (*Request) SetHeader added in v0.13.0

func (r *Request) SetHeader(key string, value string)

SetHeader sets a header on the underlying request.

func (*Request) SetServiceName added in v0.15.0

func (r *Request) SetServiceName(serviceName string)

SetServiceName sets the service name for the request.

func (*Request) WithContext added in v0.51.0

func (r *Request) WithContext(ctx context.Context)

WithContext sets the context of the underlying request.

type RequestAuthorizer added in v0.11.0

type RequestAuthorizer interface {
	AuthorizeRequest(r *Request, c *config.Config)
}

RequestAuthorizer is an interface that allows customizatino of how a request is authorized.

type RequestCompressor added in v0.26.0

type RequestCompressor interface {
	Compress(r *Request, body []byte) (io.Reader, error)
}

Jump to

Keyboard shortcuts

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