refactor: Use query params instead of sprintf
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
5b8ba0122d
commit
012bd95cbb
2 changed files with 10 additions and 2 deletions
|
@ -22,13 +22,21 @@ func New(siteName string) (*Met, error) {
|
|||
}
|
||||
|
||||
func (m *Met) Forecast(lat, lon float64, alt *int) (*LocationForecastResult, error) {
|
||||
url := fmt.Sprintf("https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=%.4f&lon=%.4f&altitude=%d", lat, lon, alt)
|
||||
url := "https://api.met.no/weatherapi/locationforecast/2.0/complete"
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Add("lat", fmt.Sprintf("%.4f", lat))
|
||||
q.Add("lon", fmt.Sprintf("%.4f", lon))
|
||||
if alt != nil {
|
||||
q.Add("altitude", fmt.Sprintf("%d", alt))
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
req.Header.Set("User-Agent", m.siteName)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
|
|
@ -25,7 +25,7 @@ func TestForecast(t *testing.T) {
|
|||
f, err := c.Forecast(60.389444, 5.33, nil)
|
||||
assert.NoError(err)
|
||||
coords := f.Geometry.Coordinates
|
||||
assert.Equal([]float64{5.33, 60.3894, 0}, coords)
|
||||
assert.Equal([]float64{5.33, 60.3894, 2}, coords)
|
||||
|
||||
_, err = c.Forecast(10000.0, 10000.33, nil)
|
||||
assert.Error(err)
|
||||
|
|
Loading…
Reference in a new issue