api

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package api provides an HTTP API-based data enrichment adapter with rate limiting, health checks, and circuit breaker support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAPIEnricher

func NewAPIEnricher(config Config) (port.EnrichmentPort, error)

NewAPIEnricher creates a new API enrichment adapter with error handling Validates configuration and initializes rate limiter and error coordinator

Types

type APIEnricher

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

APIEnricher implements the EnrichmentPort interface using HTTP API as backend Supports single and batch enrichment with rate limiting and error handling

func (*APIEnricher) BatchEnrich

func (ae *APIEnricher) BatchEnrich(ctx context.Context, keys []string, data []map[string]any) ([]map[string]any, error)

BatchEnrich fetches enrichment data for multiple keys Processes each key respecting rate limiting

func (*APIEnricher) Enrich

func (ae *APIEnricher) Enrich(ctx context.Context, key string, data map[string]any) (map[string]any, error)

Enrich fetches enrichment data from API for a single key Uses coordinator for automatic retry, circuit breaking, and error handling Respects rate limiting and operation timeout

func (*APIEnricher) IsAvailable

func (ae *APIEnricher) IsAvailable(ctx context.Context) bool

IsAvailable checks if API is reachable and healthy

type Config

type Config struct {
	Client            *http.Client
	BaseURL           string
	APIKey            string
	OperationTimeout  time.Duration
	HealthCheckPath   string
	RequestsPerSecond float64
}

Config holds API enricher configuration

func NewConfig

func NewConfig(baseURL, apiKey string, client *http.Client) Config

NewConfig creates a new Config with sensible defaults

func (Config) Validate

func (c Config) Validate() error

Validate checks if configuration is valid Returns error if any required field is missing or invalid

type HealthChecker

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

HealthChecker provides health check operations for API

func NewHealthChecker

func NewHealthChecker(client *http.Client, timeout time.Duration, baseURL, healthCheckPath string) *HealthChecker

NewHealthChecker creates a new health checker

func (*HealthChecker) Check

func (hc *HealthChecker) Check(ctx context.Context) error

Check performs a health check on API endpoint Returns error if connection fails or times out

func (*HealthChecker) IsHealthy

func (hc *HealthChecker) IsHealthy(ctx context.Context) bool

IsHealthy returns bool indicating if API is healthy

type RateLimiter

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

RateLimiter controls the rate of API requests Uses token bucket algorithm for smooth rate limiting

func NewRateLimiter

func NewRateLimiter(rps float64) *RateLimiter

NewRateLimiter creates a new rate limiter with given RPS burst is set equal to RPS for smooth bursts

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow() bool

Allow returns true if a token is immediately available

func (*RateLimiter) Wait

func (rl *RateLimiter) Wait(ctx context.Context) error

Wait blocks until a token is available for the request Returns error if context is cancelled

Jump to

Keyboard shortcuts

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