From: jloup Date: Thu, 10 May 2018 14:22:29 +0000 (+0200) Subject: Add column 'status' to market_configs. X-Git-Tag: v0.0.9 X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git;a=commitdiff_plain;h=299b6b6d9fb879c06e675ef240f361348629ff6c Add column 'status' to market_configs. --- diff --git a/api/api.go b/api/api.go index ece2a26..79a13a5 100644 --- a/api/api.go +++ b/api/api.go @@ -58,6 +58,9 @@ func ErrorIs(err error, code ErrorCode) bool { } func NewInternalError(err error) *Error { + if apiError, ok := err.(*Error); ok { + return apiError + } return &Error{InternalError, "internal error", err} } diff --git a/api/external_services.go b/api/external_services.go index c467171..41f4d87 100644 --- a/api/external_services.go +++ b/api/external_services.go @@ -7,8 +7,8 @@ import ( ) // Use this to call external services. It will handle timeout and request cancellation gracefully. -func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error { - routineDone := make(chan *Error) +func CallExternalService(tag string, timeout time.Duration, routine func() error) error { + routineDone := make(chan error) go func() { routineDone <- routine() diff --git a/api/market_config.go b/api/market_config.go index 81a92d1..e7b2341 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -75,11 +75,11 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { return nil, NewInternalError(err) } - if config.Config["key"] == "" || config.Config["secret"] == "" { + if config == nil || 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)} } - resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error { + 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"]) if utils.ErrIs(err, markets.InvalidCredentials) { @@ -98,7 +98,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { }) if resultErr != nil { - return nil, resultErr + return nil, NewInternalError(resultErr) } return nil, nil @@ -133,7 +133,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"]) + + 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) { + newStatus = db.MarketConfigInvalidCredentials + } else if resultErr != nil { + return nil, NewInternalError(resultErr) + } + + marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus) if err != nil { return nil, NewInternalError(err) } diff --git a/cmd/web/js/account.jsx b/cmd/web/js/account.jsx index d30abe7..03ca117 100644 --- a/cmd/web/js/account.jsx +++ b/cmd/web/js/account.jsx @@ -28,9 +28,15 @@ class PoloniexConfiguration extends React.Component { } handleCredentialsSubmit = () => { + this.setState({'status': 'loading'}); Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { if (err) { console.error(err, data); + if (err.code === 'invalid_market_credentials') { + this.setState({'status': 'invalidCredentials'}); + } else if (err.code === 'ip_restricted_api_key') { + this.setState({'status': 'ipRestricted'}); + } return; } diff --git a/db/market_config.go b/db/market_config.go index b26c092..30b4538 100644 --- a/db/market_config.go +++ b/db/market_config.go @@ -1,11 +1,20 @@ package db -import "github.com/go-pg/pg" +import ( + "github.com/go-pg/pg" +) + +type MarketConfigStatus string + +const MarketConfigEnabled = "enabled" +const MarketConfigDisabled = "disabled" +const MarketConfigInvalidCredentials = "invalid_credentials" type MarketConfig struct { Id int64 - MarketName string `sql:",notnull"` - UserId int64 `sql:",notnull"` + MarketName string + UserId int64 + Status MarketConfigStatus Config map[string]string } @@ -43,3 +52,13 @@ func SetUserMarketConfig(userId int64, market string, newConfig map[string]strin return &config, err } + +func SetMarketConfigStatus(marketConfig MarketConfig, status MarketConfigStatus) (*MarketConfig, error) { + marketConfig.Status = status + _, err := DB.Model(&marketConfig). + OnConflict("(user_id, market_name) DO UPDATE"). + Set("status = ?", status). + Insert() + + return &marketConfig, err +} diff --git a/db/migrations.go b/db/migrations.go index f6ecb60..f0df49c 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -78,4 +78,15 @@ var migrations []Migration = []Migration{ }, Down: []string{"DROP VIEW view_balances", "DROP INDEX checkpoints_idx"}, }, + { + Version: 201805101000, + Up: []string{ + "CREATE TYPE market_config_status AS ENUM ('enabled', 'disabled', 'invalid_credentials')", + "ALTER TABLE market_configs ADD status market_config_status NOT NULL DEFAULT 'disabled'", + }, + Down: []string{ + "ALTER TABLE market_configs DROP COLUMN status", + "DROP TYPE market_config_status", + }, + }, } diff --git a/markets/poloniex.go b/markets/poloniex.go index 58645b2..cadc829 100644 --- a/markets/poloniex.go +++ b/markets/poloniex.go @@ -20,7 +20,7 @@ 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 { @@ -65,7 +65,7 @@ func (p *Poloniex) TestCredentials(apiKey, apiSecret string) error { return utils.Error{IPRestricted, "IP restricted api key"} } - return nil + return err } func (p *Poloniex) GetBalance(apiKey, apiSecret string) (Summary, error) {