service

package
v1.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0 Imports: 20 Imported by: 2

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.

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

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.

Types

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 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 DefaultHeaders added in v1.7.0

type DefaultHeaders struct {
	Headers map[string]string
}

func (*DefaultHeaders) AddOption added in v1.7.0

func (a *DefaultHeaders) 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
	Timeout        int
}

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 MockHTTP added in v1.20.0

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

MockHTTP is a mock of HTTP interface.

func NewMockHTTP added in v1.20.0

func NewMockHTTP(ctrl *gomock.Controller) *MockHTTP

NewMockHTTP creates a new mock instance.

func (*MockHTTP) Delete added in v1.20.0

func (m *MockHTTP) Delete(ctx context.Context, api string, body []byte) (*http.Response, error)

Delete mocks base method.

func (*MockHTTP) DeleteWithHeaders added in v1.20.0

func (m *MockHTTP) DeleteWithHeaders(ctx context.Context, api string, body []byte, headers map[string]string) (*http.Response, error)

DeleteWithHeaders mocks base method.

func (*MockHTTP) EXPECT added in v1.20.0

func (m *MockHTTP) EXPECT() *MockHTTPMockRecorder

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

func (*MockHTTP) Get added in v1.20.0

func (m *MockHTTP) Get(ctx context.Context, api string, queryParams map[string]any) (*http.Response, error)

Get mocks base method.

func (*MockHTTP) GetWithHeaders added in v1.20.0

func (m *MockHTTP) GetWithHeaders(ctx context.Context, path string, queryParams map[string]any, headers map[string]string) (*http.Response, error)

GetWithHeaders mocks base method.

func (*MockHTTP) HealthCheck added in v1.20.0

func (m *MockHTTP) HealthCheck(ctx context.Context) *Health

HealthCheck mocks base method.

func (*MockHTTP) Patch added in v1.20.0

func (m *MockHTTP) Patch(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error)

Patch mocks base method.

func (*MockHTTP) PatchWithHeaders added in v1.20.0

func (m *MockHTTP) PatchWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PatchWithHeaders mocks base method.

func (*MockHTTP) Post added in v1.20.0

func (m *MockHTTP) Post(ctx context.Context, path string, queryParams map[string]any, body []byte) (*http.Response, error)

Post mocks base method.

func (*MockHTTP) PostWithHeaders added in v1.20.0

func (m *MockHTTP) PostWithHeaders(ctx context.Context, path string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PostWithHeaders mocks base method.

func (*MockHTTP) Put added in v1.20.0

func (m *MockHTTP) Put(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error)

Put mocks base method.

func (*MockHTTP) PutWithHeaders added in v1.20.0

func (m *MockHTTP) PutWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PutWithHeaders mocks base method.

type MockHTTPMockRecorder added in v1.20.0

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

MockHTTPMockRecorder is the mock recorder for MockHTTP.

func (*MockHTTPMockRecorder) Delete added in v1.20.0

func (mr *MockHTTPMockRecorder) Delete(ctx, api, body any) *gomock.Call

Delete indicates an expected call of Delete.

func (*MockHTTPMockRecorder) DeleteWithHeaders added in v1.20.0

func (mr *MockHTTPMockRecorder) DeleteWithHeaders(ctx, api, body, headers any) *gomock.Call

DeleteWithHeaders indicates an expected call of DeleteWithHeaders.

func (*MockHTTPMockRecorder) Get added in v1.20.0

func (mr *MockHTTPMockRecorder) Get(ctx, api, queryParams any) *gomock.Call

Get indicates an expected call of Get.

func (*MockHTTPMockRecorder) GetWithHeaders added in v1.20.0

func (mr *MockHTTPMockRecorder) GetWithHeaders(ctx, path, queryParams, headers any) *gomock.Call

GetWithHeaders indicates an expected call of GetWithHeaders.

func (*MockHTTPMockRecorder) HealthCheck added in v1.20.0

func (mr *MockHTTPMockRecorder) HealthCheck(ctx any) *gomock.Call

HealthCheck indicates an expected call of HealthCheck.

func (*MockHTTPMockRecorder) Patch added in v1.20.0

func (mr *MockHTTPMockRecorder) Patch(ctx, api, queryParams, body any) *gomock.Call

Patch indicates an expected call of Patch.

func (*MockHTTPMockRecorder) PatchWithHeaders added in v1.20.0

func (mr *MockHTTPMockRecorder) PatchWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call

PatchWithHeaders indicates an expected call of PatchWithHeaders.

func (*MockHTTPMockRecorder) Post added in v1.20.0

func (mr *MockHTTPMockRecorder) Post(ctx, path, queryParams, body any) *gomock.Call

Post indicates an expected call of Post.

func (*MockHTTPMockRecorder) PostWithHeaders added in v1.20.0

func (mr *MockHTTPMockRecorder) PostWithHeaders(ctx, path, queryParams, body, headers any) *gomock.Call

PostWithHeaders indicates an expected call of PostWithHeaders.

func (*MockHTTPMockRecorder) Put added in v1.20.0

func (mr *MockHTTPMockRecorder) Put(ctx, api, queryParams, body any) *gomock.Call

Put indicates an expected call of Put.

func (*MockHTTPMockRecorder) PutWithHeaders added in v1.20.0

func (mr *MockHTTPMockRecorder) PutWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call

PutWithHeaders indicates an expected call of PutWithHeaders.

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 MockhttpClient added in v1.20.0

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

MockhttpClient is a mock of httpClient interface.

func NewMockhttpClient added in v1.20.0

func NewMockhttpClient(ctrl *gomock.Controller) *MockhttpClient

NewMockhttpClient creates a new mock instance.

func (*MockhttpClient) Delete added in v1.20.0

func (m *MockhttpClient) Delete(ctx context.Context, api string, body []byte) (*http.Response, error)

Delete mocks base method.

func (*MockhttpClient) DeleteWithHeaders added in v1.20.0

func (m *MockhttpClient) DeleteWithHeaders(ctx context.Context, api string, body []byte, headers map[string]string) (*http.Response, error)

DeleteWithHeaders mocks base method.

func (*MockhttpClient) EXPECT added in v1.20.0

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

func (*MockhttpClient) Get added in v1.20.0

func (m *MockhttpClient) Get(ctx context.Context, api string, queryParams map[string]any) (*http.Response, error)

Get mocks base method.

func (*MockhttpClient) GetWithHeaders added in v1.20.0

func (m *MockhttpClient) GetWithHeaders(ctx context.Context, path string, queryParams map[string]any, headers map[string]string) (*http.Response, error)

GetWithHeaders mocks base method.

func (*MockhttpClient) Patch added in v1.20.0

func (m *MockhttpClient) Patch(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error)

Patch mocks base method.

func (*MockhttpClient) PatchWithHeaders added in v1.20.0

func (m *MockhttpClient) PatchWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PatchWithHeaders mocks base method.

func (*MockhttpClient) Post added in v1.20.0

func (m *MockhttpClient) Post(ctx context.Context, path string, queryParams map[string]any, body []byte) (*http.Response, error)

Post mocks base method.

func (*MockhttpClient) PostWithHeaders added in v1.20.0

func (m *MockhttpClient) PostWithHeaders(ctx context.Context, path string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PostWithHeaders mocks base method.

func (*MockhttpClient) Put added in v1.20.0

func (m *MockhttpClient) Put(ctx context.Context, api string, queryParams map[string]any, body []byte) (*http.Response, error)

Put mocks base method.

func (*MockhttpClient) PutWithHeaders added in v1.20.0

func (m *MockhttpClient) PutWithHeaders(ctx context.Context, api string, queryParams map[string]any, body []byte, headers map[string]string) (*http.Response, error)

PutWithHeaders mocks base method.

type MockhttpClientMockRecorder added in v1.20.0

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

MockhttpClientMockRecorder is the mock recorder for MockhttpClient.

func (*MockhttpClientMockRecorder) Delete added in v1.20.0

func (mr *MockhttpClientMockRecorder) Delete(ctx, api, body any) *gomock.Call

Delete indicates an expected call of Delete.

func (*MockhttpClientMockRecorder) DeleteWithHeaders added in v1.20.0

func (mr *MockhttpClientMockRecorder) DeleteWithHeaders(ctx, api, body, headers any) *gomock.Call

DeleteWithHeaders indicates an expected call of DeleteWithHeaders.

func (*MockhttpClientMockRecorder) Get added in v1.20.0

func (mr *MockhttpClientMockRecorder) Get(ctx, api, queryParams any) *gomock.Call

Get indicates an expected call of Get.

func (*MockhttpClientMockRecorder) GetWithHeaders added in v1.20.0

func (mr *MockhttpClientMockRecorder) GetWithHeaders(ctx, path, queryParams, headers any) *gomock.Call

GetWithHeaders indicates an expected call of GetWithHeaders.

func (*MockhttpClientMockRecorder) Patch added in v1.20.0

func (mr *MockhttpClientMockRecorder) Patch(ctx, api, queryParams, body any) *gomock.Call

Patch indicates an expected call of Patch.

func (*MockhttpClientMockRecorder) PatchWithHeaders added in v1.20.0

func (mr *MockhttpClientMockRecorder) PatchWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call

PatchWithHeaders indicates an expected call of PatchWithHeaders.

func (*MockhttpClientMockRecorder) Post added in v1.20.0

func (mr *MockhttpClientMockRecorder) Post(ctx, path, queryParams, body any) *gomock.Call

Post indicates an expected call of Post.

func (*MockhttpClientMockRecorder) PostWithHeaders added in v1.20.0

func (mr *MockhttpClientMockRecorder) PostWithHeaders(ctx, path, queryParams, body, headers any) *gomock.Call

PostWithHeaders indicates an expected call of PostWithHeaders.

func (*MockhttpClientMockRecorder) Put added in v1.20.0

func (mr *MockhttpClientMockRecorder) Put(ctx, api, queryParams, body any) *gomock.Call

Put indicates an expected call of Put.

func (*MockhttpClientMockRecorder) PutWithHeaders added in v1.20.0

func (mr *MockhttpClientMockRecorder) PutWithHeaders(ctx, api, queryParams, body, headers any) *gomock.Call

PutWithHeaders indicates an expected call of PutWithHeaders.

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

type RetryConfig added in v1.13.0

type RetryConfig struct {
	MaxRetries int
}

func (*RetryConfig) AddOption added in v1.13.0

func (r *RetryConfig) AddOption(h HTTP) HTTP

Jump to

Keyboard shortcuts

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