sources: standardize HTTP source config + factor raw-event boilerplate
- 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
This commit is contained in:
@@ -32,30 +32,18 @@ type ObservationSource struct {
|
||||
}
|
||||
|
||||
func NewObservationSource(cfg config.SourceConfig) (*ObservationSource, error) {
|
||||
if strings.TrimSpace(cfg.Name) == "" {
|
||||
return nil, fmt.Errorf("nws_observation: name is required")
|
||||
}
|
||||
if cfg.Params == nil {
|
||||
return nil, fmt.Errorf("nws_observation %q: params are required (need params.url and params.user_agent)", cfg.Name)
|
||||
}
|
||||
const driver = "nws_observation"
|
||||
|
||||
url, ok := cfg.ParamString("url", "URL")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_observation %q: params.url is required", cfg.Name)
|
||||
}
|
||||
|
||||
ua, ok := cfg.ParamString("user_agent", "userAgent")
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nws_observation %q: params.user_agent is required", cfg.Name)
|
||||
c, err := common.RequireHTTPSourceConfig(driver, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ObservationSource{
|
||||
name: cfg.Name,
|
||||
url: url,
|
||||
userAgent: ua,
|
||||
client: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
name: c.Name,
|
||||
url: c.URL,
|
||||
userAgent: c.UserAgent,
|
||||
client: common.NewHTTPClient(common.DefaultHTTPTimeout),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -95,25 +83,14 @@ func (s *ObservationSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
effectiveAt = &t
|
||||
}
|
||||
|
||||
e := event.Event{
|
||||
ID: eventID,
|
||||
Kind: s.Kind(),
|
||||
Source: s.name,
|
||||
EmittedAt: time.Now().UTC(),
|
||||
EffectiveAt: effectiveAt,
|
||||
|
||||
// RAW schema (normalizer matches on this).
|
||||
Schema: standards.SchemaRawNWSObservationV1,
|
||||
|
||||
// Raw JSON; normalizer will decode and map to canonical model.WeatherObservation.
|
||||
Payload: raw,
|
||||
}
|
||||
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []event.Event{e}, nil
|
||||
return common.SingleRawEvent(
|
||||
s.Kind(),
|
||||
s.name,
|
||||
standards.SchemaRawNWSObservationV1,
|
||||
eventID,
|
||||
effectiveAt,
|
||||
raw,
|
||||
)
|
||||
}
|
||||
|
||||
// ---- RAW fetch + minimal metadata decode ----
|
||||
|
||||
Reference in New Issue
Block a user