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
- func NewHTTPServiceWithOptions(resourceAddr string, logger log.Logger, options *Options) *httpService
- func NewSOAPClient(resourceURL string, logger log.Logger, user, pass string) *soapService
- type Auth
- type Cache
- type Cacher
- type ErrServiceDown
- type FailedRequest
- type HTTP
- type KeyGenerator
- type OAuthOption
- type Options
- type RequestCanceled
- type Response
- type SOAP
- type SurgeProtectorOption
Constants ¶
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 ¶
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 ¶
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 ¶
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 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.