Skip to content

Commit a6f93d8

Browse files
authored
Merge pull request lossycache#3 from hikarimn/patch-2
get a response with zip code
2 parents 5571aeb + aadc89d commit a6f93d8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

‎openweathermap.go‎

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

158+
159+
func (owm *OpenWeatherMap) CurrentWeatherFromZip(zip int) (*CurrentWeatherResponse, error) {
160+
if owm.API_KEY == "" {
161+
// No API keys present, return error
162+
return nil, errors.New("No API keys present")
163+
}
164+
url := fmt.Sprintf("http://%s/data/2.5/weather?zip=%d&units=imperial&APPID=%s", API_URL, zip, owm.API_KEY)
165+
166+
body, err := makeApiRequest(url)
167+
if err != nil {
168+
return nil, err
169+
}
170+
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+
158182
func (owm *OpenWeatherMap) CurrentWeatherFromCityId(id int) (*CurrentWeatherResponse, error) {
159183
if (owm.API_KEY == "") {
160184
// No API keys present, return error
@@ -178,5 +202,3 @@ func (owm *OpenWeatherMap) CurrentWeatherFromCityId(id int) (*CurrentWeatherResp
178202
}
179203

180204

181-
182-

0 commit comments

Comments
 (0)