- Require params.user_agent for all HTTP sources (uniform config across providers) - Add common.RequireHTTPSourceConfig() to validate name/url/user_agent in one call - Add common.NewHTTPClient() with DefaultHTTPTimeout for consistent client setup - Add common.SingleRawEvent() to centralize event envelope construction + validation - Refactor NWS/Open-Meteo/OpenWeather observation sources to use new helpers
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
// FILE: ./internal/sources/common/event.go
|
|
package common
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/ejr/feedkit/event"
|
|
)
|
|
|
|
// SingleRawEvent constructs, validates, and returns a slice containing exactly one event.
|
|
//
|
|
// This removes the repetitive "event envelope ceremony" from individual sources.
|
|
// Sources remain responsible for:
|
|
// - fetching bytes (raw payload)
|
|
// - choosing Schema (raw schema identifier)
|
|
// - computing a stable Event.ID and (optional) EffectiveAt
|
|
func SingleRawEvent(kind event.Kind, sourceName string, schema string, id string, effectiveAt *time.Time, payload any) ([]event.Event, error) {
|
|
e := event.Event{
|
|
ID: id,
|
|
Kind: kind,
|
|
Source: sourceName,
|
|
EmittedAt: time.Now().UTC(),
|
|
EffectiveAt: effectiveAt,
|
|
|
|
// RAW schema (normalizer matches on this).
|
|
Schema: schema,
|
|
|
|
// Raw payload (usually json.RawMessage). Normalizer will decode and map to canonical model.
|
|
Payload: payload,
|
|
}
|
|
|
|
if err := e.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return []event.Event{e}, nil
|
|
}
|