From 21ba0458e1b318eca7e7c5092f84d59cb23a6b5b Mon Sep 17 00:00:00 2001 From: Nils Jakobi Date: Sat, 21 Nov 2020 18:36:33 +0100 Subject: [PATCH] added GetCoinValue function to coinGeckoAPI --- cake.go | 4 +++- coinGeckoAPI.go | 31 ++++++++++++++++++++++++++----- go.mod | 9 +-------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/cake.go b/cake.go index ecca2dd..1edc3a6 100644 --- a/cake.go +++ b/cake.go @@ -218,7 +218,8 @@ func monthlyRewardOverview(lines []line) rewards { referral := make(map[time.Month]float64) airdrop := make(map[time.Month]float64) swapped := make(map[time.Month]float64) - + // coinValue, err := GetCoinValue("bitcoin", "eur") + // if err == nil { // loop through all lines for i := range lines { // filter out operations @@ -238,6 +239,7 @@ func monthlyRewardOverview(lines []line) rewards { case "Swapped in": swapped[lines[i].Date.Month()] += lines[i].Amount } + // } } // get precision for specific coin diff --git a/coinGeckoAPI.go b/coinGeckoAPI.go index 7457c96..b084d7d 100644 --- a/coinGeckoAPI.go +++ b/coinGeckoAPI.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "io/ioutil" "net/http" ) @@ -17,10 +18,8 @@ func CheckStatusCoinGecko() bool { url := "https://api.coingecko.com/api/v3/ping" response, err := http.Get(url) if err == nil { - // API is available, let's read the response - data, _ := ioutil.ReadAll(response.Body) var status statusResponse - json.Unmarshal(data, &status) + readBody(response, &status) if status.GeckoSays == "(V3) To the Moon!" { // API is working as expected return true @@ -29,6 +28,28 @@ func CheckStatusCoinGecko() bool { return false } -func GetCoinInfo() { - +// GetCoinValue needs a comment +func GetCoinValue(coinID string, fiat string) (float64, error) { + // check if API is up + if CheckStatusCoinGecko() == true { + // https://api.coingecko.com/api/v3/simple/price?ids=defichain&vs_currencies=eur + url := "https://api.coingecko.com/api/v3/simple/price?ids=" + coinID + "&vs_currencies=" + fiat + response, err := http.Get(url) + if err == nil { + // there's no error, we got a response + var r map[string]interface{} + readBody(response, &r) + // as the response will vary on coin and fiat we need to use interface{} here + value := r[coinID].(map[string]interface{}) + return value[fiat].(float64), nil + } + return 0, fmt.Errorf("There was a problem with the response from Coingecko, Error is: %s", err.Error()) + } + return 0, fmt.Errorf("The API of Coingecko seems to be down") +} + +func readBody(h *http.Response, i interface{}) { + // API is available, let's read the response + data, _ := ioutil.ReadAll(h.Body) + json.Unmarshal(data, i) } diff --git a/go.mod b/go.mod index 83a9f8b..622436b 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,4 @@ module git.nils.zone/nils/goCake go 1.15 -require ( - git.nils.zone/nils/prettify v0.0.4 - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/lucasb-eyer/go-colorful v1.0.3 - github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect - github.com/stretchr/testify v1.6.1 - gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect -) +require github.com/lucasb-eyer/go-colorful v1.0.3