-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather_api.go
83 lines (64 loc) · 2.04 KB
/
weather_api.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package go_weather
import (
"encoding/json"
"fmt"
"net/http"
"github.com/kashifkhan0771/go-weather/config"
"github.com/kashifkhan0771/go-weather/models"
)
// GetCurrentWeather return current weather response based on the option query
func (c WeatherAPIConfig) GetCurrentWeather(options Options) (*models.WeatherResponse, error) {
if !validateQuery(options.Query) {
return nil, fmt.Errorf("invalid query parameter")
}
url := fmt.Sprintf("%s%s?q=%s", config.BaseURL, config.CurrentWeatherJSON, options.Query)
resp, err := c.makeRequest(http.MethodGet, url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode JSON response body
var weatherResp models.WeatherResponse
err = json.NewDecoder(resp.Body).Decode(&weatherResp)
if err != nil {
return nil, err
}
return &weatherResp, nil
}
func (c WeatherAPIConfig) WeatherHistory(options Options) (*models.WeatherResponse, error) {
if !validateQuery(options.Query) {
return nil, fmt.Errorf("invalid query parameter")
} else if !validateDate(options.Date) {
return nil, fmt.Errorf("invalid date parameter")
}
url := fmt.Sprintf("%s%s?q=%s&dt=%s", config.BaseURL, config.HistoryWeatherJSON, options.Query, options.Date.Format("2006-01-02"))
resp, err := c.makeRequest(http.MethodGet, url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode JSON response body
var weatherResp models.WeatherResponse
err = json.NewDecoder(resp.Body).Decode(&weatherResp)
if err != nil {
return nil, err
}
return &weatherResp, nil
}
// makeRequest is a helper function for making API requests.
func (c WeatherAPIConfig) makeRequest(method, url string) (*http.Response, error) {
req, err := http.NewRequest(method, url, http.NoBody)
if err != nil {
return nil, err
}
req.Header.Set("key", c.XApiKey)
// Send request
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return resp, nil
}