diff options
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 +} |