Exemple #1
0
func updateFromSource2ForAppEngine(r *http.Request) (int64, bool, error) {
	var result int64
	ctx := appengine.NewContext(r)
	client := urlfetch.Client(ctx)
	resp, err := client.Get(source2Url)
	if err != nil {
		return 0, false, err
	}
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	if resp.StatusCode == 200 {
		var rate entities.Rate2Response
		dec.Decode(&rate)
		result = rate.TimestampUnix
		repo := repository.New(repoSize, true, r)
		if err := repo.Push(entities.Rate{
			Base: rate.Base,
			ID:   rate.TimestampUnix,
			RUB:  rate.Quotes["USDRUB"],
			JPY:  rate.Quotes["USDJPY"],
			GBP:  rate.Quotes["USDGBP"],
			USD:  rate.Quotes["USDUSD"],
			EUR:  rate.Quotes["USDEUR"],
			CNY:  rate.Quotes["USDCNY"],
			CHF:  rate.Quotes["USDCHF"]}); err != nil {
			return 0, false, fmt.Errorf("Push rate to repo error: %v", err)
		}
	} else {
		var err2Resp entities.Error2Response
		dec.Decode(&err2Resp)
		return 0, false, fmt.Errorf(err2Resp.ToString())
	}
	return result, true, nil
}
Exemple #2
0
func updateFromSource2() (int64, bool, error) {
	var result int64
	resp, err := http.Get(source2URL)
	if err != nil {
		return 0, false, err
	}
	defer resp.Body.Close()
	dec := json.NewDecoder(resp.Body)
	if resp.StatusCode == 200 {
		var rate entities.Rate2Response
		dec.Decode(&rate)
		result = rate.TimestampUnix
		log.Println(rate.ToShortString())
		if err := _repo.Push(entities.Rate{
			Base: rate.Base,
			ID:   rate.TimestampUnix,
			RUB:  rate.Quotes["USDRUB"],
			JPY:  rate.Quotes["USDJPY"],
			GBP:  rate.Quotes["USDGBP"],
			USD:  rate.Quotes["USDUSD"],
			EUR:  rate.Quotes["USDEUR"],
			CNY:  rate.Quotes["USDCNY"],
			CHF:  rate.Quotes["USDCHF"]}); err != nil {
			log.Printf("Push rate to repo error: %v.", err)
			return 0, false, nil
		}
	} else {
		var err entities.Error2Response
		dec.Decode(&err)
		return 0, false, fmt.Errorf(err.ToString())
	}
	return result, true, nil
}