eia-api-go/pkg/eia/eia.go

95 lines
2.8 KiB
Go
Raw Normal View History

2024-11-16 19:02:46 +00:00
package eia
import (
"context"
"net/url"
"slices"
"time"
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
"github.com/rs/zerolog"
eiaapi "gitea.libretechconsulting.com/50W/eia-api-go/api"
)
const (
defaultBaseURL = "https://api.eia.gov"
defaultPingTimeout = 10 * time.Second
defaultLogLevel = zerolog.DebugLevel
)
// Simple wrapper around generated EIA API client
// that embeds a client with a bearer auth interceptor
// and optional logging middleware
//
// Performs a basic availability check against the API
// when creating a new client, and exposes a Ping() method
// for health / readiness probes
type Client struct {
ctx context.Context
apiKey string
healthCheckTimeout time.Duration
2024-11-19 13:42:18 +00:00
*eiaapi.ClientWithResponses
2024-11-16 19:02:46 +00:00
}
type ClientOpts struct {
Context context.Context // Base context for requests
APIKey string // API Key [EIA Opendata API v2](https://www.eia.gov/opendata/index.php)
Logger *zerolog.Logger // Optional logger, if set is injected into logging middleware
LogLevel *zerolog.Level // Optional log level, default is zerolog.DebugLevel
BaseURL *url.URL // Optional, default is https://api.eia.gov
HealthCheckTimeout *time.Duration // Timeout for Ping() function, default is 10s
}
func NewClient(opts *ClientOpts) (*Client, error) {
baseURL := defaultBaseURL
if opts.BaseURL != nil {
baseURL = opts.BaseURL.String()
}
hcTimeout := defaultPingTimeout
if opts.HealthCheckTimeout != nil && *opts.HealthCheckTimeout > time.Duration(0) {
hcTimeout = *opts.HealthCheckTimeout
}
middlewares := make([]eiaapi.ClientOption, 0, 2)
// Injects Authorization: Bearer <APIKey> header into
// outbound API calls
2024-11-19 13:42:18 +00:00
// basicAuth, err := securityprovider.NewSecurityProviderBearerToken(opts.APIKey)
// if err != nil {
// return nil, err
// }
// middlewares = append(middlewares, eiaapi.WithRequestEditorFn(basicAuth.Intercept))
// Injects API key as query parameter: ?api_key=<apiKey>
paramAuth, err := securityprovider.NewSecurityProviderApiKey("query", "api_key", opts.APIKey)
2024-11-16 19:02:46 +00:00
if err != nil {
return nil, err
}
2024-11-19 13:42:18 +00:00
middlewares = append(middlewares, eiaapi.WithRequestEditorFn(paramAuth.Intercept))
2024-11-16 19:02:46 +00:00
// Logging middleware, if logger is given
if opts.Logger != nil {
logLevel := defaultLogLevel
if opts.LogLevel != nil {
logLevel = *opts.LogLevel
}
middlewares = append(middlewares,
eiaapi.WithRequestEditorFn(newLoggingMiddleware(opts.Logger, logLevel)))
}
2024-11-19 13:42:18 +00:00
client, err := eiaapi.NewClientWithResponses(baseURL, slices.Clip(middlewares)...)
2024-11-16 19:02:46 +00:00
if err != nil {
return nil, err
}
return &Client{
2024-11-19 13:42:18 +00:00
apiKey: opts.APIKey,
ctx: opts.Context,
healthCheckTimeout: hcTimeout,
ClientWithResponses: client,
2024-11-16 19:02:46 +00:00
}, nil
}