diff options
author | jloup <jeanloup.jamet@gmail.com> | 2018-05-10 16:22:29 +0200 |
---|---|---|
committer | jloup <jeanloup.jamet@gmail.com> | 2018-05-10 16:22:29 +0200 |
commit | 299b6b6d9fb879c06e675ef240f361348629ff6c (patch) | |
tree | 27909cd54e53d58e612da93e15f1005af86870ec /api | |
parent | 3b8833854f83f75e3d16c1fdb869937f690e48ea (diff) | |
download | Front-299b6b6d9fb879c06e675ef240f361348629ff6c.tar.gz Front-299b6b6d9fb879c06e675ef240f361348629ff6c.tar.zst Front-299b6b6d9fb879c06e675ef240f361348629ff6c.zip |
Add column 'status' to market_configs.v0.0.9
Diffstat (limited to 'api')
-rw-r--r-- | api/api.go | 3 | ||||
-rw-r--r-- | api/external_services.go | 4 | ||||
-rw-r--r-- | api/market_config.go | 39 |
3 files changed, 40 insertions, 6 deletions
@@ -58,6 +58,9 @@ func ErrorIs(err error, code ErrorCode) bool { | |||
58 | } | 58 | } |
59 | 59 | ||
60 | func NewInternalError(err error) *Error { | 60 | func NewInternalError(err error) *Error { |
61 | if apiError, ok := err.(*Error); ok { | ||
62 | return apiError | ||
63 | } | ||
61 | return &Error{InternalError, "internal error", err} | 64 | return &Error{InternalError, "internal error", err} |
62 | } | 65 | } |
63 | 66 | ||
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 ( | |||
7 | ) | 7 | ) |
8 | 8 | ||
9 | // Use this to call external services. It will handle timeout and request cancellation gracefully. | 9 | // Use this to call external services. It will handle timeout and request cancellation gracefully. |
10 | func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error { | 10 | func CallExternalService(tag string, timeout time.Duration, routine func() error) error { |
11 | routineDone := make(chan *Error) | 11 | routineDone := make(chan error) |
12 | 12 | ||
13 | go func() { | 13 | go func() { |
14 | routineDone <- routine() | 14 | 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) { | |||
75 | return nil, NewInternalError(err) | 75 | return nil, NewInternalError(err) |
76 | } | 76 | } |
77 | 77 | ||
78 | if config.Config["key"] == "" || config.Config["secret"] == "" { | 78 | if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" { |
79 | return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} | 79 | return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} |
80 | } | 80 | } |
81 | 81 | ||
82 | resultErr := CallExternalService(fmt.Sprintf("'%s' GetBalanceValue", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() *Error { | 82 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { |
83 | err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) | 83 | err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) |
84 | 84 | ||
85 | if utils.ErrIs(err, markets.InvalidCredentials) { | 85 | if utils.ErrIs(err, markets.InvalidCredentials) { |
@@ -98,7 +98,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { | |||
98 | }) | 98 | }) |
99 | 99 | ||
100 | if resultErr != nil { | 100 | if resultErr != nil { |
101 | return nil, resultErr | 101 | return nil, NewInternalError(resultErr) |
102 | } | 102 | } |
103 | 103 | ||
104 | return nil, nil | 104 | return nil, nil |
@@ -133,7 +133,38 @@ func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) { | |||
133 | configMap["secret"] = q.In.Secret | 133 | configMap["secret"] = q.In.Secret |
134 | } | 134 | } |
135 | 135 | ||
136 | _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) | 136 | marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) |
137 | if err != nil { | ||
138 | return nil, NewInternalError(err) | ||
139 | } | ||
140 | |||
141 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { | ||
142 | err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"]) | ||
143 | |||
144 | if utils.ErrIs(err, markets.InvalidCredentials) { | ||
145 | return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)} | ||
146 | } | ||
147 | |||
148 | if utils.ErrIs(err, markets.IPRestricted) { | ||
149 | return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} | ||
150 | } | ||
151 | |||
152 | if err != nil { | ||
153 | return NewInternalError(err) | ||
154 | } | ||
155 | |||
156 | return nil | ||
157 | }) | ||
158 | |||
159 | var newStatus db.MarketConfigStatus = db.MarketConfigEnabled | ||
160 | |||
161 | if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) { | ||
162 | newStatus = db.MarketConfigInvalidCredentials | ||
163 | } else if resultErr != nil { | ||
164 | return nil, NewInternalError(resultErr) | ||
165 | } | ||
166 | |||
167 | marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus) | ||
137 | if err != nil { | 168 | if err != nil { |
138 | return nil, NewInternalError(err) | 169 | return nil, NewInternalError(err) |
139 | } | 170 | } |