utils

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package utils //

Package utils //

Package utils //

Index

Constants

View Source
const (
	// HeaderContentType is the HTTP Content Type header.
	HeaderContentType = "Content-Type"

	// HeaderAuthorization is the HTTP Authorization Type header.
	HeaderAuthorization = "Authorization"

	// HeaderAccept is the HTTP Accept Type header.
	HeaderAccept = "Accept"

	// ContentTypeJSON is the Content-Type value for a JSON response.
	ContentTypeJSON = "application/json"
)

Variables

This section is empty.

Functions

func Client

func Client(client http.Client) func(r *HTTPRequester)

Client sets http client

func GetBoolValue

func GetBoolValue(value interface{}) (bool, error)

GetBoolValue will attempt to convert the given value to a bool

func GetFloatValue

func GetFloatValue(value interface{}) (float64, error)

GetFloatValue will attempt to convert the given value to a float64

func GetIntValue

func GetIntValue(value interface{}) (int64, error)

GetIntValue will attempt to convert the given value to an int64

func GetStringValue

func GetStringValue(value interface{}) (string, error)

GetStringValue will attempt to convert the given value to a string

func Headers

func Headers(headers ...Header) func(r *HTTPRequester)

Headers sets request headers

func IsValidAttribute

func IsValidAttribute(value interface{}) bool

IsValidAttribute check if attribute value is valid

func Retries

func Retries(retries int) func(r *HTTPRequester)

Retries sets max number of retries for failed calls

func Timeout

func Timeout(timeout time.Duration) func(r *HTTPRequester)

Timeout sets http client timeout

Types

type ExecGroup

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

ExecGroup is a utility for managing graceful, blocking cancellation of goroutines.

func NewExecGroup

func NewExecGroup(ctx context.Context, logger logging.OptimizelyLogProducer) *ExecGroup

NewExecGroup returns constructed object

func (ExecGroup) Go

func (c ExecGroup) Go(f func(ctx context.Context))

Go initiates a goroutine with the inputted function. Each invocation increments a shared WaitGroup before being initiated. Once the supplied function exits the WaitGroup is decremented. A common ctx is passed to each input function to signal a shutdown sequence.

func (ExecGroup) TerminateAndWait

func (c ExecGroup) TerminateAndWait()

TerminateAndWait sends termination signal and waits

type HTTPRequester

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

HTTPRequester contains main info

func NewHTTPRequester

func NewHTTPRequester(logger logging.OptimizelyLogProducer, params ...func(*HTTPRequester)) *HTTPRequester

NewHTTPRequester makes Requester with api and parameters. Sets defaults api has the base part of request's url, like http://localhost/api/v1

func (HTTPRequester) Do

func (r HTTPRequester) Do(url, method string, body io.Reader, headers []Header) (response []byte, responseHeaders http.Header, code int, err error)

Do executes request and returns response body for requested url

func (HTTPRequester) Get

func (r HTTPRequester) Get(url string, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)

Get executes HTTP GET with url and optional extra headers, returns body in []bytes

func (HTTPRequester) GetObj

func (r HTTPRequester) GetObj(url string, result interface{}, headers ...Header) error

GetObj executes HTTP GET with url and optional extra headers, returns filled object

func (HTTPRequester) Post

func (r HTTPRequester) Post(url string, body interface{}, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)

Post executes HTTP POST with url, body and optional extra headers

func (HTTPRequester) PostObj

func (r HTTPRequester) PostObj(url string, body, result interface{}, headers ...Header) error

PostObj executes HTTP POST with url, body and optional extra headers. Returns filled object

func (HTTPRequester) String

func (r HTTPRequester) String() string
type Header struct {
	Name, Value string
}

Header element to be sent

type Requester

type Requester interface {
	Get(url string, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)
	GetObj(url string, result interface{}, headers ...Header) error

	Post(url string, body interface{}, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)
	PostObj(url string, body interface{}, result interface{}, headers ...Header) error

	String() string
}

Requester is used to make outbound requests with

Jump to

Keyboard shortcuts

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