Skip to content

Commit fad2a30

Browse files
Merge pull request #1 from kashifkhan0771/task/initial-setup
Added initial setup with current weather api client
2 parents 6eac79d + 5057ddd commit fad2a30

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

client/weather-api.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/kashifkhan0771/go-weather/config"
9+
"github.com/kashifkhan0771/go-weather/models"
10+
)
11+
12+
type WeatherAPIConfig struct {
13+
XApiKey string `json:"key"`
14+
}
15+
16+
func (c *WeatherAPIConfig) GetCurrentWeather(query string) (*models.WeatherResponse, error) {
17+
if query == "" {
18+
return nil, fmt.Errorf("query is empty")
19+
}
20+
21+
url := fmt.Sprintf("%s%s?q=%s", config.BaseURL, config.CurrentWeatherJSON, query)
22+
23+
// Create HTTP client
24+
client := http.Client{}
25+
26+
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
req.Header.Set("key", c.XApiKey)
32+
33+
// Send request
34+
resp, err := client.Do(req)
35+
if err != nil {
36+
return nil, err
37+
}
38+
defer resp.Body.Close()
39+
40+
// Check response status code
41+
if resp.StatusCode != http.StatusOK {
42+
fmt.Println("Error: Unexpected status code:", resp.StatusCode)
43+
return nil, err
44+
}
45+
46+
// Decode JSON response body
47+
var weatherResp *models.WeatherResponse
48+
err = json.NewDecoder(resp.Body).Decode(&weatherResp)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
return weatherResp, nil
54+
}

config/configuration.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package config
2+
3+
const (
4+
// BaseURL - is the base url of the all APIs
5+
BaseURL = "https://api.weatherapi.com/v1"
6+
7+
// API EndPoints
8+
CurrentWeatherJSON = "/current.json"
9+
)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/kashifkhan0771/go-weather
2+
3+
go 1.22.0

models/current.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package models
2+
3+
type Current struct {
4+
LastUpdatedEpoch int64 `json:"last_updated_epoch"`
5+
LastUpdated string `json:"last_updated"`
6+
TempC float64 `json:"temp_c"`
7+
TempF float64 `json:"temp_f"`
8+
IsDay int `json:"is_day"`
9+
Condition Condition `json:"condition"`
10+
WindMPH float64 `json:"wind_mph"`
11+
WindKPH float64 `json:"wind_kph"`
12+
WindDegree int `json:"wind_degree"`
13+
WindDir string `json:"wind_dir"`
14+
PressureMB float64 `json:"pressure_mb"`
15+
PressureIN float64 `json:"pressure_in"`
16+
PrecipMM float64 `json:"precip_mm"`
17+
PrecipIN float64 `json:"precip_in"`
18+
Humidity int `json:"humidity"`
19+
Cloud int `json:"cloud"`
20+
FeelsLikeC float64 `json:"feelslike_c"`
21+
FeelsLikeF float64 `json:"feelslike_f"`
22+
VisibilityKM float64 `json:"vis_km"`
23+
VisibilityMiles float64 `json:"vis_miles"`
24+
UVIndex float64 `json:"uv"`
25+
GustMPH float64 `json:"gust_mph"`
26+
GustKPH float64 `json:"gust_kph"`
27+
}
28+
29+
type Condition struct {
30+
Text string `json:"text"`
31+
Icon string `json:"icon"`
32+
Code int `json:"code"`
33+
}

models/location.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package models
2+
3+
type Location struct {
4+
Name string `json:"name"`
5+
Region string `json:"region"`
6+
Country string `json:"country"`
7+
Latitude float64 `json:"lat"`
8+
Longitude float64 `json:"lon"`
9+
TimezoneID string `json:"tz_id"`
10+
LocaltimeEpoch int64 `json:"localtime_epoch"`
11+
Localtime string `json:"localtime"`
12+
}

models/weather_response.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models
2+
3+
type WeatherResponse struct {
4+
Location Location `json:"location"`
5+
Current Current `json:"current"`
6+
}

0 commit comments

Comments
 (0)