Skip to content

Commit aadc89d

Browse files
authored
Merge branch 'master' into patch-2
2 parents f90c31b + 5571aeb commit aadc89d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ var err error
3131
currentWeather, err = owm.CurrentWeatherFromCoordinates(lat, long)
3232
```
3333

34+
If fetching the weather from a city ID, pass the city ID as an argument to the CurrentWeatherFromCityID function
35+
```go
36+
var currentWeather *openweathermap.CurrentWeatherResponse
37+
var err error
38+
39+
currentWeather, err = owm.CurrentWeatherFromCityID(id)
40+
```
41+
42+
If fetching the weather from a zip code, pass the zip code as an argument to the CurrentWeatherFromZip function
43+
```go
44+
var currentWeather *openweathermap.CurrentWeatherResponse
45+
var err error
46+
47+
currentWeather, err = owm.CurrentWeatherFromZip(zip)
48+
```
3449

3550
This function returns a struct, (CurrentWeatherResonse) that matches the fields of the json response from the API
3651
```json

‎openweathermap.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func (owm *OpenWeatherMap) CurrentWeatherFromCoordinates(lat, long float64) (*Cu
155155
return &cwr, nil
156156
}
157157

158+
158159
func (owm *OpenWeatherMap) CurrentWeatherFromZip(zip int) (*CurrentWeatherResponse, error) {
159160
if owm.API_KEY == "" {
160161
// No API keys present, return error
@@ -167,6 +168,29 @@ func (owm *OpenWeatherMap) CurrentWeatherFromZip(zip int) (*CurrentWeatherRespon
167168
return nil, err
168169
}
169170
var cwr CurrentWeatherResponse
171+
172+
// unmarshal the byte stream into a Go data type
173+
jsonErr := json.Unmarshal(body, &cwr)
174+
if jsonErr != nil {
175+
return nil, jsonErr
176+
}
177+
178+
return &cwr, nil
179+
}
180+
181+
182+
func (owm *OpenWeatherMap) CurrentWeatherFromCityId(id int) (*CurrentWeatherResponse, error) {
183+
if (owm.API_KEY == "") {
184+
// No API keys present, return error
185+
return nil, errors.New("No API keys present")
186+
}
187+
url := fmt.Sprintf("http://%s/data/2.5/weather?id=%d&units=imperial&APPID=%s", API_URL, id, owm.API_KEY)
188+
189+
body, err := makeApiRequest(url)
190+
if (err != nil) {
191+
return nil, err
192+
}
193+
var cwr CurrentWeatherResponse
170194

171195
// unmarshal the byte stream into a Go data type
172196
jsonErr := json.Unmarshal(body, &cwr)
@@ -177,3 +201,4 @@ func (owm *OpenWeatherMap) CurrentWeatherFromZip(zip int) (*CurrentWeatherRespon
177201
return &cwr, nil
178202
}
179203

204+

0 commit comments

Comments
 (0)