]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/market_config.go
Set poloniex client timeout to 20 seconds.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / market_config.go
index 81a92d1e6d35fb476b9ddd79ff97e6f487347122..155da1abc12fcdf1e022c688396c2a716f787fd9 100644 (file)
@@ -5,9 +5,9 @@ import (
        "strings"
        "time"
 
+       "git.immae.eu/Cryptoportfolio/Front.git/db"
+       "git.immae.eu/Cryptoportfolio/Front.git/markets"
        "github.com/jloup/utils"
-       "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
-       "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets"
 )
 
 type MarketConfigQuery struct {
@@ -75,12 +75,12 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
                return nil, NewInternalError(err)
        }
 
-       if config.Config["key"] == "" || config.Config["secret"] == "" {
-               return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
+       if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
+               return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
        }
 
-       resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error {
-               err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"])
+       resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
+               err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)
 
                if utils.ErrIs(err, markets.InvalidCredentials) {
                        return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
@@ -97,8 +97,21 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
                return nil
        })
 
-       if resultErr != nil {
-               return nil, resultErr
+       var newStatus db.MarketConfigStatus = config.Status
+
+       if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
+               newStatus = db.MarketConfigInvalidCredentials
+       } else if resultErr != nil {
+               return nil, NewInternalError(resultErr)
+       } else if resultErr != nil {
+               return nil, NewInternalError(resultErr)
+       }
+
+       if newStatus != config.Status {
+               config, err = db.SetMarketConfigStatus(*config, newStatus)
+               if err != nil {
+                       return nil, NewInternalError(err)
+               }
        }
 
        return nil, nil
@@ -133,7 +146,38 @@ func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) {
                configMap["secret"] = q.In.Secret
        }
 
-       _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+       marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap)
+       if err != nil {
+               return nil, NewInternalError(err)
+       }
+
+       resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
+               err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"], EXTERNAL_SERVICE_TIMEOUT_SECONDS)
+
+               if utils.ErrIs(err, markets.InvalidCredentials) {
+                       return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)}
+               }
+
+               if utils.ErrIs(err, markets.IPRestricted) {
+                       return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)}
+               }
+
+               if err != nil {
+                       return NewInternalError(err)
+               }
+
+               return nil
+       })
+
+       var newStatus db.MarketConfigStatus = db.MarketConfigEnabled
+
+       if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) {
+               newStatus = db.MarketConfigInvalidCredentials
+       } else if resultErr != nil {
+               return nil, NewInternalError(resultErr)
+       }
+
+       marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus)
        if err != nil {
                return nil, NewInternalError(err)
        }