diff options
author | NeonXP <i@neonxp.dev> | 2023-01-04 18:52:25 +0300 |
---|---|---|
committer | NeonXP <i@neonxp.dev> | 2023-01-04 18:52:25 +0300 |
commit | 5947c1d643dfc077c19d3b4c01e599578e1dfe62 (patch) | |
tree | 6723c5982626403a507ed25c74711ae74eccbe23 /pkg/weather/weather.go |
Diffstat (limited to 'pkg/weather/weather.go')
-rw-r--r-- | pkg/weather/weather.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/pkg/weather/weather.go b/pkg/weather/weather.go new file mode 100644 index 0000000..4674ee1 --- /dev/null +++ b/pkg/weather/weather.go @@ -0,0 +1,30 @@ +package weather + +import ( + "fmt" + "io" + "net/http" + "time" +) + +func Get(city string) (string, error) { + client := http.Client{ + Timeout: 30 * time.Second, + } + u := fmt.Sprintf(`https://wttr.in/%s?format=%%l:+%%t+(ощущается+как+%%f)+%%c+%%C`, city) + req, _ := http.NewRequest(http.MethodGet, u, nil) + req.Header.Set("Accept-Language", "ru") + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("Не смог получить погоду") + } + result, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + return string(result), nil +} |