]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - markets/poloniex.go
Set poloniex client timeout to 20 seconds.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / markets / poloniex.go
index 9aaeafbfcb1cedc3f6812bb0cd34d61a97ff160d..595d5a4890ac73001f37c2a20f9cf4ba5abe18eb 100644 (file)
@@ -16,11 +16,13 @@ var (
        IPRestricted                          = utils.InitFlag(&ErrorFlagCounter, "IPRestricted")
 )
 
+const defaultTimeout = 10
+
 func poloniexInvalidCredentialsError(err error) bool {
        if err == nil {
                return false
        }
-       return strings.Contains(err.Error(), "Invalid API key/secret pair")
+       return strings.Contains(err.Error(), "Invalid API key/secret pair") || strings.Contains(err.Error(), "Set the API KEY and API SECRET")
 }
 
 func poloniexRestrictedIPError(err error) bool {
@@ -43,7 +45,7 @@ type Poloniex struct {
 }
 
 func NewPoloniex() *Poloniex {
-       client, _ := poloniex.NewClient("", "")
+       client, _ := poloniex.NewClient("", "", defaultTimeout)
 
        return &Poloniex{
                TickerCache:      make(map[string]CurrencyPair),
@@ -52,8 +54,24 @@ func NewPoloniex() *Poloniex {
        }
 }
 
-func (p *Poloniex) GetBalance(apiKey, apiSecret string) (Summary, error) {
-       client, _ := poloniex.NewClient(apiKey, apiSecret)
+func (p *Poloniex) TestCredentials(apiKey, apiSecret string, timeout int32) error {
+       client, _ := poloniex.NewClient(apiKey, apiSecret, timeout)
+
+       _, err := client.TradeReturnDepositAdresses()
+
+       if poloniexInvalidCredentialsError(err) {
+               return utils.Error{InvalidCredentials, "invalid poloniex credentials"}
+       }
+
+       if poloniexRestrictedIPError(err) {
+               return utils.Error{IPRestricted, "IP restricted api key"}
+       }
+
+       return err
+}
+
+func (p *Poloniex) GetBalance(apiKey, apiSecret string, timeout int32) (Summary, error) {
+       client, _ := poloniex.NewClient(apiKey, apiSecret, timeout)
        var summary Summary
 
        accounts, err := client.TradeReturnAvailableAccountBalances()