예제 #1
0
// GetCityWeather api URL
func GetCityWeather(cityID string) map[string]interface{} {
	start := time.Now()

	// HTTP get to url
	var result map[string]interface{}
	if err := HTTPGetter(APIURL(cityID, appID), &result); err != nil {
		utils.Error(fmt.Sprintf("error HTTPGetter %v", err))
		return map[string]interface{}{
			"city": cityID,
		}
	}
	finish := time.Since(start)

	// return if cod == 404
	if result["cod"] == "404" {
		return map[string]interface{}{
			"city":    cityID,
			"cod":     result["cod"],
			"message": result["message"],
		}
	}

	// return if cod == 200
	// push to map required display
	return map[string]interface{}{
		"city":   result["name"],
		"temp":   weatherUtils.ConvertKelvin(result["main"].(map[string]interface{})["temp"].(float64)),
		"getter": finish,
		"dt":     weatherUtils.UnixToLocalTime(int64(result["dt"].(float64))),
	}
}
예제 #2
0
// HTTPGetter main http getter or GET request
func HTTPGetter(url string, target interface{}) error {
	r, err := http.Get(url)
	if err != nil {
		utils.Error(fmt.Sprintf("HTTP GET %v", err))
		return err
	}
	defer r.Body.Close()

	return json.NewDecoder(r.Body).Decode(target)
}