Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SetQueryParams in the Provider Interface #18

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions providers/meteoblue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
const meteoBlueProviderName = "meteoblue"

type MeteoBlueProvider struct {
client rest.HTTPClient
config config.MeteoProvider
client rest.HTTPClient
config config.MeteoProvider
queryParams map[string]string
}

func NewMeteoBlueProvider() (*MeteoBlueProvider, error) {
Expand Down Expand Up @@ -45,16 +46,26 @@ func NewMeteoBlueProvider() (*MeteoBlueProvider, error) {
client.EnableTrace()
}

return &MeteoBlueProvider{
provider := MeteoBlueProvider{
client: client,
config: meteoConfig,
}, nil
}
// Setting the default location to 0,0
provider.SetQueryParams(plumber.NewCoordinates(0, 0))

// Creating the new request(which will be reused in all FetchData calls) and setting the default queryParams on the client
provider.client.NewRequest()
provider.client.SetQueryParams(provider.queryParams)
return &provider, nil
}

func (p *MeteoBlueProvider) FetchData(qp map[string]string) (*plumber.BaseData, error) {
// Creating a new request everytime we fecth data
p.client.NewRequest()
resp, err := p.client.SetQueryParams(qp).Get(p.config.APIPath)
func (p *MeteoBlueProvider) FetchData(coords *plumber.Coordinates) (*plumber.BaseData, error) {
resp, err := p.client.
SetQueryParams(map[string]string{
"lat": fmt.Sprintf("%f", coords.Latitude),
"lon": fmt.Sprintf("%f", coords.Longitude),
}).
Get(p.config.APIPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,6 +97,7 @@ func (p *MeteoBlueProvider) FetchData(qp map[string]string) (*plumber.BaseData,
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

// TO-DO: #12
// Map the JSON response to plumber.BaseData
baseData := &plumber.BaseData{
Latitude: data.Latitude,
Expand All @@ -96,4 +108,14 @@ func (p *MeteoBlueProvider) FetchData(qp map[string]string) (*plumber.BaseData,
return baseData, nil
}

// Maybe add a MeteoBlueQueryParams like OpenMeteoQueryParams to have some standard for all providers?
// Set the QueryParams for the request
func (p *MeteoBlueProvider) SetQueryParams(coords *plumber.Coordinates) {
// Setting the queryParams on the provider
p.queryParams = map[string]string{
"tz": "GMT",
"format": "json",
"forecast_days": "1",
"apikey": p.config.APIKey,
}

}
35 changes: 22 additions & 13 deletions providers/open_meteo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
const openMeteoProviderName = "open-meteo"

type OpenMeteoProvider struct {
client rest.HTTPClient
config config.MeteoProvider
client rest.HTTPClient
config config.MeteoProvider
queryParams map[string]string
}

// NewOpenMeteoProvider returns a new instance of OpenMeteoProvider
Expand Down Expand Up @@ -46,16 +47,27 @@ func NewOpenMeteoProvider() (*OpenMeteoProvider, error) {
client.EnableTrace()
}

return &OpenMeteoProvider{
provider := OpenMeteoProvider{
client: client,
config: meteoConfig,
}, nil
}
// Setting the default location to 0,0
provider.SetQueryParams(plumber.NewCoordinates(0, 0))
// Creating the new request(which will be reused in all FetchData calls) and setting the default queryParams on the client
provider.client.NewRequest()
provider.client.SetQueryParams(provider.queryParams)
return &provider, nil
}

// FetchData fetches API data from open-meteo provider for the given query parameters map
func (p *OpenMeteoProvider) FetchData(qp map[string]string) (*plumber.BaseData, error) {
p.client.NewRequest()
resp, err := p.client.SetQueryParams(qp).Get(p.config.APIPath)
func (p *OpenMeteoProvider) FetchData(coords *plumber.Coordinates) (*plumber.BaseData, error) {
resp, err := p.client.
SetQueryParams(map[string]string{
"latitude": fmt.Sprintf("%f", coords.Latitude),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"lat" and "long"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore. What you have put here is correct.

"longitude": fmt.Sprintf("%f", coords.Longitude),
}).
Get(p.config.APIPath)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,11 +98,9 @@ func (p *OpenMeteoProvider) FetchData(qp map[string]string) (*plumber.BaseData,
return data, nil
}

// OpenMeteoQueryParams forms the query parameters for OpenMeteo API based on given coordinates
func OpenMeteoQueryParams(coords *plumber.Coordinates) map[string]string {
qp := map[string]string{
"latitude": fmt.Sprintf("%f", coords.Latitude),
"longitude": fmt.Sprintf("%f", coords.Longitude),
// SetQueryParams forms the query parameters for OpenMeteo API based on given coordinates
func (p *OpenMeteoProvider) SetQueryParams(coords *plumber.Coordinates) {
p.queryParams = map[string]string{
"current": "temperature_2m,relative_humidity_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,weather_code,cloud_cover,pressure_msl,surface_pressure,wind_speed_10m,wind_direction_10m,wind_gusts_10m",
"hourly": "temperature_2m,relative_humidity_2m,dew_point_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,pressure_msl,surface_pressure,cloud_cover,cloud_cover_low,cloud_cover_mid,cloud_cover_high,visibility,evapotranspiration,et0_fao_evapotranspiration,vapour_pressure_deficit,wind_speed_10m,wind_speed_80m,wind_speed_120m,wind_speed_180m,wind_direction_10m,wind_direction_80m,wind_direction_120m,wind_direction_180m,wind_gusts_10m,temperature_80m,temperature_120m,temperature_180m,uv_index,uv_index_clear_sky,is_day,sunshine_duration,total_column_integrated_water_vapour,cape,lifted_index,convective_inhibition,freezing_level_height,boundary_layer_height,temperature_1000hPa,temperature_975hPa,temperature_950hPa,temperature_925hPa,temperature_900hPa,temperature_850hPa,temperature_800hPa,temperature_700hPa,temperature_600hPa,temperature_500hPa,temperature_400hPa,relative_humidity_1000hPa,relative_humidity_975hPa,relative_humidity_950hPa,relative_humidity_925hPa,relative_humidity_900hPa,relative_humidity_850hPa,relative_humidity_800hPa,relative_humidity_700hPa,relative_humidity_600hPa,relative_humidity_500hPa,relative_humidity_400hPa,cloud_cover_1000hPa,cloud_cover_975hPa,cloud_cover_950hPa,cloud_cover_925hPa,cloud_cover_900hPa,cloud_cover_850hPa,cloud_cover_800hPa,cloud_cover_700hPa,cloud_cover_600hPa,cloud_cover_500hPa,cloud_cover_400hPa,wind_speed_1000hPa,wind_speed_975hPa,wind_speed_950hPa,wind_speed_925hPa,wind_speed_900hPa,wind_speed_850hPa,wind_speed_800hPa,wind_speed_700hPa,wind_speed_600hPa,wind_speed_500hPa,wind_speed_400hPa,wind_direction_1000hPa,wind_direction_975hPa,wind_direction_950hPa,wind_direction_925hPa,wind_direction_900hPa,wind_direction_850hPa,wind_direction_800hPa,wind_direction_700hPa,wind_direction_600hPa,wind_direction_500hPa,wind_direction_400hPa,geopotential_height_1000hPa,geopotential_height_975hPa,geopotential_height_950hPa,geopotential_height_925hPa,geopotential_height_900hPa,geopotential_height_850hPa,geopotential_height_800hPa,geopotential_height_700hPa,geopotential_height_600hPa,geopotential_height_500hPa,geopotential_height_400hPa",
"daily": "weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,daylight_duration,sunshine_duration,uv_index_max,uv_index_clear_sky_max,precipitation_sum,precipitation_hours,precipitation_probability_max,wind_speed_10m_max,wind_gusts_10m_max,wind_direction_10m_dominant,shortwave_radiation_sum,et0_fao_evapotranspiration",
Expand All @@ -101,5 +111,4 @@ func OpenMeteoQueryParams(coords *plumber.Coordinates) map[string]string {
"cell_selection": "nearest",
"models": "best_match",
}
return qp
}
3 changes: 2 additions & 1 deletion providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func init() {

// Provider interface defines the methods that each provider must implement
type Provider interface {
FetchData(queryParams map[string]string) (*plumber.BaseData, error)
FetchData(coords *plumber.Coordinates) (*plumber.BaseData, error)
SetQueryParams(coords *plumber.Coordinates)
}

// NewProvider returns the appropriate provider based on the name
Expand Down
17 changes: 3 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Serve(ctx context.Context, args []string) {
w.WriteHeader(http.StatusInternalServerError)
return
}
bd, err := p.FetchData(providers.OpenMeteoQueryParams(plumber.NewCoordinates(11.0056, 76.9661)))
bd, err := p.FetchData(plumber.NewCoordinates(10.9018379, 76.8998445))
if err != nil {
logger.Error(e.FAIL, "err", err, "description", "Couldn't fetch data from OpenMeteoProvider")
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -66,19 +66,8 @@ func Serve(ctx context.Context, args []string) {
w.WriteHeader(http.StatusInternalServerError)
return
}
qp := map[string]string{
"lat": "47.558",
"lon": "7.587",
"asl": "279",
"tz": "Europe/Zurich",
"name": "Test",
"windspeed": "kmh",
"format": "json",
"history_days": "1",
"forecast_days": "0",
"apikey": cfg.MeteoProviders[0].APIKey,
}
bd, err := p.FetchData(qp)
// TO-DO : After defining the MB response, write it to the response
bd, err := p.FetchData(plumber.NewCoordinates(10.9018379, 76.8998445))
if err != nil {
logger.Error(e.FAIL, "err", err, "description", "Couldn't fetch data from MeteoBlueProvider")
w.WriteHeader(http.StatusInternalServerError)
Expand Down