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

Conversation

yendelevium
Copy link
Collaborator

Resolves #13
References #5

- 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
// 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
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

@yendelevium yendelevium Oct 21, 2024

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

Copy link
Contributor

@raamsri raamsri left a comment

Choose a reason for hiding this comment

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

  1. This patch makes FetchData() receive and set Location dynamically. That's good.

  2. 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.

@yendelevium
Copy link
Collaborator Author

  1. This patch makes FetchData() receive and set Location dynamically. That's good.
  2. 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
@yendelevium
Copy link
Collaborator Author

Have implemented the changes @adofm @raamsri , Please review

func (p *MeteoBlueProvider) 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"

Copy link
Contributor

@raamsri raamsri left a 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),
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.

@raamsri
Copy link
Contributor

raamsri commented Oct 22, 2024

LGTM. Just the typo of the query fields "lat" and "long" needs fixing.

Open Meteo uses "latitude" and "longitude" while Meteoblue uses "lat" and "lon"

Copy link
Contributor

@raamsri raamsri left a 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().

resp, err := p.client.
SetQueryParams(map[string]string{
"latitude": fmt.Sprintf("%f", coords.Latitude),
"longitude": fmt.Sprintf("%f", coords.Longitude),
Copy link
Contributor

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"

Copy link
Collaborator Author

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?

Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@raamsri raamsri merged commit 67ac69d into tinkershack:main Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: Providers must implement Query Parameters
3 participants