Skip to content

Commit f90c31b

Browse files
authored
get a response with zip code
1 parent 4d81eb7 commit f90c31b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

openweathermap.go

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

158+
func (owm *OpenWeatherMap) CurrentWeatherFromZip(zip 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?zip=%d&units=imperial&APPID=%s", API_URL, zip, owm.API_KEY)
164+
165+
body, err := makeApiRequest(url)
166+
if err != nil {
167+
return nil, err
168+
}
169+
var cwr CurrentWeatherResponse
158170

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+
}
159179

0 commit comments

Comments
 (0)