X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Fmarket_config.go;h=e02c3ba48f5f44fdff6abedf8a7d76f98ef10e83;hb=refs%2Ftags%2Fv0.0.17;hp=3fd10ae000d19fba89f6e86b4911a1dde21bf294;hpb=7a9e5112eaaea58d55f181d3e5296e4ff839921c;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/market_config.go b/api/market_config.go index 3fd10ae..e02c3ba 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -2,8 +2,12 @@ package api import ( "fmt" + "strings" + "time" - "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" + "github.com/jloup/utils" + "git.immae.eu/Cryptoportfolio/Front.git/db" + "git.immae.eu/Cryptoportfolio/Front.git/markets" ) type MarketConfigQuery struct { @@ -39,9 +43,67 @@ func (q MarketConfigQuery) Run() (interface{}, *Error) { } + if _, ok := config.Config["key"]; !ok { + config.Config["key"] = "" + } + + if _, ok := config.Config["secret"]; !ok { + config.Config["secret"] = "" + } + return config.Config, nil } +type TestMarketCredentialsQuery struct { + In struct { + User db.User + Market string + } +} + +func (q TestMarketCredentialsQuery) ValidateParams() *Error { + if q.In.Market != "poloniex" { + return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} + } + + return nil +} + +func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { + config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) + if err != nil { + return nil, NewInternalError(err) + } + + 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' 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) { + 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 + }) + + if resultErr != nil { + return nil, NewInternalError(resultErr) + } + + return nil, nil +} + type UpdateMarketConfigQuery struct { In struct { User db.User @@ -56,6 +118,9 @@ func (q UpdateMarketConfigQuery) ValidateParams() *Error { return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} } + q.In.Secret = strings.TrimSpace(q.In.Secret) + q.In.Key = strings.TrimSpace(q.In.Key) + return nil } @@ -68,10 +133,74 @@ 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) } return nil, nil } + +type GetPortfolioQuery struct { + In struct { + User db.User + Market string + } +} + +func (q GetPortfolioQuery) ValidateParams() *Error { + if q.In.Market != "poloniex" { + return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} + } + + return nil +} + +func (q GetPortfolioQuery) Run() (interface{}, *Error) { + marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) + if err != nil { + return nil, NewInternalError(err) + } + + report, err := GetWeekPortfolio(*marketConfig) + if ErrorIs(err, NotFound) { + return nil, err.(*Error) + } + + if err != nil { + return nil, NewInternalError(err) + } + + return report.Round(), nil +}