-
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
Conversation
- Changed the function signature of the FetchData method in the Provides interface - Added the SetQueryParams method to the providers aligned to the concerns raised in tinkershack#13 Resolves tinkershack#13 References tinkershack#5
providers/meteoblue.go
Outdated
// 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) map[string]string { | ||
// Get the config to accesss the API Key |
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.
we can add a check to see whether the coords aren't nil ?
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.
I thought we could do this when we define the endpoints, before calling fetchData itself. If it's nil, we don't make a request to the provider and write the err response
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.
-
This patch makes FetchData() receive and set Location dynamically. That's good.
-
The idea is to reduce the movement/redundant passing of QP, however this patch hasn't addressed it yet; since it returns the QP data, it must then be passed around. Please consider the following changes?
a. OpenMeteoProvider
struct must have QueryParams
field to hold the QP values that is of type map[string]string
b. SetQueryParams(*plumber.Coordinates)
must set the field OpenMeteoProvider.QueryParams with appropriate values. Since it's a method on OpenMeteoProvider
that sets its own field, it doesn't have to return anything. In this patch, it's instead returning the query params rather than setting it.
Please correct if I have misunderstand something.
c. NewOpenMeteoProvider()
and NewMeteoBlueProvider()
must appropriately call their respective SetQueryParams() method to set the QP before returning a new struct. (A default location, or nil, may be set here.) It would be convenient to also make the appropriate p.client.SetQueryParams()
to resty with this QP before returning the struct, so that it sticks and only the location cords may have to passed while fetching data.
d. FetchData()
need not change location data of QueryParams
field of the MeteoProvider struct as it's a transient call. This side effect is misleading, and not necessary. It simply has to set the passed location co-ords via p.client.SetQueryParams()
as the other QP is already set earlier while creating a new provider. That way, only the location co-ords has to be set freshly, and that's the intention as well.
Makes sense, I will implement them. |
- Add queryParams field on the providers - Update SetQueryParams method to set the queryParams field on the provider - Update NewOpenMeteoProvider() and NewMeteoBlueProvider() to set default QPs on the client - Update FetchData() to only set the coords, and not the entire QPs
providers/meteoblue.go
Outdated
func (p *MeteoBlueProvider) 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 comment
The reason will be displayed to describe this comment to others. Learn more.
"lat"
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.
LGTM. Just the typo of the query fields "lat" and "long" needs fixing.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please ignore. What you have put here is correct.
Open Meteo uses "latitude" and "longitude" while Meteoblue uses "lat" and "lon" |
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.
There's a mix up between the use of "lat" and "lon" for Meteo Blue, in SetQueryParams()
call within FetchData()
.
providers/meteoblue.go
Outdated
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 comment
The 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 "...&lat=0.000000&latitude=10.901838&lon=0.000000&longitude=76.899845&tz=GMT"
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Resolves #13
References #5