Moved HTTP polling helpers upstream into feedkit, and updated to feedkit v0.8.0
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -1,104 +0,0 @@
|
||||
// FILE: ./internal/sources/common/config.go
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
)
|
||||
|
||||
// This file centralizes small, boring config-validation patterns shared across
|
||||
// weatherfeeder source drivers.
|
||||
//
|
||||
// Goal: keep driver constructors (New*Source) easy to read and consistent, while
|
||||
// keeping driver-specific options in cfg.Params (feedkit remains domain-agnostic).
|
||||
|
||||
// HTTPSourceConfig is the standard "HTTP-polling source" config shape used across drivers.
|
||||
type HTTPSourceConfig struct {
|
||||
Name string
|
||||
URL string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
// RequireHTTPSourceConfig enforces weatherfeeder's standard HTTP source config:
|
||||
//
|
||||
// - cfg.Name must be present
|
||||
// - cfg.Params must be present
|
||||
// - params.url must be present (accepts "url" or "URL")
|
||||
// - params.user_agent must be present (accepts "user_agent" or "userAgent")
|
||||
//
|
||||
// We intentionally require a User-Agent for *all* sources, even when upstreams
|
||||
// do not strictly require one. This keeps config uniform across providers.
|
||||
func RequireHTTPSourceConfig(driver string, cfg config.SourceConfig) (HTTPSourceConfig, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return HTTPSourceConfig{}, fmt.Errorf("%s: name is required", driver)
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return HTTPSourceConfig{}, fmt.Errorf("%s %q: params are required (need params.url and params.user_agent)", driver, cfg.Name)
|
||||
}
|
||||
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return HTTPSourceConfig{}, fmt.Errorf("%s %q: params.url is required", driver, cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return HTTPSourceConfig{}, fmt.Errorf("%s %q: params.user_agent is required", driver, cfg.Name)
|
||||
}
|
||||
|
||||
return HTTPSourceConfig{
|
||||
Name: cfg.Name,
|
||||
URL: url,
|
||||
UserAgent: ua,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// --- The helpers below remain useful for future drivers; they are not required
|
||||
// --- by the observation sources after adopting RequireHTTPSourceConfig.
|
||||
|
||||
// RequireName ensures cfg.Name is present and non-whitespace.
|
||||
func RequireName(driver string, cfg config.SourceConfig) error {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return fmt.Errorf("%s: name is required", driver)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RequireParams ensures cfg.Params is non-nil. The "want" string should be a short
|
||||
// description of required keys, e.g. "need params.url and params.user_agent".
|
||||
func RequireParams(driver string, cfg config.SourceConfig, want string) error {
|
||||
if cfg.Params == nil {
|
||||
return fmt.Errorf("%s %q: params are required (%s)", driver, cfg.Name, want)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RequireURL returns the configured URL for a source.
|
||||
// Canonical key is "url"; we also accept "URL" as a convenience.
|
||||
func RequireURL(driver string, cfg config.SourceConfig) (string, error) {
|
||||
if cfg.Params == nil {
|
||||
return "", fmt.Errorf("%s %q: params are required (need params.url)", driver, cfg.Name)
|
||||
}
|
||||
|
||||
u, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%s %q: params.url is required", driver, cfg.Name)
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// RequireUserAgent returns the configured User-Agent for a source.
|
||||
// Canonical key is "user_agent"; we also accept "userAgent" as a convenience.
|
||||
func RequireUserAgent(driver string, cfg config.SourceConfig) (string, error) {
|
||||
if cfg.Params == nil {
|
||||
return "", fmt.Errorf("%s %q: params are required (need params.user_agent)", driver, cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%s %q: params.user_agent is required", driver, cfg.Name)
|
||||
}
|
||||
return ua, nil
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
// FILE: ./internal/sources/common/http_source.go
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/transport"
|
||||
)
|
||||
|
||||
// HTTPSource is a tiny, reusable "HTTP polling spine" for weatherfeeder sources.
|
||||
//
|
||||
// It centralizes the boring parts:
|
||||
// - standard config shape (url + user_agent) via RequireHTTPSourceConfig
|
||||
// - a default http.Client with timeout
|
||||
// - FetchBody / headers / max-body safety limit
|
||||
// - consistent error wrapping (driver + source name)
|
||||
//
|
||||
// Individual drivers remain responsible for:
|
||||
// - decoding minimal metadata (for Event.ID / EffectiveAt)
|
||||
// - constructing the event envelope (kind/schema/payload)
|
||||
type HTTPSource struct {
|
||||
Driver string
|
||||
Name string
|
||||
URL string
|
||||
UserAgent string
|
||||
Accept string
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
// NewHTTPSource builds an HTTPSource using weatherfeeder's standard HTTP source
|
||||
// config (params.url + params.user_agent) and a default HTTP client.
|
||||
func NewHTTPSource(driver string, cfg config.SourceConfig, accept string) (*HTTPSource, error) {
|
||||
c, err := RequireHTTPSourceConfig(driver, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &HTTPSource{
|
||||
Driver: driver,
|
||||
Name: c.Name,
|
||||
URL: c.URL,
|
||||
UserAgent: c.UserAgent,
|
||||
Accept: accept,
|
||||
Client: transport.NewHTTPClient(transport.DefaultHTTPTimeout),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FetchBytes fetches the URL and returns the raw response body bytes.
|
||||
func (s *HTTPSource) FetchBytes(ctx context.Context) ([]byte, error) {
|
||||
client := s.Client
|
||||
if client == nil {
|
||||
// Defensive: allow tests or callers to nil out Client; keep behavior sane.
|
||||
client = transport.NewHTTPClient(transport.DefaultHTTPTimeout)
|
||||
}
|
||||
|
||||
b, err := transport.FetchBody(ctx, client, s.URL, s.UserAgent, s.Accept)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s %q: %w", s.Driver, s.Name, err)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// FetchJSON fetches the URL and returns the raw body as json.RawMessage.
|
||||
// json.Unmarshal accepts json.RawMessage directly, so callers can decode minimal
|
||||
// metadata without keeping both []byte and RawMessage in their own structs.
|
||||
func (s *HTTPSource) FetchJSON(ctx context.Context) (json.RawMessage, error) {
|
||||
b, err := s.FetchBytes(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.RawMessage(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user