Added metrics route

This commit is contained in:
2025-06-17 12:05:58 +02:00
parent 4844e946d3
commit cbafd785b9
8 changed files with 173 additions and 56 deletions

45
model/geoModel.go Normal file
View File

@@ -0,0 +1,45 @@
package model
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"github.com/ceticamarco/zephyr/types"
)
func GetCoordinates(cityName string, apiKey string) (types.City, error) {
url, err := url.Parse(GEO_URL)
if err != nil {
return types.City{}, err
}
params := url.Query()
params.Set("q", cityName)
params.Set("limit", "1")
params.Set("appid", apiKey)
url.RawQuery = params.Encode()
res, err := http.Get(url.String())
if err != nil {
return types.City{}, err
}
defer res.Body.Close()
var geoArr []types.City
if err := json.NewDecoder(res.Body).Decode(&geoArr); err != nil {
return types.City{}, err
}
if len(geoArr) == 0 {
return types.City{}, errors.New("Cannot find this city")
}
return types.City{
Name: geoArr[0].Name,
Lat: geoArr[0].Lat,
Lon: geoArr[0].Lon,
}, nil
}

59
model/metricsModel.go Normal file
View File

@@ -0,0 +1,59 @@
package model
import (
"encoding/json"
"math"
"net/http"
"net/url"
"strconv"
"github.com/ceticamarco/zephyr/types"
)
func GetMetrics(city *types.City, apiKey string) (types.Metrics, error) {
url, err := url.Parse(WTR_URL)
if err != nil {
return types.Metrics{}, err
}
params := url.Query()
params.Set("lat", strconv.FormatFloat(city.Lat, 'f', -1, 64))
params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64))
params.Set("appid", apiKey)
params.Set("units", "metric")
params.Set("exclude", "minutely,hourly,daily,alerts")
url.RawQuery = params.Encode()
res, err := http.Get(url.String())
if err != nil {
return types.Metrics{}, err
}
defer res.Body.Close()
// Structures representing the JSON response
type CurrentRes struct {
Humidity int `json:"humidity"`
Pressure int `json:"pressure"`
DewPoint float64 `json:"dew_point"`
UvIndex float64 `json:"uvi"`
Visibility float64 `json:"visibility"`
}
type MetricsRes struct {
Current CurrentRes `json:"current"`
}
var metricRes MetricsRes
if err := json.NewDecoder(res.Body).Decode(&metricRes); err != nil {
return types.Metrics{}, err
}
return types.Metrics{
Humidity: strconv.Itoa(metricRes.Current.Humidity),
Pressure: strconv.Itoa(metricRes.Current.Pressure),
DewPoint: strconv.FormatFloat(metricRes.Current.DewPoint, 'f', -1, 64),
UvIndex: strconv.FormatFloat(math.Round(metricRes.Current.UvIndex), 'f', -1, 64),
Visibility: strconv.FormatFloat((metricRes.Current.Visibility / 1000), 'f', -1, 64),
}, nil
}

6
model/urls.go Normal file
View File

@@ -0,0 +1,6 @@
package model
const (
GEO_URL = "https://api.openweathermap.org/geo/1.0/direct"
WTR_URL = "https://api.openweathermap.org/data/3.0/onecall"
)

View File

@@ -2,7 +2,6 @@ package model
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
@@ -12,11 +11,6 @@ import (
"github.com/ceticamarco/zephyr/types"
)
const (
GEO_URL = "https://api.openweathermap.org/geo/1.0/direct"
WTR_URL = "https://api.openweathermap.org/data/3.0/onecall"
)
func getEmoji(condition string, isNight bool) string {
switch condition {
case "Thunderstorm":
@@ -27,8 +21,8 @@ func getEmoji(condition string, isNight bool) string {
return "🌧️"
case "Snow":
return "☃️"
case "Mist", "Smoke", "Haze", "Dust", "Fog", "Sand", "Ash", "Squall":
return "🌫"
case "Mist", "Smoke", "Haze", "Dust", "Fog", "Sand", "Ash", "Squall", "Clouds":
return ""
case "Tornado":
return "🌪️"
case "Clear":
@@ -39,8 +33,6 @@ func getEmoji(condition string, isNight bool) string {
return "☀️"
}
}
case "Clouds":
return "☁️"
case "SunWithCloud":
return "🌤️"
case "CloudWithSun":
@@ -50,41 +42,6 @@ func getEmoji(condition string, isNight bool) string {
return "❓"
}
func GetCoordinates(cityName string, apiKey string) (types.City, error) {
url, err := url.Parse(GEO_URL)
if err != nil {
return types.City{}, err
}
params := url.Query()
params.Set("q", cityName)
params.Set("limit", "1")
params.Set("appid", apiKey)
url.RawQuery = params.Encode()
res, err := http.Get(url.String())
if err != nil {
return types.City{}, err
}
defer res.Body.Close()
var geoArr []types.City
if err := json.NewDecoder(res.Body).Decode(&geoArr); err != nil {
return types.City{}, err
}
if len(geoArr) == 0 {
return types.City{}, errors.New("Cannot find this city")
}
return types.City{
Name: geoArr[0].Name,
Lat: geoArr[0].Lat,
Lon: geoArr[0].Lon,
}, nil
}
func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
url, err := url.Parse(WTR_URL)
if err != nil {