blob: 4674ee1ffa2efafbd47688b9a8467436aa812f3b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
}
|