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 1 commit
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
29 changes: 27 additions & 2 deletions providers/meteoblue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"

"github.com/tinkershack/meteomunch/config"
e "github.com/tinkershack/meteomunch/errors"
"github.com/tinkershack/meteomunch/http/rest"
"github.com/tinkershack/meteomunch/logger"
"github.com/tinkershack/meteomunch/plumber"
Expand Down Expand Up @@ -51,9 +54,13 @@ func NewMeteoBlueProvider() (*MeteoBlueProvider, error) {
}, nil
}

func (p *MeteoBlueProvider) FetchData(qp map[string]string) (*plumber.BaseData, error) {
func (p *MeteoBlueProvider) FetchData(coords *plumber.Coordinates) (*plumber.BaseData, error) {
// Creating a new request everytime we fecth data
p.client.NewRequest()
// Getting the query parameters
qp := p.SetQueryParams(coords)

// Setting the paramters on the request and making the request
resp, err := p.client.SetQueryParams(qp).Get(p.config.APIPath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,6 +93,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 +104,21 @@ 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) 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

cfg, err := config.Get()
if err != nil {
slog.Error("Couldn't parse config", "error", err, "description", e.FAIL)
os.Exit(-1)
}
return map[string]string{
"lat": fmt.Sprintf("%f", coords.Latitude),
"lon": fmt.Sprintf("%f", coords.Longitude),
"tz": "GMT",
"format": "json",
"forecast_days": "1",
"apikey": cfg.MeteoProviders[0].APIKey,
}

}
11 changes: 6 additions & 5 deletions providers/open_meteo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func NewOpenMeteoProvider() (*OpenMeteoProvider, error) {
}

// 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) {
func (p *OpenMeteoProvider) FetchData(coords *plumber.Coordinates) (*plumber.BaseData, error) {
p.client.NewRequest()
qp := p.SetQueryParams(coords)

resp, err := p.client.SetQueryParams(qp).Get(p.config.APIPath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,9 +88,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) map[string]string {
return 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",
Expand All @@ -101,5 +103,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) map[string]string
}

// 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