21
loading...
This website collects cookies to deliver better user experience
package main
import (
"log"
)
type endpoint struct {
Path string `yaml:"path"`
Method string `yaml:"method"`
}
type weatherAPI struct {
Scheme string `yaml:"scheme"`
Host string `yaml:"host"`
Endpoints weatherEndpoints `yaml:"endpoints"`
}
type weatherEndpoints struct {
Forecast endpoint `yaml:"forecast"`
Sports endpoint `yaml:"sports"`
}
func main() {
conn := weatherAPI{
Scheme: "https",
Host: "api.weatherapi.com/v1",
Endpoints: weatherEndpoints{
Forecast: endpoint{
Path: "/forecast.json",
Method: "GET",
},
Sports: endpoint{
Path: "/sports.json",
Method: "GET",
},
},
}
log.Println(conn)
}
type Formatter func() (string, *url.URL)
func (w *weatherAPI) ForecastURL(date, region string) Formatter {
return func() (string, *url.URL) {
u := &url.URL{
Scheme: w.Scheme,
Host: w.Host,
Path: w.Endpoints.Forecast.Path,
}
rq := u.Query()
rq.Set("dateKey", date)
rq.Set("regionKey", region)
u.RawQuery = rq.Encode()
return http.MethodGet, u
}
}
func main() {
conn := weatherAPI{
Scheme: "https",
Host: "api.weatherapi.com",
Endpoints: weatherEndpoints{
Forecast: endpoint{
Path: "/forecast.json",
Method: "GET",
},
Sports: endpoint{
Path: "/sports.json",
Method: "GET",
},
},
}
f := conn.ForecastURL("testDate", "testRegion")
m, u := f()
log.Println(m)
log.Println(u)
}
go run main.go
2021/06/22 22:36:51 GET
2021/06/22 22:36:51 https://api.weatherapi.com/forecast.json?dateKey=testDate®ionKey=testRegion