handlers

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIDataHandler

type APIDataHandler[K providertypes.ResponseKey, V providertypes.ResponseValue] interface {
	// CreateURL is used to create the URL to be sent to the http client. The function
	// should utilize the IDs passed in as references to the data that needs to be fetched.
	CreateURL(ids []K) (string, error)

	// ParseResponse is used to parse the response from the client. The response should be
	// parsed into a map of IDs to results. If any IDs are not resolved, they should
	// be returned in the unresolved map. The timestamp associated with the result should
	// reflect either the time the data was fetched or the time the API last updated the data.
	ParseResponse(ids []K, response *http.Response) providertypes.GetResponse[K, V]
}

APIDataHandler defines an interface that must be implemented by all providers that want to fetch data from an API using HTTP requests. This interface is meant to be paired with the APIQueryHandler. The APIQueryHandler will use the APIDataHandler to create the URL to be sent to the HTTP client and parse the response from the client.

type APIFetcher

type APIFetcher[K providertypes.ResponseKey, V providertypes.ResponseValue] interface {
	// Fetch fetches data from the API for the given IDs. The response is returned as a map of IDs to
	// their respective responses. The request should respect the context timeout and cancel the request
	// if the context is cancelled.
	Fetch(
		ctx context.Context,
		ids []K,
	) providertypes.GetResponse[K, V]
}

APIFetcher is an interface that encapsulates fetching data from a provider. This interface is meant to abstract over the various processes of interacting w/ GRPC, JSON-RPC, REST, etc. APIs.

type APIQueryHandler

type APIQueryHandler[K providertypes.ResponseKey, V providertypes.ResponseValue] interface {
	Query(
		ctx context.Context,
		ids []K,
		responseCh chan<- providertypes.GetResponse[K, V],
	)
}

APIQueryHandler is an interface that encapsulates querying a data provider for info. The handler must respect the context timeout and cancel the request if the context is cancelled. All responses must be sent to the response channel. These are processed asynchronously by the provider.

func NewAPIQueryHandler

func NewAPIQueryHandler[K providertypes.ResponseKey, V providertypes.ResponseValue](
	logger *zap.Logger,
	cfg config.APIConfig,
	requestHandler RequestHandler,
	apiHandler APIDataHandler[K, V],
	metrics metrics.APIMetrics,
) (APIQueryHandler[K, V], error)

NewAPIQueryHandler creates a new APIQueryHandler. It manages querying the data provider by using the APIDataHandler and RequestHandler.

func NewAPIQueryHandlerWithFetcher

func NewAPIQueryHandlerWithFetcher[K providertypes.ResponseKey, V providertypes.ResponseValue](
	logger *zap.Logger,
	cfg config.APIConfig,
	fetcher APIFetcher[K, V],
	metrics metrics.APIMetrics,
) (APIQueryHandler[K, V], error)

NewAPIQueryHandlerWithFetcher creates a new APIQueryHandler with a custom api fetcher.

type APIQueryHandlerImpl

type APIQueryHandlerImpl[K providertypes.ResponseKey, V providertypes.ResponseValue] struct {
	// contains filtered or unexported fields
}

APIQueryHandlerImpl is the default API implementation of the QueryHandler interface. This is used to query using http requests. It manages querying the data provider by using the APIDataHandler and RequestHandler. All responses are sent to the response channel. In the case where the APIQueryHandler is atomic, the handler will make a single request for all IDs. If the APIQueryHandler is not atomic, the handler will make a request for each ID in a separate go routine.

func (*APIQueryHandlerImpl[K, V]) Query

func (h *APIQueryHandlerImpl[K, V]) Query(
	ctx context.Context,
	ids []K,
	responseCh chan<- providertypes.GetResponse[K, V],
)

Query is used to query the API data provider for the given IDs. This method blocks until all responses have been sent to the response channel. Query will only make N concurrent requests at a time, where N is the capacity of the response channel.

type Option

type Option func(*RequestHandlerImpl)

Option is a function that is used to configure a RequestHandler.

func WithHTTPHeaders

func WithHTTPHeaders(headers map[string]string) Option

WithHTTPHeaders is an option that is used to set the HTTP headers used to make requests.

func WithHTTPMethod

func WithHTTPMethod(method string) Option

WithHTTPMethod is an option that is used to set the HTTP method used to make requests.

type RequestHandler

type RequestHandler interface {
	// Do is used to send a request with the given URL to the data provider.
	Do(ctx context.Context, url string) (*http.Response, error)

	// Type defines the type of the RequestHandler based on the type of
	// HTTP requests it makes  - GET, POST, etc.
	Type() string
}

RequestHandler is an interface that encapsulates sending an HTTP request to a data provider.

func NewRequestHandlerImpl

func NewRequestHandlerImpl(client *http.Client, opts ...Option) (RequestHandler, error)

NewRequestHandlerImpl creates a new RequestHandlerImpl. It manages making HTTP requests.

type RequestHandlerImpl

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

RequestHandlerImpl is the default implementation of the RequestHandler interface.

func (*RequestHandlerImpl) Do

Do is used to send a request with the given URL to the data provider. It first wraps the request with the given context before sending it to the data provider.

func (*RequestHandlerImpl) Type

func (r *RequestHandlerImpl) Type() string

Type returns the HTTP method used to send requests.

type RestAPIFetcher

type RestAPIFetcher[K providertypes.ResponseKey, V providertypes.ResponseValue] struct {
	// contains filtered or unexported fields
}

RestAPIFetcher handles the logic of fetching prices from a REST API. This implementation depends on an APIDataHandler to handle the creation of URLs / parsing the API response.

func NewRestAPIFetcher

func NewRestAPIFetcher[K providertypes.ResponseKey, V providertypes.ResponseValue](
	requestHandler RequestHandler,
	apiDataHandler APIDataHandler[K, V],
	metrics metrics.APIMetrics,
	config config.APIConfig,
	logger *zap.Logger,
) (*RestAPIFetcher[K, V], error)

NewRestAPIFetcher creates a new RestAPIFetcher.

func (*RestAPIFetcher[K, V]) Fetch

func (pf *RestAPIFetcher[K, V]) Fetch(
	ctx context.Context,
	ids []K,
) providertypes.GetResponse[K, V]

Fetch is used to fetch the corresponding IDs from the API. This method blocks until the response is received from the API, parsed, and returned.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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