From 2d73e0567c9ac6e03cfc8bfc62a8f24ef4fadebc Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Tue, 11 Jan 2022 23:39:30 +0100 Subject: Use new met.no locationforecast 2.0 API The old XML API will be discontinued next month and is already returning errors occasionally (apparently to nudge lazy devs like me to update their code :D). We're also doing a much better job at caching now, as suggested by the API documentation. We're not yet showing a weather icon, I still have to figure out how to do this now (apparently the weather icons are a separate API / actually just a tag.gz archive that the weather API references?), but at least we're not panic()ing any more in the request handlers due to the unexpected error responses :/. See https://developer.yr.no/doc/GettingStarted/ for an intorduction to the new API. --- http_getter/http_getter.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'http_getter') diff --git a/http_getter/http_getter.go b/http_getter/http_getter.go index 5df3ac2..9fdf7a5 100644 --- a/http_getter/http_getter.go +++ b/http_getter/http_getter.go @@ -4,16 +4,27 @@ import ( "net/http" ) -// Get is like http.Get, but we're sending our own user agent. -func Get(url string) (*http.Response, error) { - client := &http.Client{} - +func BuildGetRequest(url string) (*http.Request, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } req.Header.Add("User-Agent", "github.com/slivasur/startpage") + return req, nil +} +func Do(req *http.Request) (*http.Response, error) { + client := &http.Client{} return client.Do(req) } + +// Get is like http.Get, but we're sending our own user agent. +func Get(url string) (*http.Response, error) { + req, err := BuildGetRequest(url) + if err != nil { + return nil, err + } + + return Do(req) +} -- cgit v1.2.3-70-g09d2