-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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{ | ||
"latitude": fmt.Sprintf("%f", coords.Latitude), | ||
"longitude": fmt.Sprintf("%f", coords.Longitude), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open-Meteo uses "latitude" and "longitude" while Meteoblue uses "lat" and "lon". This forms the URL as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, then how about we don't set the latitude and longitude / lat - long while setting the default query params and directly set them for the first time in the FetchData() call? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reckon that should be okay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
}). | ||
Get(p.config.APIPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -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, | ||
|
@@ -96,4 +108,16 @@ 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{ | ||
"lat": fmt.Sprintf("%f", coords.Latitude), | ||
"lon": fmt.Sprintf("%f", coords.Longitude), | ||
"tz": "GMT", | ||
"format": "json", | ||
"forecast_days": "1", | ||
"apikey": p.config.APIKey, | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "lat" and "long" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -86,9 +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{ | ||
// SetQueryParams forms the query parameters for OpenMeteo API based on given coordinates | ||
func (p *OpenMeteoProvider) SetQueryParams(coords *plumber.Coordinates) { | ||
p.queryParams = map[string]string{ | ||
"latitude": fmt.Sprintf("%f", coords.Latitude), | ||
"longitude": fmt.Sprintf("%f", coords.Longitude), | ||
"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", | ||
|
@@ -101,5 +113,4 @@ func OpenMeteoQueryParams(coords *plumber.Coordinates) map[string]string { | |
"cell_selection": "nearest", | ||
"models": "best_match", | ||
} | ||
return qp | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"lat"