service

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package service provides generic HTTP client that can be used to call remote services.The service package also provides support for custom headers, authentication, caching settings, and surge protection options.The package defines three main interfaces: HTTP, SOAP, and Cacher, which can be used to implement various service clients.

Index

Constants

View Source
const (
	JSON responseType = iota
	XML
	TEXT
	HTML
	RetryFrequency          = 5
	UnixTimeStampMultiplier = 1000
	ErrToken                = errors.Error("token could not be obtained")
)

Variables

This section is empty.

Functions

func NewHTTPServiceWithOptions

func NewHTTPServiceWithOptions(resourceAddr string, logger log.Logger, options *Options) *httpService

NewHTTPServiceWithOptions creates a http client based on the options configured

func NewSOAPClient

func NewSOAPClient(resourceURL string, logger log.Logger, user, pass string) *soapService

NewSOAPClient sets up an HTTP client with specific transport and timeout settings, configures surge protection, and prepares the SOAP service for use.

Types

type Auth

type Auth struct {
	UserName string // if token is not sent then the username and password can be sent and the token will be generated by the framework
	Password string
	*OAuthOption
}

Auth stores the information related to authentication. One can either use basic auth or OAuth

type Cache

type Cache struct {
	Cacher
	TTL          time.Duration
	KeyGenerator KeyGenerator
}

Cache provides the options needed for caching of HTTPService responses

type Cacher

type Cacher interface {
	Get(key string) ([]byte, error)
	Set(key string, content []byte, duration time.Duration) error
	Delete(key string) error
}

Cacher is an interface for caching data.

type ErrServiceDown

type ErrServiceDown struct {
	URL string
}

func (ErrServiceDown) Error

func (e ErrServiceDown) Error() string

type FailedRequest

type FailedRequest struct {
	URL string
	Err error
}

func (FailedRequest) Error

func (f FailedRequest) Error() string

Error Return's error message about a failed service request, specifically the URL of the request and the associated error.

type HTTP

type HTTP interface {
	Get(ctx context.Context, api string, params map[string]interface{}) (*Response, error)
	Post(ctx context.Context, api string, params map[string]interface{}, body []byte) (*Response, error)
	Put(ctx context.Context, api string, params map[string]interface{}, body []byte) (*Response, error)
	Delete(ctx context.Context, api string, body []byte) (*Response, error)
	Patch(ctx context.Context, api string, params map[string]interface{}, body []byte) (*Response, error)

	GetWithHeaders(ctx context.Context, api string, params map[string]interface{}, headers map[string]string) (*Response, error)
	PostWithHeaders(ctx context.Context, api string, params map[string]interface{}, body []byte, headers map[string]string) (*Response, error)
	PutWithHeaders(ctx context.Context, api string, params map[string]interface{}, body []byte, headers map[string]string) (*Response, error)
	DeleteWithHeaders(ctx context.Context, api string, body []byte, headers map[string]string) (*Response, error)
	PatchWithHeaders(ctx context.Context, api string, params map[string]interface{}, body []byte, headers map[string]string) (*Response, error)

	Bind(resp []byte, i interface{}) error
	BindStrict(resp []byte, i interface{}) error

	// PropagateHeaders is used to specify the header keys that needs to be propagated through context
	// By default the headers: True-Client-IP, X-Authenticated-UserId,
	// are propagated.
	PropagateHeaders(headers ...string)

	// SetSurgeProtectorOptions sets the configuration for the surge protector, the default configuration is :-
	// surge protection is enabled, the heartbeat URL is /.well-known/heartbeat, retry frequency is 5 seconds.
	// The surge protector ensures that the HTTP client does not bombard a downstream service that is down,
	// it returns a 500 right away, until the service is back up again. It figures out if the service
	// is back up again by asynchronously making request to the heartbeat API until its up again
	SetSurgeProtectorOptions(isEnabled bool, customHeartbeatURL string, retryFrequencySeconds int)
}

HTTP is an interface for making HTTP requests and handling responses.

type KeyGenerator

type KeyGenerator func(url string, params map[string]interface{}, headers map[string]string) string

KeyGenerator provides ability to the user that can use custom or own logic to Generate the key for HTTPCached

type OAuthOption

type OAuthOption struct {
	ClientID                  string
	ClientSecret              string
	KeyProviderURL            string
	Scope                     string
	Audience                  string
	MaxSleep                  int
	WaitForTokenGen           bool
	TimeBeforeExpiryToRefresh int // Time(in seconds) before token expiry to get the fresh token. Default is 5seconds
}

OAuthOption represents configuration options for OAuth authentication.

type Options

type Options struct {
	Headers           map[string]string // this can be used to pass service level headers.
	NumOfRetries      int
	SkipQParamLogging bool
	*Auth
	*Cache
	*SurgeProtectorOption
}

Options allows the user set all the options needs for http service like auth, service level headers, caching and surge protection

type RequestCanceled

type RequestCanceled struct{}

func (RequestCanceled) Error

func (r RequestCanceled) Error() string

type Response

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

Response represents an HTTP response with body, status code, and headers.

func (*Response) GetHeader

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

GetHeader fetches the value of a specified HTTP header,

type SOAP

type SOAP interface {
	Call(ctx context.Context, action string, body []byte) (*Response, error)
	CallWithHeaders(ctx context.Context, action string, body []byte, headers map[string]string) (*Response, error)
	Bind(resp []byte, i interface{}) error
	BindStrict(resp []byte, i interface{}) error
}

SOAP is an interface for making SOAP requests and handling responses.

type SurgeProtectorOption

type SurgeProtectorOption struct {
	HeartbeatURL   string
	RetryFrequency int // indicates the time in seconds
	Disable        bool
}

Jump to

Keyboard shortcuts

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