use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.
the class CoinfloorTradeServiceRaw method placeMarketOrder.
public CoinfloorMarketOrderResponse placeMarketOrder(CurrencyPair pair, OrderType side, BigDecimal amount) throws IOException {
Currency base = normalise(pair.base);
Currency counter = normalise(pair.counter);
try {
if (side == OrderType.BID) {
return coinfloor.buyMarket(base, counter, amount);
} else {
return coinfloor.sellMarket(base, counter, amount);
}
} catch (HttpStatusIOException e) {
if (e.getHttpStatusCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
// contains the message: "quantity" parameter must not have more than 4 fractional digits
throw new ExchangeException(e.getHttpBody(), e);
} else {
throw e;
}
}
}
use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.
the class CoinfloorTradeServiceRaw method placeLimitOrder.
public CoinfloorOrder placeLimitOrder(CurrencyPair pair, OrderType side, BigDecimal amount, BigDecimal price) throws IOException {
Currency base = normalise(pair.base);
Currency counter = normalise(pair.counter);
try {
if (side == OrderType.BID) {
return coinfloor.buy(base, counter, amount, price);
} else {
return coinfloor.sell(base, counter, amount, price);
}
} catch (HttpStatusIOException e) {
if (e.getHttpStatusCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
// contains the message: "amount" parameter must not have more than 4 fractional digits
throw new ExchangeException(e.getHttpBody(), e);
} else {
throw e;
}
}
}
use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.
the class QuoineTradeServiceRaw method placeLimitOrder.
public QuoineOrderResponse placeLimitOrder(CurrencyPair currencyPair, String type, BigDecimal originalAmount, BigDecimal price) throws IOException {
int productId = productId(currencyPair);
QuoineNewOrderRequest quoineNewOrderRequest = useMargin ? new QuoineNewMarginOrderRequest("limit", productId, type, originalAmount, price, leverageLevel, currencyPair.counter.getCurrencyCode()) : new QuoineNewOrderRequest("limit", productId, type, originalAmount, price);
try {
return quoine.placeOrder(QUOINE_API_VERSION, signatureCreator, contentType, new QuoineNewOrderRequestWrapper(quoineNewOrderRequest));
} catch (HttpStatusIOException e) {
throw handleHttpError(e);
}
}
use of si.mazi.rescu.HttpStatusIOException in project cassandre-trading-bot by cassandre-tech.
the class ExchangeAutoConfiguration method configure.
/**
* Instantiating the exchange services based on user parameters.
*/
@PostConstruct
public void configure() {
try {
// Instantiate exchange class.
Class<? extends Exchange> exchangeClass = Class.forName(getExchangeClassName()).asSubclass(Exchange.class);
ExchangeSpecification exchangeSpecification = new ExchangeSpecification(exchangeClass);
// Exchange configuration.
exchangeSpecification.setUserName(exchangeParameters.getUsername());
exchangeSpecification.setApiKey(exchangeParameters.getKey());
exchangeSpecification.setSecretKey(exchangeParameters.getSecret());
exchangeSpecification.getResilience().setRateLimiterEnabled(true);
exchangeSpecification.setExchangeSpecificParametersItem("Use_Sandbox", exchangeParameters.getModes().getSandbox());
exchangeSpecification.setExchangeSpecificParametersItem("passphrase", exchangeParameters.getPassphrase());
exchangeSpecification.setProxyHost(exchangeParameters.getProxyHost());
exchangeSpecification.setProxyPort(exchangeParameters.getProxyPort());
exchangeSpecification.setSslUri(exchangeParameters.getSslUri());
exchangeSpecification.setPlainTextUri(exchangeParameters.getPlainTextUri());
exchangeSpecification.setHost(exchangeParameters.getHost());
if (exchangeParameters.getPort() != null) {
exchangeSpecification.setPort(Integer.parseInt(exchangeParameters.getPort()));
}
// Creates XChange services.
xChangeExchange = ExchangeFactory.INSTANCE.createExchange(exchangeSpecification);
xChangeAccountService = xChangeExchange.getAccountService();
xChangeMarketDataService = xChangeExchange.getMarketDataService();
xChangeTradeService = xChangeExchange.getTradeService();
// Force login to check credentials.
logger.info("Exchange connection with driver {}", exchangeParameters.getDriverClassName());
xChangeAccountService.getAccountInfo();
logger.info("Exchange connection successful with username {} (Dry mode: {} / Sandbox: {})", exchangeParameters.getUsername(), exchangeParameters.getModes().getDry(), exchangeParameters.getModes().getSandbox());
} catch (ClassNotFoundException e) {
// If we can't find the exchange class.
throw new ConfigurationException("Impossible to find the exchange driver class you requested: " + exchangeParameters.getDriverClassName(), "Choose and configure a valid exchange (https://trading-bot.cassandre.tech/learn/exchange-connection-configuration.html#how-does-it-works)");
} catch (HttpStatusIOException e) {
if (e.getHttpStatusCode() == UNAUTHORIZED_STATUS_CODE) {
// Authorization failure.
throw new ConfigurationException("Invalid credentials for " + exchangeParameters.getDriverClassName(), "Check your exchange credentials: " + e.getMessage() + " - login used: " + exchangeParameters.getUsername());
} else {
// Another HTTP failure.
throw new ConfigurationException("Error while connecting to the exchange: " + e.getMessage());
}
} catch (IOException e) {
throw new ConfigurationException("IO error: " + e.getMessage());
}
}
use of si.mazi.rescu.HttpStatusIOException in project XChange by knowm.
the class CmcErrorAdapter method adapt.
/**
* Parse errors from HTTP exceptions
*/
public static void adapt(HttpStatusIOException httpStatusException) {
String msg = "HTTP Status: " + httpStatusException.getHttpStatusCode();
// if we have a HTTP body try to parse more error details from body
if (isNotEmpty(httpStatusException.getHttpBody())) {
ObjectMapper mapper = new ObjectMapper();
CmcResult result;
try {
result = mapper.readValue(httpStatusException.getHttpBody(), CmcResult.class);
} catch (Exception e) {
// but ignore errors on parsing and throw generic ExchangeException instead
throw new ExchangeException(msg, httpStatusException);
}
// but if it contains a parsable result, then try to parse errors from result:
if (result.getStatus() != null && isNotEmpty(result.getStatus().getErrorMessage()) && !result.isSuccess()) {
String error = result.getStatus().getErrorMessage();
if (result.getStatus().getErrorCode() == 401) {
throw new ExchangeSecurityException(error);
}
if (result.getStatus().getErrorCode() == 402) {
throw new FundsExceededException(error);
}
if (result.getStatus().getErrorCode() == 429) {
throw new FrequencyLimitExceededException(error);
}
msg = error + " - ErrorCode: " + result.getStatus().getErrorCode();
throw new ExchangeException(msg);
}
}
// else: just throw ExchangeException with causing Exception
throw new ExchangeException(msg, httpStatusException);
}
Aggregations