@@ -4,16 +4,15 @@ import (
4
4
"encoding/json"
5
5
"fmt"
6
6
"net/http"
7
- "time"
8
7
9
8
"github.com/kashifkhan0771/go-weather/config"
10
9
"github.com/kashifkhan0771/go-weather/models"
11
10
)
12
11
13
12
// GetCurrentWeather return current weather response based on the option query
14
13
func (c WeatherAPIConfig ) GetCurrentWeather (options Options ) (* models.WeatherResponse , error ) {
15
- if options .Query == "" {
16
- return nil , fmt .Errorf ("query is empty " )
14
+ if ! validateQuery ( options .Query ) {
15
+ return nil , fmt .Errorf ("invalid query parameter " )
17
16
}
18
17
19
18
url := fmt .Sprintf ("%s%s?q=%s" , config .BaseURL , config .CurrentWeatherJSON , options .Query )
@@ -25,27 +24,44 @@ func (c WeatherAPIConfig) GetCurrentWeather(options Options) (*models.WeatherRes
25
24
26
25
defer resp .Body .Close ()
27
26
28
- // Check response status code
29
- if resp .StatusCode != http .StatusOK {
27
+ // Decode JSON response body
28
+ var weatherResp models.WeatherResponse
29
+ err = json .NewDecoder (resp .Body ).Decode (& weatherResp )
30
+ if err != nil {
31
+ return nil , err
32
+ }
33
+
34
+ return & weatherResp , nil
35
+ }
36
+
37
+ func (c WeatherAPIConfig ) WeatherHistory (options Options ) (* models.WeatherResponse , error ) {
38
+ if ! validateQuery (options .Query ) {
39
+ return nil , fmt .Errorf ("invalid query parameter" )
40
+ } else if ! validateDate (options .Date ) {
41
+ return nil , fmt .Errorf ("invalid date parameter" )
42
+ }
43
+
44
+ url := fmt .Sprintf ("%s%s?q=%s&dt=%s" , config .BaseURL , config .HistoryWeatherJSON , options .Query , options .Date .Format ("2006-01-02" ))
45
+
46
+ resp , err := c .makeRequest (http .MethodGet , url )
47
+ if err != nil {
30
48
return nil , err
31
49
}
32
50
51
+ defer resp .Body .Close ()
52
+
33
53
// Decode JSON response body
34
- var weatherResp * models.WeatherResponse
54
+ var weatherResp models.WeatherResponse
35
55
err = json .NewDecoder (resp .Body ).Decode (& weatherResp )
36
56
if err != nil {
37
57
return nil , err
38
58
}
39
59
40
- return weatherResp , nil
60
+ return & weatherResp , nil
41
61
}
42
62
43
63
// makeRequest is a helper function for making API requests.
44
64
func (c WeatherAPIConfig ) makeRequest (method , url string ) (* http.Response , error ) {
45
- client := & http.Client {
46
- Timeout : time .Second * config .APIsTimeout ,
47
- }
48
-
49
65
req , err := http .NewRequest (method , url , http .NoBody )
50
66
if err != nil {
51
67
return nil , err
@@ -54,9 +70,13 @@ func (c WeatherAPIConfig) makeRequest(method, url string) (*http.Response, error
54
70
req .Header .Set ("key" , c .XApiKey )
55
71
56
72
// Send request
57
- resp , err := client .Do (req )
73
+ resp , err := c . HttpClient .Do (req )
58
74
if err != nil {
59
- return nil , err
75
+ return nil , fmt .Errorf ("HTTP request failed: %w" , err )
76
+ }
77
+
78
+ if resp .StatusCode != http .StatusOK {
79
+ return nil , fmt .Errorf ("unexpected status code: %d" , resp .StatusCode )
60
80
}
61
81
62
82
return resp , nil
0 commit comments