rewrite using openweathermap onecall api, adding forecasts

This commit is contained in:
joethei 2021-04-12 09:56:35 +02:00
parent d7d370aa42
commit f8c439a139
7 changed files with 190 additions and 137 deletions

View File

@ -9,19 +9,12 @@ import (
"strconv"
)
var baseUrl = "https://api.openweathermap.org/data/2.5/weather"
func GetByCityName(city string, language string, unit config.Units) WeatherResponse {
return get(baseUrl+"?q="+city, language, unit)
}
var baseUrl = "https://api.openweathermap.org/data/2.5/onecall"
func GetByCoordinates(longitude, latitude float64, language string, unit config.Units) WeatherResponse {
return get(baseUrl+"?lat="+strconv.FormatFloat(latitude, 'f', 6, 32)+"&lon="+strconv.FormatFloat(longitude, 'f', 6, 32), language, unit)
}
func GetByZipCode(zip int, country string, language string, unit config.Units) WeatherResponse {
return get(baseUrl+"?zip="+string(zip)+","+country, language, unit)
}
func get(url string, language string, unit config.Units) WeatherResponse {
var configuration = config.LoadConfiguration()

View File

@ -1,68 +1,87 @@
package client
type WeatherResponse struct {
Coordinates Coordinates `json:"coord,omitempty"`
Weather []Weather `json:"weather,omitempty"`
Base string `json:"base,omitempty"`
Main Main `json:"main,omitempty"`
Visibility int `json:"visibility,omitempty"`
Wind Wind `json:"wind,omitempty"`
Clouds Clouds `json:"clouds,omitempty"`
Rain Rain `json:"rain,omitempty"`
Snow Snow `json:"snow,omitempty"`
Date int `json:"dt,omitempty"`
System System `json:"sys,omitempty"`
Timezone int `json:"timezone,omitempty"`
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Cod int `json:"cod,omitempty"`
Latitude float32 `json:"lat,omitempty"`
Longitude float32 `json:"lon,omitempty"`
Timezone string `json:"timezone,omitempty"`
TimezoneOffset int `json:"timezone_offset,omitempty"`
Current WeatherForecast `json:"current,omitempty"`
Minutely []Minutely `json:"minutely,omitempty"`
Hourly []WeatherForecast `json:"hourly,omitempty"`
Daily []DailyForecast `json:"daily,omitempty"`
Alerts []Alerts `json:"alerts,omitempty"`
}
type WeatherForecast struct {
Date int64 `json:"dt,omitempty"`
Sunrise int64 `json:"sunrise,omitempty"`
Sunset int64 `json:"sunset,omitempty"`
Temperature float32 `json:"temp,omitempty"`
FeelsLike float32 `json:"feels_like,omitempty"`
Pressure int `json:"pressure,omitempty"`
Humidity int `json:"humidity,omitempty"`
DewPoint float32 `json:"dew_point,omitempty"`
UVI float32 `json:"uvi,omitempty"`
Clouds int `json:"clouds,omitempty"`
Visibility int `json:"visibility,omitempty"`
WindSpeed float32 `json:"wind_speed,omitempty"`
WindDegree int `json:"wind_degree,omitempty"`
WindGust float32 `json:"wind_gust,omitempty"`
Weather []Weather `json:"weather,omitempty"`
PossibilityOfPrecipitation float32 `json:"pop,omitempty"`
}
type Weather struct {
Id int `json:"id,omitempty"`
ID int `json:"id,omitempty"`
Main string `json:"main,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
}
type Coordinates struct {
Longitude float32 `json:"lon,omitempty"`
Latitude float32 `json:"lat,omitempty"`
type Minutely struct {
Date int64 `json:"dt,omitempty"`
Precipitation float32 `json:"precipitation,omitempty"`
}
type Main struct {
Temperature float32 `json:"temp,omitempty"`
FeelsLike float32 `json:"feels_like,omitempty"`
TemperatureMin float32 `json:"temp_min,omitempty"`
TemperatureMax float32 `json:"temp_max,omitempty"`
Pressure int `json:"pressure,omitempty"`
Humidity int `json:"humidity,omitempty"`
type DailyForecast struct {
Date int64 `json:"dt,omitempty"`
Sunrise int64 `json:"sunrise,omitempty"`
Sunset int64 `json:"sunset,omitempty"`
Temperature Temp `json:"temp,omitempty"`
FeelsLike FeelsLike `json:"feels_like,omitempty"`
Pressure int `json:"pressure,omitempty"`
Humidity int `json:"humidity,omitempty"`
DewPoint float32 `json:"dew_point,omitempty"`
UVI float32 `json:"uvi,omitempty"`
Clouds int `json:"clouds,omitempty"`
Visibility int `json:"visibility,omitempty"`
WindSpeed float32 `json:"wind_speed,omitempty"`
WindDegree int `json:"wind_degree,omitempty"`
WindGust float32 `json:"wind_gust,omitempty"`
Weather []Weather `json:"weather,omitempty"`
PossibilityOfPrecipitation float32 `json:"pop,omitempty"`
}
type Wind struct {
Speed float32 `json:"speed,omitempty"`
Degree int `json:"deg,omitempty"`
type Temp struct {
Day float32 `json:"day,omitempty"`
Min float32 `json:"min,omitempty"`
Max float32 `json:"max,omitempty"`
Night float32 `json:"night,omitempty"`
Eve float32 `json:"eve,omitempty"`
Morning float32 `json:"morn,omitempty"`
}
type Clouds struct {
All int `json:"all,omitempty"`
type FeelsLike struct {
Day float32 `json:"day,omitempty"`
Night float32 `json:"night,omitempty"`
Eve float32 `json:"eve,omitempty"`
Morning float32 `json:"morn,omitempty"`
}
type Rain struct {
OneHour int `json:",omitempty"`
ThreeHours int `json:"3h,omitempty"`
}
type Snow struct {
OneHour float32 `json:"1h,omitempty"`
ThreeHours float32 `json:"3h,omitempty"`
}
type System struct {
Type int `json:",omitempty"`
Id int `json:"id,omitempty"`
Message float32 `json:"message,omitempty"`
Country string `json:"country,omitempty"`
Sunrise int `json:"sunrise,omitempty"`
Sunset int `json:"sunset,omitempty"`
type Alerts struct {
Sender string `json:"sender_name,omitempty"`
Event string `json:"event,omitempty"`
Start int64 `json:"start,omitempty"`
End int64 `json:"end,omitempty"`
Description string `json:"description,omitempty"`
}

View File

@ -8,9 +8,8 @@ replace (
joethei.xyz/weather/server => ./server
)
require (
joethei.xyz/weather/config v1.0.0
joethei.xyz/weather/server v1.0.0
github.com/getsentry/sentry-go v0.6.1
)
joethei.xyz/weather/config v1.0.0
joethei.xyz/weather/server v1.0.0
)

View File

@ -18,7 +18,6 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
@ -35,6 +34,7 @@ github.com/getsentry/sentry-go v0.6.1/go.mod h1:0yZBuzSvbZwBnvaF9VwZIMen3kXscY8/
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
@ -42,10 +42,13 @@ github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6Wezm
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38 h1:y0Wmhvml7cGnzPa9nocn/fMraMH/lMDdeG+rkx4VgYY=
github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
@ -62,6 +65,7 @@ github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/juju/loggo v0.0.0-20180524022052-584905176618/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
@ -104,7 +108,9 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
@ -112,7 +118,9 @@ github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFo
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.1.0 h1:MkTeG1DMwsrdH7QtLXy5W+fUxWq+vmb6cLmyJ7aRtF0=
github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@ -124,13 +132,13 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/throttled/throttled v1.0.0 h1:GQ7a1ilkYMXnZ6V0EG7RiU3qHk1k/kpXO/aIQQXPfEQ=
github.com/throttled/throttled v2.2.4+incompatible h1:aVKdoH/qT5Mo1Lm/678OkX2pFg7aRpHlTn1tfgaSKxs=
github.com/throttled/throttled v2.2.4+incompatible/go.mod h1:0BjlrEGQmvxps+HuXLsyRdqpSRvJpq0PNIsOtqP9Nos=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w=
@ -169,6 +177,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190327201419-c70d86f8b7cf/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=

View File

@ -80,9 +80,8 @@ func StartServer() {
router := mux.NewRouter()
router.HandleFunc("/", defaultHandler)
router.HandleFunc("/city/{city}", chain.ThenFunc(cityHandler).ServeHTTP)
router.HandleFunc("/coordinates/{latitude},{longitude}", chain.ThenFunc(coordinatesHandler).ServeHTTP)
router.HandleFunc("/zip/{zip},{country}", chain.ThenFunc(zipCodeHandler).ServeHTTP)
router.HandleFunc("/current/{latitude},{longitude}", chain.ThenFunc(currentHandler).ServeHTTP)
router.HandleFunc("/forecast/{latitude},{longitude}", chain.ThenFunc(forecastHandler).ServeHTTP)
if err := http.ListenAndServe(":"+port, router); err != nil {
panic(err)
}
@ -96,26 +95,7 @@ func defaultHandler(writer http.ResponseWriter, _ *http.Request) {
}
func cityHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Content-Type", "application/json")
vars := mux.Vars(req)
if vars["city"] == "" {
if _, err := writer.Write([]byte("no city provided")); err != nil {
panic(err)
}
}
language, units := getOptionalArgs(req)
data := client.GetByCityName(vars["city"], language, units)
writer.WriteHeader(http.StatusOK)
if err := json.NewEncoder(writer).Encode(convert(data)); err != nil {
panic(err)
}
}
func coordinatesHandler(writer http.ResponseWriter, req *http.Request) {
func currentHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Content-Type", "application/json")
vars := mux.Vars(req)
@ -131,21 +111,28 @@ func coordinatesHandler(writer http.ResponseWriter, req *http.Request) {
data := client.GetByCoordinates(longitude, latitude, language, units)
writer.WriteHeader(http.StatusOK)
if err := json.NewEncoder(writer).Encode(convert(data)); err != nil {
if err := json.NewEncoder(writer).Encode(ConvertCurrent(data)); err != nil {
panic(err)
}
}
func zipCodeHandler(writer http.ResponseWriter, req *http.Request) {
func forecastHandler(writer http.ResponseWriter, req *http.Request) {
writer.Header().Set("Content-Type", "application/json")
vars := mux.Vars(req)
zip, err := strconv.Atoi(vars["zip"])
longitude, err := strconv.ParseFloat(vars["longitude"], 32)
if err != nil {
panic(err)
}
data := client.GetByZipCode(zip, vars["country"], config.LoadConfiguration().Language, config.Metric)
latitude, err := strconv.ParseFloat(vars["latitude"], 32)
if err != nil {
panic(err)
}
language, units := getOptionalArgs(req)
data := client.GetByCoordinates(longitude, latitude, language, units)
writer.WriteHeader(http.StatusOK)
if err := json.NewEncoder(writer).Encode(convert(data)); err != nil {
if err := json.NewEncoder(writer).Encode(ConvertDaily(data)); err != nil {
panic(err)
}
}
@ -168,36 +155,53 @@ func getOptionalArgs(req *http.Request) (language string, units config.Units) {
return language, units
}
func convert(weatherData client.WeatherResponse) Weather {
iconMapping := map[string]string{
"01d": "★",
"01n": "★",
"02d": "☁☀",
"02n": "☁☀",
"03d": "☁",
"03n": "☁",
"10d": "☔",
"10n": "☔",
"11d": "⚕",
"11n": "⚕",
"13d": "❄",
"13n": "❄",
"50d": "☷",
"50n": "☷",
}
return Weather{
TemperatureCurrent: weatherData.Main.Temperature,
TemperatureMin: weatherData.Main.TemperatureMin,
TemperatureMax: weatherData.Main.TemperatureMax,
FeelsLike: weatherData.Main.FeelsLike,
Pressure: weatherData.Main.Pressure,
Humidity: weatherData.Main.Humidity,
Description: weatherData.Weather[0].Description,
Icon: iconMapping[weatherData.Weather[0].Icon],
WindSpeeed: weatherData.Wind.Speed,
WindDegree: weatherData.Wind.Degree,
Clouds: weatherData.Clouds.All,
Rain: weatherData.Rain.OneHour,
Snow: weatherData.Snow.OneHour,
func ConvertCurrent(weatherData client.WeatherResponse) Current {
return Current{
Sunrise: time.Unix(weatherData.Current.Sunrise, 0),
Sunset: time.Unix(weatherData.Current.Sunset, 0),
Temperature: weatherData.Current.Temperature,
FeelsLike: weatherData.Current.FeelsLike,
Pressure: weatherData.Current.Pressure,
Humidity: weatherData.Current.Humidity,
DewPoint: weatherData.Current.DewPoint,
UVI: weatherData.Current.UVI,
Clouds: weatherData.Current.Clouds,
Visibility: weatherData.Current.Visibility,
WindSpeed: weatherData.Current.WindSpeed,
WindDegree: weatherData.Current.WindDegree,
Description: weatherData.Current.Weather[0].Description,
Icon: weatherData.Current.Weather[0].Icon,
}
}
func ConvertDaily(weatherData client.WeatherResponse) []Daily {
var result []Daily
for i := 0; i < len(weatherData.Daily); i++ {
var daily = Daily{
Date: time.Unix(weatherData.Daily[i].Date, 0),
Sunrise: time.Unix(weatherData.Daily[i].Sunrise, 0),
Sunset: time.Unix(weatherData.Daily[i].Sunset, 0),
TemperatureDay: weatherData.Daily[i].Temperature.Day,
TemperatureNight: weatherData.Daily[i].Temperature.Night,
TemperatureMin: weatherData.Daily[i].Temperature.Min,
TemperatureMax: weatherData.Daily[i].Temperature.Max,
FeelsLikeDay: weatherData.Daily[i].FeelsLike.Day,
FeelsLikeNight: weatherData.Daily[i].FeelsLike.Night,
Pressure: weatherData.Daily[i].Pressure,
Humidity: weatherData.Daily[i].Humidity,
DewPoint: weatherData.Daily[i].DewPoint,
UVI: weatherData.Daily[i].UVI,
Clouds: weatherData.Daily[i].Clouds,
Visibility: weatherData.Daily[i].Visibility,
WindSpeed: weatherData.Daily[i].WindSpeed,
WindDegree: weatherData.Daily[i].WindDegree,
Description: weatherData.Daily[i].Weather[0].Description,
Icon: weatherData.Daily[i].Weather[0].Icon,
ProbabilityOfPrecipitation: weatherData.Daily[i].PossibilityOfPrecipitation,
}
result = append(result, daily)
}
return result
}

View File

@ -1,20 +1,43 @@
package server
type Weather struct {
TemperatureCurrent float32 `json:"temperature_current"`
FeelsLike float32 `json:"feels_like"`
TemperatureMin float32 `json:"temperature_min"`
TemperatureMax float32 `json:"temperature_max"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
Description string `json:"description"`
Icon string `json:"icon"`
import "time"
WindSpeeed float32 `json:"wind_speed"`
WindDegree int `json:"wind_degree"`
Clouds int `json:"clouds"`
Rain int `json:"rain"`
Snow float32 `json:"snow"`
type Current struct {
Sunrise time.Time `json:"sunrise"`
Sunset time.Time `json:"sunset"`
Temperature float32 `json:"temperature"`
FeelsLike float32 `json:"feels_like"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
DewPoint float32 `json:"dew_point"`
UVI float32 `json:"uvi"`
Clouds int `json:"clouds"`
Visibility int `json:"visibility"`
WindSpeed float32 `json:"wind_speed"`
WindDegree int `json:"wind_degree"`
Description string `json:"description"`
Icon string `json:"icon"`
}
type Daily struct {
Date time.Time `json:"date"`
Sunrise time.Time `json:"sunrise"`
Sunset time.Time `json:"sunset"`
TemperatureDay float32 `json:"temperature_day"`
TemperatureNight float32 `json:"temperature_night"`
TemperatureMin float32 `json:"temperature_min"`
TemperatureMax float32 `json:"temperature_max"`
FeelsLikeDay float32 `json:"feels_like_day"`
FeelsLikeNight float32 `json:"feels_like_night"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
DewPoint float32 `json:"dew_point"`
UVI float32 `json:"uvi"`
Clouds int `json:"clouds"`
Visibility int `json:"visibility"`
WindSpeed float32 `json:"wind_speed"`
WindDegree int `json:"wind_degree"`
Description string `json:"description"`
Icon string `json:"icon"`
ProbabilityOfPrecipitation float32 `json:"probability_of_precipitation"`
}

6
test/test.http Normal file
View File

@ -0,0 +1,6 @@
GET http://localhost:8082/forecast/53.3632202,7.2036405
Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJjbGllbnQiOiJlcGFwZXIifQ.BE5t0MrX_33WhRw2cuqnaLR1VfdvVnvYUAdxZrbj60e3v9k58wwJW9vjodYK1toX2M1ATOIGJFwii9O44ehJFw
###
GET http://localhost:8082/current/53.3632202,7.2036405
Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJjbGllbnQiOiJlcGFwZXIifQ.BE5t0MrX_33WhRw2cuqnaLR1VfdvVnvYUAdxZrbj60e3v9k58wwJW9vjodYK1toX2M1ATOIGJFwii9O44ehJFw