Skip to content

Latest commit

 

History

History
426 lines (266 loc) · 14.5 KB

GODOC.md

File metadata and controls

426 lines (266 loc) · 14.5 KB

blaster

import "github.com/joelhill/go-rest-http-blaster"

client.go ifaces.go invision_req014.go logrus_shim.go package_scope.go

const NAME = "blaster"

NAME is the name of this library

func SetDefaults(defaults *Defaults)

SetDefaults will apply package-level default values to be used on all requests

type CircuitBreakerPrototype interface {
    Execute(func() (interface{}, error)) (interface{}, error)
}

CircuitBreakerPrototype defines the circuit breaker Execute function signature

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

Client encapsulates the http Request functionality

func NewClient(uri string) (*Client, error)

NewClient will initialize and return a new client with a request and endpoint. The client's content type defaults to application/json

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, payload interface{}) (int, error)

Delete performs an HTTP DELETE request

func (*Client) Do

func (c *Client) Do(ctx context.Context, method string, payload interface{}) (int, error)

Do will prepare the request and either run it directly or from within a circuit breaker

func (*Client) Duration

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

Duration will return the elapsed time of the request in an int64 nanosecond count

func (*Client) Get

func (c *Client) Get(ctx context.Context) (int, error)

Get performs an HTTP GET request

func (c *Client) KeepRawResponse()

KeepRawResponse will cause the raw bytes from the http response to be retained

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, payload interface{}) (int, error)

Patch performs an HTTP PATCH request with the specified payload

func (*Client) Post

func (c *Client) Post(ctx context.Context, payload interface{}) (int, error)

Post performs an HTTP POST request with the specified payload

func (*Client) Put

func (c *Client) Put(ctx context.Context, payload interface{}) (int, error)

Put performs an HTTP PUT request with the specified payload

func (*Client) RawResponse

func (c *Client) RawResponse() []byte

RawResponse is a shortcut to access the raw bytes returned in the http response

func (c *Client) SetCircuitBreaker(cb CircuitBreakerPrototype)

SetCircuitBreaker sets the optional circuit breaker interface that wraps the http request.

func (c *Client) SetContentType(ct string)

SetContentType will set the request content type. By default, all requests are of type application/json. If you wish to use a different type, here is where you override it. Also note that if you do provide a content type, your payload for POST, PUT, or PATCH must be a byte slice or it must be convertible to a byte slice

func (*Client) SetHeader

func (c *Client) SetHeader(key string, value string)

SetHeader allows for custom http headers

func (*Client) SetLogger

func (c *Client) SetLogger(logger log.Logger)

SetLogger will set the client's internal logger. If no logger is set, a no-op logger will be used

func (c *Client) SetStatsdDelegate(sdClient StatsdClientPrototype, stat string, tags []string)

SetStatsdDelegate will set the statsd client, the stat, and tags

func (*Client) SetTimeoutMS

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

SetTimeoutMS sets the maximum number of milliseconds allowed for a request to complete. The default request timeout is 8 seconds (8000 ms)

func (c *Client) StatusCodeIsError() bool

StatusCodeIsError is a shortcut to determine if the status code is considered an error

func (*Client) WillSaturate

func (c *Client) WillSaturate(proto interface{})

WillSaturate assigns the interface that will be saturated when the request succeeds. It is assumed that the value passed into this function can be saturated via the unmarshalling of json. If that is not the case, you will need to process the raw bytes returned in the response instead

func (c *Client) WillSaturateOnError(proto interface{})

WillSaturateOnError assigns the interface that will be saturated when the request fails. It is assumed that the value passed into this function can be saturated via the unmarshalling of json. If that is not the case, you will need to process the raw bytes returned in the response instead. This library treats an error as any response with a status code not in the 2XX range.

func (c *Client) WillSaturateWithStatusCode(statusCode int, proto interface{})

WillSaturateWithStatusCode assigns the interface that will be saturated when a specific response code is encountered. This overrides the value of WillSaturate or WillSaturateOnError for the same code. For example, if a value is passed into this function that should saturate on a 200 response code, that will take precedence over anything set in WillSaturate, but will only return the saturated value for a 200, and no other 2XX-level code, unless specified here.

type Defaults struct {
    // ServiceName is the name of the calling service
    ServiceName string

    // TracerProviderFunc is a function that provides
    // the opentracing.Tracer for tracing HTTP requests
    TracerProviderFunc func(ctx context.Context, operationName string, r *http.Request) (*http.Request, opentracing.Span)

    // ContextLoggerProviderFunc is a function that provides
    // a logger from the current context.  If this function
    // is not set, the client will create a new logger for
    // the Request.
    // Deprecated: This function will return a generic Logger interface (defined in github.com/InVisionApp/go-logger) instead of a vendor-specific implementation
    ContextLoggerProviderFunc func(ctx context.Context) (*logrus.Entry, bool)

    // RequestIDProviderFunc is a function that provides the
    // parent Request id used in tracing the caller's Request.
    // If this function is not set, the client will generate
    // a new UUID for the Request id.
    RequestIDProviderFunc func(ctx context.Context) (string, bool)

    // RequestSourceProviderFunc is a function that provides
    // the Request-Source header
    RequestSourceProviderFunc func(ctx context.Context) (string, bool)

    // UserAgent is a package-level user agent header used for
    // each outgoing request
    UserAgent string

    // RequireHeaders will cancel any request and return an error if any of the following
    // headers are missing:
    // 		Request-ID
    // 		Request-Source
    // 		Calling-Service
    RequireHeaders bool

    // StatsdRate is the statsd reporting rate
    StatsdRate float64

    // StatsdSuccessTag is the tag added to the statsd metric when the request succeeds (200 <= status_code < 300)
    StatsdSuccessTag string

    // StatsdFailureTag is the tag added to the statsd metric when the request fails
    StatsdFailureTag string
}

Defaults is a container for setting package level values

type IClient interface {
    Delete(ctx context.Context, payload interface{}) (int, error)
    Duration() time.Duration
    Do(ctx context.Context, method string, payload interface{}) (int, error)
    Get(ctx context.Context) (int, error)
    KeepRawResponse()
    Post(ctx context.Context, payload interface{}) (int, error)
    Put(ctx context.Context, payload interface{}) (int, error)
    Patch(ctx context.Context, payload interface{}) (int, error)
    RawResponse() []byte
    SetCircuitBreaker(cb CircuitBreakerPrototype)
    SetStatsdDelegate(sdClient StatsdClientPrototype, stat string, tags []string)
    SetContentType(ct string)
    SetHeader(key string, value string)
    SetNRTxnName(name string)
    SetTimeoutMS(timeout time.Duration)
    StatusCodeIsError() bool
    WillSaturate(proto interface{})
    WillSaturateOnError(proto interface{})
    WillSaturateWithStatusCode(statusCode int, proto interface{})
}

IClient - interface for the cb api client

type StatsdClientPrototype interface {
    Incr(name string, tags []string, rate float64) error
    Timing(name string, value time.Duration, tags []string, rate float64) error
}

StatsdClientPrototype defines the statsd client functions used in this library


Generated by godoc2md