From c6aa553f48d1014f651441bbd9e023508d0a819c Mon Sep 17 00:00:00 2001 From: jloup Date: Sun, 13 May 2018 14:10:21 +0100 Subject: Load credentials only when user requests it. --- api/const.go | 3 ++- api/const_string.go | 12 ++++++------ api/market_config.go | 2 +- cmd/web/js/account.jsx | 46 +++++++++++++++++++++------------------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/api/const.go b/api/const.go index 1f15c6e..2ad4d61 100644 --- a/api/const.go +++ b/api/const.go @@ -19,6 +19,7 @@ const ( InvalidCredentials InvalidEmail InvalidMarketCredentials + MarketCredentialsNotConfigured IPRestrictedApiKey InvalidOtp InvalidPassword @@ -36,7 +37,7 @@ func StatusToHttpCode(status Status, code ErrorCode) int { } switch code { - case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey: + case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured: return http.StatusBadRequest case InvalidCredentials, InvalidOtp: diff --git a/api/const_string.go b/api/const_string.go index 58ed230..5af7574 100644 --- a/api/const_string.go +++ b/api/const_string.go @@ -1,8 +1,8 @@ -// Code generated by "stringer -type=Status,ErrorCode -output const_string.go"; DO NOT EDIT +// Code generated by "stringer -type=Status,ErrorCode -output const_string.go"; DO NOT EDIT. package api -import "fmt" +import "strconv" const _Status_name = "OKNOK" @@ -10,19 +10,19 @@ var _Status_index = [...]uint8{0, 2, 5} func (i Status) String() string { if i >= Status(len(_Status_index)-1) { - return fmt.Sprintf("Status(%d)", i) + return "Status(" + strconv.FormatInt(int64(i), 10) + ")" } return _Status_name[_Status_index[i]:_Status_index[i+1]] } -const _ErrorCode_name = "BadRequestEmailExistsExternalServiceTimeoutInternalErrorInvalidCredentialsInvalidEmailInvalidMarketCredentialsIPRestrictedApiKeyInvalidOtpInvalidPasswordNeedOtpValidationNotAuthorizedNotFoundOtpAlreadySetupOtpNotSetupUserNotConfirmed" +const _ErrorCode_name = "BadRequestEmailExistsExternalServiceTimeoutInternalErrorInvalidCredentialsInvalidEmailInvalidMarketCredentialsMarketCredentialsNotConfiguredIPRestrictedApiKeyInvalidOtpInvalidPasswordNeedOtpValidationNotAuthorizedNotFoundOtpAlreadySetupOtpNotSetupUserNotConfirmed" -var _ErrorCode_index = [...]uint8{0, 10, 21, 43, 56, 74, 86, 110, 128, 138, 153, 170, 183, 191, 206, 217, 233} +var _ErrorCode_index = [...]uint16{0, 10, 21, 43, 56, 74, 86, 110, 140, 158, 168, 183, 200, 213, 221, 236, 247, 263} func (i ErrorCode) String() string { i -= 3 if i >= ErrorCode(len(_ErrorCode_index)-1) { - return fmt.Sprintf("ErrorCode(%d)", i+3) + return "ErrorCode(" + strconv.FormatInt(int64(i+3), 10) + ")" } return _ErrorCode_name[_ErrorCode_index[i]:_ErrorCode_index[i+1]] } diff --git a/api/market_config.go b/api/market_config.go index e7b2341..09eb8a9 100644 --- a/api/market_config.go +++ b/api/market_config.go @@ -76,7 +76,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { } 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)} + 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 { diff --git a/cmd/web/js/account.jsx b/cmd/web/js/account.jsx index 03ca117..c80be33 100644 --- a/cmd/web/js/account.jsx +++ b/cmd/web/js/account.jsx @@ -4,17 +4,20 @@ import React from 'react'; class PoloniexConfiguration extends React.Component { constructor(props) { super(props); - this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'status': 'loading'}; + this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'status': 'loading', 'editMode': false}; } checkCredentials = () => { Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) { + this.setState({'apiRequested': true}); 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'}); + } else if (err.code === 'market_credentials_not_configured') { + this.setState({'status': 'emptyCredentials'}); } return; } @@ -28,7 +31,7 @@ class PoloniexConfiguration extends React.Component { } handleCredentialsSubmit = () => { - this.setState({'status': 'loading'}); + this.setState({'status': 'loading', 'editMode': false}); Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { if (err) { console.error(err, data); @@ -40,14 +43,17 @@ class PoloniexConfiguration extends React.Component { return; } - this.setState({'status': 'loading'}); this.checkCredentials(); }.bind(this)); } componentDidMount = () => { - Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) { - this.setState({'apiRequested': true}); + this.checkCredentials(); + } + + onEditClick = () => { + Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) { + this.setState({'apiRequested': true, 'editMode': true}); if (err) { console.error(err, data); return; @@ -56,8 +62,6 @@ class PoloniexConfiguration extends React.Component { var newStatus = this.state.status; if (!data.key || !data.secret) { newStatus = 'emptyCredentials'; - } else { - this.checkCredentials(); } this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'status': newStatus}); @@ -97,21 +101,17 @@ class PoloniexConfiguration extends React.Component { apiSecret={this.state.apiSecret} apiKey={this.state.apiKey} status={this.state.status} - statusMessage={displayText}/> + statusMessage={displayText} + editMode={this.state.editMode} + onEditClick={this.onEditClick}/> ); } } class PoloniexCredentialsForm extends React.Component { - constructor(props) { - super(props); - this.state = {'editMode': false}; - } - handleSubmit = (e) => { this.props.onCredentialsSubmit(); - this.setState({'editMode': false}); e.preventDefault(); } @@ -123,15 +123,11 @@ class PoloniexCredentialsForm extends React.Component { this.props.onCredentialsChange(this.props.apiKey, event.target.value); } - onEditClick = () => { - this.setState({'editMode': true}); - } - render = () => { - var submitType = this.state.editMode === true ? 'submit' : 'hidden'; - var buttonDisplay = this.state.editMode === true ? 'none' : 'inline'; - var secretDisplayed = this.state.editMode === true ? this.props.apiSecret : 'XXXXXXX'; - var keyDisplayed = this.state.editMode === true ? this.props.apiKey : 'XXXXXXX'; + var submitType = this.props.editMode === true ? 'submit' : 'hidden'; + var buttonDisplay = this.props.editMode === true ? 'none' : 'inline'; + var secretDisplayed = this.props.editMode === true ? this.props.apiSecret : 'XXXXXXX'; + var keyDisplayed = this.props.editMode === true ? this.props.apiKey : 'XXXXXXX'; var iconName = 'icon-cancel-circled'; switch (this.props.status) { @@ -157,13 +153,13 @@ class PoloniexCredentialsForm extends React.Component {
- +
-- cgit v1.2.3