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"
"strings"
"time"
@@ -18,10 +17,7 @@ import (
// ObservationSource polls an Open-Meteo endpoint and emits one RAW Observation Event.
type ObservationSource struct {
name string
url string
userAgent string
client *http.Client
http *common.HTTPSource
}
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
@@ -29,20 +25,15 @@ func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
// We require params.user_agent for uniformity across sources (even though Open-Meteo
// itself does not strictly require a special User-Agent).
c, err := common.RequireHTTPSourceConfig(driver, cfg)
hs, err := common.NewHTTPSource(driver, cfg, "application/json")
if err != nil {
return nil, 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") }
@@ -53,10 +44,10 @@ 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) == "" {
// Extremely defensive fallback: keep the envelope valid no matter what.
eventID = fmt.Sprintf("openmeteo:current:%s:%s", s.name, time.Now().UTC().Format(time.RFC3339Nano))
eventID = fmt.Sprintf("openmeteo:current:%s:%s", s.http.Name, time.Now().UTC().Format(time.RFC3339Nano))
}
var effectiveAt *time.Time
@@ -67,7 +58,7 @@ func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
return common.SingleRawEvent(
s.Kind(),
s.name,
s.http.Name,
standards.SchemaRawOpenMeteoCurrentV1,
eventID,
effectiveAt,
@@ -91,15 +82,13 @@ type openMeteoMeta struct {
}
func (s *ObservationSource) fetchRaw(ctx context.Context) (json.RawMessage, openMeteoMeta, 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, openMeteoMeta{}, fmt.Errorf("openmeteo_observation %q: %w", s.name, err)
return nil, openMeteoMeta{}, err
}
raw := json.RawMessage(b)
var meta openMeteoMeta
if err := json.Unmarshal(b, &meta); err != nil {
if err := json.Unmarshal(raw, &meta); err != nil {
// If metadata decode fails, still return raw; envelope will fall back to computed ID without EffectiveAt.
return raw, openMeteoMeta{}, nil
}