v0.x: add reusable HTTP source spine; fix routing; upstream HTTP transport helper

- fix dispatch route compilation so empty Kinds matches all (nil), not none
- introduce internal/sources/common/HTTPSource to centralize HTTP polling boilerplate:
  - standard cfg parsing (url + user_agent)
  - default HTTP client + Accept/User-Agent headers
  - consistent error wrapping
- refactor observation sources (nws/openmeteo/openweather) to use HTTPSource
- upstream generic HTTP fetch/limits/timeout helper from weatherfeeder to feedkit:
  - move internal/sources/common/http.go -> feedkit/transport/http.go
  - keep behavior: status checks, max-body limit, default timeout
This commit is contained in:
2026-01-15 19:11:58 -06:00
parent 1790218d38
commit d9474b5a5b
4 changed files with 91 additions and 116 deletions

View File

@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
@@ -23,40 +22,32 @@ import (
// system in the response body. To keep normalization deterministic, this driver *requires*
// `units=metric`. If absent (or non-metric), the driver returns an error.
type ObservationSource struct {
name string
url string
userAgent string
client *http.Client
http *common.HTTPSource
}
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
const driver = "openweather_observation"
c, err := common.RequireHTTPSourceConfig(driver, cfg)
hs, err := common.NewHTTPSource(driver, cfg, "application/json")
if err != nil {
return nil, err
}
if err := requireMetricUnits(c.URL); err != nil {
return nil, fmt.Errorf("openweather_observation %q: %w", c.Name, err)
if err := requireMetricUnits(hs.URL); err != nil {
return nil, fmt.Errorf("%s %q: %w", hs.Driver, hs.Name, err)
}
return &ObservationSource{
name: c.Name,
url: c.URL,
userAgent: c.UserAgent,
client: common.NewHTTPClient(common.DefaultHTTPTimeout),
}, nil
return &ObservationSource{http: hs}, nil
}
func (s *ObservationSource) Name() string { return s.name }
func (s *ObservationSource) Name() string { return s.http.Name }
func (s *ObservationSource) Kind() event.Kind { return event.Kind("observation") }
func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
// Re-check policy defensively (in case the URL is mutated after construction).
if err := requireMetricUnits(s.url); err != nil {
return nil, fmt.Errorf("openweather_observation %q: %w", s.name, err)
if err := requireMetricUnits(s.http.URL); err != nil {
return nil, fmt.Errorf("%s %q: %w", s.http.Driver, s.http.Name, err)
}
raw, meta, err := s.fetchRaw(ctx)
@@ -64,9 +55,9 @@ func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
return nil, err
}
eventID := buildEventID(s.name, meta)
eventID := buildEventID(s.http.Name, meta)
if strings.TrimSpace(eventID) == "" {
eventID = fmt.Sprintf("openweather:current:%s:%s", s.name, time.Now().UTC().Format(time.RFC3339Nano))
eventID = fmt.Sprintf("openweather:current:%s:%s", s.http.Name, time.Now().UTC().Format(time.RFC3339Nano))
}
var effectiveAt *time.Time
@@ -77,7 +68,7 @@ func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
return common.SingleRawEvent(
s.Kind(),
s.name,
s.http.Name,
standards.SchemaRawOpenWeatherCurrentV1,
eventID,
effectiveAt,
@@ -102,15 +93,13 @@ type openWeatherMeta struct {
}
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, openWeatherMeta, error) {
b, err := common.FetchBody(ctx, s.client, s.url, s.userAgent, "application/json")
raw, err := s.http.FetchJSON(ctx)
if err != nil {
return nil, openWeatherMeta{}, fmt.Errorf("openweather_observation %q: %w", s.name, err)
return nil, openWeatherMeta{}, err
}
raw := json.RawMessage(b)
var meta openWeatherMeta
if err := json.Unmarshal(b, &meta); err != nil {
if err := json.Unmarshal(raw, &meta); err != nil {
return raw, openWeatherMeta{}, nil
}