service

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package service provides an HTTP client with features for logging, metrics, and resilience.It supports various functionalities like health checks, circuit-breaker and various authentication.

Package service is a generated GoMock package.

Index

Constants

View Source
const (
	ClosedState = iota
	OpenState
)

CircuitBreaker states.

Variables

View Source
var (
	// ErrCircuitOpen indicates that the circuit breaker is open.
	ErrCircuitOpen                        = errors.New("unable to connect to server at host")
	ErrUnexpectedCircuitBreakerResultType = errors.New("unexpected result type from circuit breaker")
)

Functions

This section is empty.

Types

type APIKeyAuthProvider added in v1.1.1

type APIKeyAuthProvider struct {
	HTTP
	// contains filtered or unexported fields
}

func (*APIKeyAuthProvider) Delete added in v1.1.1

func (a *APIKeyAuthProvider) Delete(ctx context.Context, path string, body []byte) (*http.Response, error)

func (*APIKeyAuthProvider) DeleteWithHeaders added in v1.1.1

func (a *APIKeyAuthProvider) DeleteWithHeaders(ctx context.Context, path string, body []byte, headers map[string]string) (
	*http.Response, error)

func (*APIKeyAuthProvider) Get added in v1.1.1

func (a *APIKeyAuthProvider) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error)

func (*APIKeyAuthProvider) GetWithHeaders added in v1.1.1

func (a *APIKeyAuthProvider) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	headers map[string]string) (*http.Response, error)

func (*APIKeyAuthProvider) Patch added in v1.1.1

func (a *APIKeyAuthProvider) Patch(ctx context.Context, path string, queryParams map[string]interface{}, body []byte) (
	*http.Response, error)

func (*APIKeyAuthProvider) PatchWithHeaders added in v1.1.1

func (a *APIKeyAuthProvider) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
	headers map[string]string) (*http.Response, error)

func (*APIKeyAuthProvider) Post added in v1.1.1

func (a *APIKeyAuthProvider) Post(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

func (*APIKeyAuthProvider) PostWithHeaders added in v1.1.1

func (a *APIKeyAuthProvider) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
	headers map[string]string) (*http.Response, error)

func (*APIKeyAuthProvider) Put added in v1.1.1

func (a *APIKeyAuthProvider) Put(ctx context.Context, api string, queryParams map[string]interface{}, body []byte) (
	*http.Response, error)

func (*APIKeyAuthProvider) PutWithHeaders added in v1.1.1

func (a *APIKeyAuthProvider) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{}, body []byte,
	headers map[string]string) (*http.Response, error)

type APIKeyConfig added in v1.1.1

type APIKeyConfig struct {
	APIKey string
}

func (*APIKeyConfig) AddOption added in v1.2.0

func (a *APIKeyConfig) AddOption(h HTTP) HTTP

type BasicAuthConfig added in v1.1.1

type BasicAuthConfig struct {
	UserName string
	Password string
}

func (*BasicAuthConfig) AddOption added in v1.2.0

func (a *BasicAuthConfig) AddOption(h HTTP) HTTP

type BasicAuthProvider added in v1.1.1

type BasicAuthProvider struct {
	HTTP
	// contains filtered or unexported fields
}

func (*BasicAuthProvider) Delete added in v1.1.1

func (ba *BasicAuthProvider) Delete(ctx context.Context, path string, body []byte) (*http.Response, error)

func (*BasicAuthProvider) DeleteWithHeaders added in v1.1.1

func (ba *BasicAuthProvider) DeleteWithHeaders(ctx context.Context, path string, body []byte,
	headers map[string]string) (*http.Response, error)

func (*BasicAuthProvider) Get added in v1.1.1

func (ba *BasicAuthProvider) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error)

func (*BasicAuthProvider) GetWithHeaders added in v1.1.1

func (ba *BasicAuthProvider) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	headers map[string]string) (*http.Response, error)

func (*BasicAuthProvider) Patch added in v1.1.1

func (ba *BasicAuthProvider) Patch(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

func (*BasicAuthProvider) PatchWithHeaders added in v1.1.1

func (ba *BasicAuthProvider) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

func (*BasicAuthProvider) Post added in v1.1.1

func (ba *BasicAuthProvider) Post(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

func (*BasicAuthProvider) PostWithHeaders added in v1.1.1

func (ba *BasicAuthProvider) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

func (*BasicAuthProvider) Put added in v1.1.1

func (ba *BasicAuthProvider) Put(ctx context.Context, api string, queryParams map[string]interface{}, body []byte) (*http.Response, error)

func (*BasicAuthProvider) PutWithHeaders added in v1.1.1

func (ba *BasicAuthProvider) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

type CircuitBreaker added in v0.2.0

type CircuitBreaker struct {
	HTTP
	// contains filtered or unexported fields
}

CircuitBreaker represents a circuit breaker implementation.

func NewCircuitBreaker added in v0.2.0

func NewCircuitBreaker(config CircuitBreakerConfig, h HTTP) *CircuitBreaker

NewCircuitBreaker creates a new CircuitBreaker instance based on the provided config.

func (*CircuitBreaker) Delete added in v0.2.0

func (cb *CircuitBreaker) Delete(ctx context.Context, path string, body []byte) (
	*http.Response, error)

Delete is a wrapper for doRequest with the DELETE method and headers.

func (*CircuitBreaker) DeleteWithHeaders added in v0.2.0

func (cb *CircuitBreaker) DeleteWithHeaders(ctx context.Context, path string, body []byte, headers map[string]string) (
	*http.Response, error)

DeleteWithHeaders is a wrapper for doRequest with the DELETE method and headers.

func (*CircuitBreaker) Get added in v0.2.0

func (cb *CircuitBreaker) Get(ctx context.Context, path string, queryParams map[string]interface{}) (*http.Response, error)

func (*CircuitBreaker) GetWithHeaders added in v0.2.0

func (cb *CircuitBreaker) GetWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	headers map[string]string) (*http.Response, error)

func (*CircuitBreaker) Patch added in v0.2.0

func (cb *CircuitBreaker) Patch(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

Patch is a wrapper for doRequest with the PATCH method and headers.

func (*CircuitBreaker) PatchWithHeaders added in v0.2.0

func (cb *CircuitBreaker) PatchWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

PatchWithHeaders is a wrapper for doRequest with the PATCH method and headers.

func (*CircuitBreaker) Post added in v0.2.0

func (cb *CircuitBreaker) Post(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

Post is a wrapper for doRequest with the POST method and headers.

func (*CircuitBreaker) PostWithHeaders added in v0.2.0

func (cb *CircuitBreaker) PostWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

PostWithHeaders is a wrapper for doRequest with the POST method and headers.

func (*CircuitBreaker) Put added in v1.1.1

func (cb *CircuitBreaker) Put(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte) (*http.Response, error)

Put is a wrapper for doRequest with the PUT method and headers.

func (*CircuitBreaker) PutWithHeaders added in v0.2.0

func (cb *CircuitBreaker) PutWithHeaders(ctx context.Context, path string, queryParams map[string]interface{},
	body []byte, headers map[string]string) (*http.Response, error)

PutWithHeaders is a wrapper for doRequest with the PUT method and headers.

type CircuitBreakerConfig added in v0.2.0

type CircuitBreakerConfig struct {
	Threshold int           // Threshold represents the max no of retry before switching the circuit breaker state.
	Interval  time.Duration // Interval represents the time interval duration between hitting the HealthURL
}

CircuitBreakerConfig holds the configuration for the CircuitBreaker.

func (*CircuitBreakerConfig) AddOption added in v1.2.0

func (cb *CircuitBreakerConfig) AddOption(h HTTP) HTTP

type ErrorLog

type ErrorLog struct {
	*Log
	ErrorMessage string `json:"errorMessage"`
}

func (*ErrorLog) PrettyPrint added in v1.4.0

func (el *ErrorLog) PrettyPrint(writer io.Writer)

type HTTP

type HTTP interface {

	// HealthCheck to get the service health and report it to the current application
	HealthCheck(ctx context.Context) *Health
	// contains filtered or unexported methods
}

func NewHTTPService

func NewHTTPService(serviceAddress string, logger Logger, metrics Metrics, options ...Options) HTTP

NewHTTPService function creates a new instance of the httpService struct, which implements the HTTP interface. It initializes the http.Client, url, Tracer, and Logger fields of the httpService struct with the provided values.

type Health added in v0.2.0

type Health struct {
	Status  string                 `json:"status"`
	Details map[string]interface{} `json:"details"`
}

type HealthConfig added in v0.2.0

type HealthConfig struct {
	HealthEndpoint string
}

func (*HealthConfig) AddOption added in v1.2.0

func (h *HealthConfig) AddOption(svc HTTP) HTTP

type Log

type Log struct {
	Timestamp     time.Time `json:"timestamp"`
	ResponseTime  int64     `json:"latency"`
	CorrelationID string    `json:"correlationId"`
	ResponseCode  int       `json:"responseCode"`
	HTTPMethod    string    `json:"httpMethod"`
	URI           string    `json:"uri"`
}

func (*Log) PrettyPrint added in v1.4.0

func (l *Log) PrettyPrint(writer io.Writer)

type Logger

type Logger interface {
	Log(args ...interface{})
}

type Metrics added in v0.3.0

type Metrics interface {
	RecordHistogram(ctx context.Context, name string, value float64, labels ...string)
}

type MockMetrics added in v1.1.1

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

MockMetrics is a mock of Metrics interface.

func NewMockMetrics added in v1.1.1

func NewMockMetrics(ctrl *gomock.Controller) *MockMetrics

NewMockMetrics creates a new mock instance.

func (*MockMetrics) EXPECT added in v1.1.1

func (m *MockMetrics) EXPECT() *MockMetricsMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockMetrics) RecordHistogram added in v1.1.1

func (m *MockMetrics) RecordHistogram(ctx context.Context, name string, value float64, labels ...string)

RecordHistogram mocks base method.

type MockMetricsMockRecorder added in v1.1.1

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

MockMetricsMockRecorder is the mock recorder for MockMetrics.

func (*MockMetricsMockRecorder) RecordHistogram added in v1.1.1

func (mr *MockMetricsMockRecorder) RecordHistogram(ctx, name, value any, labels ...any) *gomock.Call

RecordHistogram indicates an expected call of RecordHistogram.

type OAuthConfig added in v1.1.1

type OAuthConfig struct {
	// ClientID is the application's ID.
	ClientID string

	// ClientSecret is the application's secret.
	ClientSecret string

	// TokenURL is the resource server's token endpoint
	// URL. This is a constant specific to each server.
	TokenURL string

	// Scope specifies optional requested permissions.
	Scopes []string

	// EndpointParams specifies additional parameters for requests to the token endpoint.
	EndpointParams url.Values
}

OAuthConfig describes a 2-legged OAuth2 flow, with both the client application information and the server's endpoint URLs.

func (*OAuthConfig) AddOption added in v1.2.0

func (h *OAuthConfig) AddOption(svc HTTP) HTTP

type Options added in v0.2.0

type Options interface {
	AddOption(h HTTP) HTTP
}

type Response

type Response struct {
	Body       []byte
	StatusCode int
	// contains filtered or unexported fields
}

func (*Response) GetHeader

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

Jump to

Keyboard shortcuts

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