package wunderground import ( "context" "errors" "net/url" "github.com/go-resty/resty/v2" "github.com/rs/zerolog/log" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "k8s.io/utils/ptr" "gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel" "gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/weather" ) func (wu *WUProvider) ProxyReq(ctx context.Context, update *weather.WeatherUpdate) error { tracer := otel.GetTracer(ctx, "wuProvider", "proxyReq") ctx, span := tracer.Start(ctx, "proxyToWunderground") defer span.End() if update.StationConfig.WundergroundID == "" { err := errors.New("no wunderground id set in update") span.RecordError(err) return err } else if update.StationConfig.WundergroundPassword == "" { err := errors.New("no wunderground id set in update") span.RecordError(err) return err } params := updateToWuParams(update) resp, err := resty.New().R(). SetContext(ctx). SetQueryParamsFromValues(*params). Get(wuURL) if err != nil { span.SetStatus(codes.Error, err.Error()) span.SetAttributes( attribute.String("query", resp.Request.QueryParam.Encode()), attribute.String("body", string(resp.Body())), ) span.RecordError(err) log.Err(err). Int("statusCode", resp.StatusCode()). Any("query", resp.Request.PathParams). Msg("wunderground proxy failed") } span.SetAttributes( attribute.Int("statusCode", resp.StatusCode()), ) return err } func updateToWuParams(u *weather.WeatherUpdate) *url.Values { params := &url.Values{} weather.SetURLVal(params, "ID", &u.StationConfig.WundergroundID) weather.SetURLVal(params, "PASSWORD", &u.StationConfig.WundergroundPassword) params.Set("action", "updateraw") params.Set("dateutc", "now") weather.SetURLVal(params, "UV", u.UV) weather.SetURLVal(params, "baromin", u.BaromRelativeIn) weather.SetURLVal(params, "dailyrainin", u.DailyRainIn) weather.SetURLVal(params, "dewptf", u.DewPointF) weather.SetURLVal(params, "humidity", u.HumidityOudoor) weather.SetURLVal(params, "indoorhumidity", u.HumidityIndoor) weather.SetURLVal(params, "indoortempf", u.TempIndoorF) weather.SetURLVal(params, "lowbatt", ptr.To(0)) weather.SetURLVal(params, "monthlyrainin", u.MonthlyRainIn) weather.SetURLVal(params, "rainin", u.HourlyRainIn) weather.SetURLVal(params, "realtime", ptr.To(1)) weather.SetURLVal(params, "rtfreq", ptr.To(60)) weather.SetURLVal(params, "softwaretype", u.StationType) weather.SetURLVal(params, "solarradiation", u.SolarRadiation) weather.SetURLVal(params, "tempf", u.TempOutdoorF) weather.SetURLVal(params, "weeklyrainin", u.WeeklyRainIn) weather.SetURLVal(params, "windchillf", u.WindChillF) weather.SetURLVal(params, "winddir", u.WindDir) weather.SetURLVal(params, "windgustmph", u.WindGustMPH) weather.SetURLVal(params, "windspeedmph", u.WindSpeedMPH) weather.SetURLVal(params, "yearlyrainin", u.YearlyRainIn) return params }