Skip to content

Commit df0d63b

Browse files
authored
Merge pull request lossycache#2 from hikarimn/patch-1
Get a response using City ID
2 parents 4d81eb7 + 36f4e37 commit df0d63b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎openweathermap.go‎

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

158+
func (owm *OpenWeatherMap) CurrentWeatherFromCityId(id int) (*CurrentWeatherResponse, error) {
159+
if (owm.API_KEY == "") {
160+
// No API keys present, return error
161+
return nil, errors.New("No API keys present")
162+
}
163+
url := fmt.Sprintf("http://%s/data/2.5/weather?id=%d&units=imperial&APPID=%s", API_URL, id, owm.API_KEY)
164+
165+
body, err := makeApiRequest(url)
166+
if (err != nil) {
167+
return nil, err
168+
}
169+
var cwr CurrentWeatherResponse
170+
171+
// unmarshal the byte stream into a Go data type
172+
jsonErr := json.Unmarshal(body, &cwr)
173+
if jsonErr != nil {
174+
return nil, jsonErr
175+
}
176+
177+
return &cwr, nil
178+
}
179+
180+
158181

159182

0 commit comments

Comments
 (0)