Add option for coingecko demo api key

master
Miguel Mota 1 month ago
parent 409479636d
commit cbe3557015
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -31,7 +31,8 @@ func RootCmd() *cobra.Command {
config := os.Getenv("COINTOP_CONFIG") config := os.Getenv("COINTOP_CONFIG")
apiChoice := os.Getenv("COINTOP_API") apiChoice := os.Getenv("COINTOP_API")
cmcAPIKey := os.Getenv("CMC_PRO_API_KEY") cmcAPIKey := os.Getenv("CMC_PRO_API_KEY")
coingeckoAPIKey := os.Getenv("COINGECKO_PRO_API_KEY") coingeckoAPIKey := os.Getenv("COINGECKO_API_KEY")
coingeckoProAPIKey := os.Getenv("COINGECKO_PRO_API_KEY")
perPage := cointop.DefaultPerPage perPage := cointop.DefaultPerPage
maxPages := cointop.DefaultMaxPages maxPages := cointop.DefaultMaxPages
@ -104,6 +105,7 @@ See git.io/cointop for more info.`,
ConfigFilepath: config, ConfigFilepath: config,
CoinMarketCapAPIKey: cmcAPIKey, CoinMarketCapAPIKey: cmcAPIKey,
CoinGeckoAPIKey: coingeckoAPIKey, CoinGeckoAPIKey: coingeckoAPIKey,
CoinGeckoProAPIKey: coingeckoProAPIKey,
APIChoice: apiChoice, APIChoice: apiChoice,
Colorscheme: colorscheme, Colorscheme: colorscheme,
HideMarketbar: hideMarketbar, HideMarketbar: hideMarketbar,
@ -143,7 +145,8 @@ See git.io/cointop for more info.`,
rootCmd.Flags().UintVarP(&maxPages, "max-pages", "", maxPages, "Max number of pages") rootCmd.Flags().UintVarP(&maxPages, "max-pages", "", maxPages, "Max number of pages")
rootCmd.Flags().StringVarP(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) rootCmd.Flags().StringVarP(&config, "config", "c", config, fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", cmcAPIKey, "Set the CoinMarketCap Pro API key") rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", cmcAPIKey, "Set the CoinMarketCap Pro API key")
rootCmd.Flags().StringVarP(&coingeckoAPIKey, "coingecko-api-key", "", coingeckoAPIKey, "Set the CoinGecko Pro API key") rootCmd.Flags().StringVarP(&coingeckoAPIKey, "coingecko-api-key", "", coingeckoAPIKey, "Set the CoinGecko Demo API key")
rootCmd.Flags().StringVarP(&coingeckoProAPIKey, "coingecko-pro-api-key", "", coingeckoProAPIKey, "Set the CoinGecko Pro API key")
rootCmd.Flags().StringVarP(&apiChoice, "api", "", apiChoice, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"") rootCmd.Flags().StringVarP(&apiChoice, "api", "", apiChoice, "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")
rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", colorscheme, fmt.Sprintf("Colorscheme to use (default \"cointop\").\n%s", cointop.ColorschemeHelpString())) rootCmd.Flags().StringVarP(&colorscheme, "colorscheme", "", colorscheme, fmt.Sprintf("Colorscheme to use (default \"cointop\").\n%s", cointop.ColorschemeHelpString()))
rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, fmt.Sprintf("Cache directory (default %s)", cointop.DefaultCacheDir)) rootCmd.Flags().StringVarP(&cacheDir, "cache-dir", "", cacheDir, fmt.Sprintf("Cache directory (default %s)", cointop.DefaultCacheDir))

@ -170,6 +170,7 @@ type Config struct {
ConfigFilepath string ConfigFilepath string
CoinMarketCapAPIKey string CoinMarketCapAPIKey string
CoinGeckoAPIKey string CoinGeckoAPIKey string
CoinGeckoProAPIKey string
NoPrompts bool NoPrompts bool
HideMarketbar bool HideMarketbar bool
HideChart bool HideChart bool
@ -186,8 +187,9 @@ type Config struct {
// APIKeys is api keys structure // APIKeys is api keys structure
type APIKeys struct { type APIKeys struct {
cmc string cmc string
coingecko string coingecko string
coingeckoPro string
} }
// DefaultCurrency ... // DefaultCurrency ...
@ -385,7 +387,6 @@ func NewCointop(config *Config) (*Cointop, error) {
} }
} }
// prompt for CoinGecko api key if not found
if config.CoinGeckoAPIKey != "" { if config.CoinGeckoAPIKey != "" {
ct.apiKeys.coingecko = config.CoinGeckoAPIKey ct.apiKeys.coingecko = config.CoinGeckoAPIKey
if err := ct.SaveConfig(); err != nil { if err := ct.SaveConfig(); err != nil {
@ -393,6 +394,13 @@ func NewCointop(config *Config) (*Cointop, error) {
} }
} }
if config.CoinGeckoProAPIKey != "" {
ct.apiKeys.coingeckoPro = config.CoinGeckoProAPIKey
if err := ct.SaveConfig(); err != nil {
return nil, err
}
}
if config.Colorscheme != "" { if config.Colorscheme != "" {
ct.colorschemeName = config.Colorscheme ct.colorschemeName = config.Colorscheme
} }
@ -431,10 +439,10 @@ func NewCointop(config *Config) (*Cointop, error) {
} }
if ct.apiChoice == CoinGecko && ct.apiKeys.coingecko == "" { if ct.apiChoice == CoinGecko && ct.apiKeys.coingecko == "" {
apiKey := os.Getenv("COINGECKO_PRO_API_KEY") apiKey := os.Getenv("COINGECKO_API_KEY")
if apiKey == "" { if apiKey == "" {
// if !config.NoPrompts { // if !config.NoPrompts {
// apiKey, err = ct.ReadAPIKeyFromStdin("CoinGecko Pro") // apiKey, err = ct.ReadAPIKeyFromStdin("CoinGecko")
// if err != nil { // if err != nil {
// return nil, err // return nil, err
// } // }
@ -450,13 +458,34 @@ func NewCointop(config *Config) (*Cointop, error) {
} }
} }
if ct.apiChoice == CoinGecko && ct.apiKeys.coingeckoPro == "" {
apiKey := os.Getenv("COINGECKO_PRO_API_KEY")
if apiKey == "" {
// if !config.NoPrompts {
// apiKey, err = ct.ReadAPIKeyFromStdin("CoinGecko Pro")
// if err != nil {
// return nil, err
// }
// ct.apiKeys.coingeckoPro = apiKey
// }
} else {
ct.apiKeys.coingeckoPro = apiKey
}
if err := ct.SaveConfig(); err != nil {
return nil, err
}
}
if ct.apiChoice == CoinMarketCap { if ct.apiChoice == CoinMarketCap {
ct.api = api.NewCMC(ct.apiKeys.cmc) ct.api = api.NewCMC(ct.apiKeys.cmc)
} else if ct.apiChoice == CoinGecko { } else if ct.apiChoice == CoinGecko {
ct.api = api.NewCG(&api.CoinGeckoConfig{ ct.api = api.NewCG(&api.CoinGeckoConfig{
PerPage: perPage, PerPage: perPage,
MaxPages: maxPages, MaxPages: maxPages,
ApiKey: ct.apiKeys.coingecko, ApiKey: ct.apiKeys.coingecko,
ProApiKey: ct.apiKeys.coingeckoPro,
}) })
} else { } else {
return nil, ErrInvalidAPIChoice return nil, ErrInvalidAPIChoice

@ -255,7 +255,8 @@ func (ct *Cointop) ConfigToToml() ([]byte, error) {
} }
coingeckoIfc := map[string]interface{}{ coingeckoIfc := map[string]interface{}{
"pro_api_key": ct.apiKeys.coingecko, "api_key": ct.apiKeys.coingecko,
"pro_api_key": ct.apiKeys.coingeckoPro,
} }
var priceAlertsIfc []interface{} var priceAlertsIfc []interface{}
@ -484,9 +485,12 @@ func (ct *Cointop) loadAPIKeysFromConfig() error {
} }
for key, value := range ct.config.CoinGecko { for key, value := range ct.config.CoinGecko {
k := strings.TrimSpace(strings.ToLower(key)) k := strings.TrimSpace(strings.ToLower(key))
if k == "pro_api_key" { if k == "api_key" {
ct.apiKeys.coingecko = value.(string) ct.apiKeys.coingecko = value.(string)
} }
if k == "pro_api_key" {
ct.apiKeys.coingeckoPro = value.(string)
}
} }
return nil return nil
} }

@ -59,7 +59,8 @@ func GetCoinPrices(config *PricesConfig) ([]string, error) {
priceAPI = api.NewCMC("") priceAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko { } else if config.APIChoice == CoinGecko {
priceAPI = api.NewCG(&api.CoinGeckoConfig{ priceAPI = api.NewCG(&api.CoinGeckoConfig{
ApiKey: os.Getenv("COINGECKO_PRO_API_KEY"), ApiKey: os.Getenv("COINGECKO_API_KEY"),
ProApiKey: os.Getenv("COINGECKO_PRO_API_KEY"),
}) })
} else { } else {
return nil, ErrInvalidAPIChoice return nil, ErrInvalidAPIChoice

@ -123,6 +123,9 @@ refresh_rate = 60
[coinmarketcap] [coinmarketcap]
pro_api_key = "" pro_api_key = ""
[coingecko]
pro_api_key = ""
``` ```
## List of actions ## List of actions

@ -97,7 +97,7 @@ draft: false
Run cointop with the `--clean` flag to delete the cache. If you're still not seeing any data, then please [submit an issue](https://github.com/cointop-sh/cointop/issues/new). Run cointop with the `--clean` flag to delete the cache. If you're still not seeing any data, then please [submit an issue](https://github.com/cointop-sh/cointop/issues/new).
## How do I get a CoinMarketCap Pro API key? ## How do I get a CoinMarketCap Pro (Paid) API key?
Create an account on [CoinMarketCap](https://pro.coinmarketcap.com/signup) and visit the [Account](https://pro.coinmarketcap.com/account) page to copy your Pro API key. Create an account on [CoinMarketCap](https://pro.coinmarketcap.com/signup) and visit the [Account](https://pro.coinmarketcap.com/account) page to copy your Pro API key.
@ -122,7 +122,28 @@ draft: false
cointop --coinmarketcap-api-key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx cointop --coinmarketcap-api-key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
``` ```
## How do I add my CoinGecko Pro API key? ## How do I add my CoinGecko Demo (Free) API key?
Add the API key in the cointop config file:
```toml
[coingecko]
api_key = "CG-xxxxxxxxxxxxxxxxxxxxxxxx"
```
Alternatively, you can export the environment variable `COINGECKO_API_KEY` containing the API key in your `~/.bashrc`
```bash
export COINGECKO_API_KEY=CG-xxxxxxxxxxxxxxxxxxxxxxxx
```
You may also set the API key on start:
```bash
cointop --coingecko-api-key=CG-xxxxxxxxxxxxxxxxxxxxxxxx
```
## How do I add my CoinGecko Pro (Paid) API key?
Add the API key in the cointop config file: Add the API key in the cointop config file:
@ -140,7 +161,7 @@ draft: false
You may also set the API key on start: You may also set the API key on start:
```bash ```bash
cointop --coingecko-api-key=CG-xxxxxxxxxxxxxxxxxxxxxxxx cointop --coingecko-pro-api-key=CG-xxxxxxxxxxxxxxxxxxxxxxxx
``` ```
## I can I add my own API to cointop? ## I can I add my own API to cointop?

@ -6,9 +6,10 @@ import (
) )
type CoinGeckoConfig struct { type CoinGeckoConfig struct {
PerPage uint PerPage uint
MaxPages uint MaxPages uint
ApiKey string ApiKey string
ProApiKey string
} }
// NewCMC new CoinMarketCap API // NewCMC new CoinMarketCap API
@ -24,8 +25,9 @@ func NewCC() {
// NewCG new CoinGecko API // NewCG new CoinGecko API
func NewCG(config *CoinGeckoConfig) Interface { func NewCG(config *CoinGeckoConfig) Interface {
return cg.NewCoinGecko(&cg.Config{ return cg.NewCoinGecko(&cg.Config{
PerPage: config.PerPage, PerPage: config.PerPage,
MaxPages: config.MaxPages, MaxPages: config.MaxPages,
ApiKey: config.ApiKey, ApiKey: config.ApiKey,
ProApiKey: config.ProApiKey,
}) })
} }

@ -24,9 +24,10 @@ var ErrNotFound = errors.New("not found")
// Config config // Config config
type Config struct { type Config struct {
PerPage uint PerPage uint
MaxPages uint MaxPages uint
ApiKey string ApiKey string
ProApiKey string
} }
// Service service // Service service
@ -53,7 +54,7 @@ func NewCoinGecko(config *Config) *Service {
maxPages = uint(math.Ceil(math.Max(float64(maxResults)/float64(maxResultsPerPage), 1))) maxPages = uint(math.Ceil(math.Max(float64(maxResults)/float64(maxResultsPerPage), 1)))
} }
client := gecko.NewClient(nil, config.ApiKey) client := gecko.NewClient(nil, config.ApiKey, config.ProApiKey)
svc := &Service{ svc := &Service{
client: client, client: client,
maxResultsPerPage: uint(math.Min(float64(maxResults), float64(maxResultsPerPage))), maxResultsPerPage: uint(math.Min(float64(maxResults), float64(maxResultsPerPage))),

@ -20,14 +20,15 @@ import (
type Client struct { type Client struct {
httpClient *http.Client httpClient *http.Client
apiKey string apiKey string
proApiKey string
} }
// NewClient create new client object // NewClient create new client object
func NewClient(httpClient *http.Client, apiKey string) *Client { func NewClient(httpClient *http.Client, apiKey string, proApiKey string) *Client {
if httpClient == nil { if httpClient == nil {
httpClient = http.DefaultClient httpClient = http.DefaultClient
} }
return &Client{httpClient: httpClient, apiKey: apiKey} return &Client{httpClient: httpClient, apiKey: apiKey, proApiKey: proApiKey}
} }
// helper // helper
@ -64,8 +65,11 @@ func (c *Client) getApiUrl(path string, params *url.Values) string {
urlParams = *params urlParams = *params
} }
if c.apiKey != "" { if c.apiKey != "" {
urlParams.Add("x_cg_demo_api_key", c.apiKey)
}
if c.proApiKey != "" {
subdomain = "pro-api" subdomain = "pro-api"
urlParams.Add("x_cg_pro_api_key", c.apiKey) urlParams.Add("x_cg_pro_api_key", c.proApiKey)
} }
url := fmt.Sprintf("https://%s.coingecko.com/api/v3%s?%s", subdomain, path, urlParams.Encode()) url := fmt.Sprintf("https://%s.coingecko.com/api/v3%s?%s", subdomain, path, urlParams.Encode())
return url return url

Loading…
Cancel
Save