diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/Coinex.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/Coinex.java index d73646e5784..07d07ec2e89 100644 --- a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/Coinex.java +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/Coinex.java @@ -13,6 +13,7 @@ import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1; import org.knowm.xchange.coinex.dto.marketdata.CoinexChainInfo; import org.knowm.xchange.coinex.dto.marketdata.CoinexCurrencyPairInfo; +import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo; import org.knowm.xchange.coinex.dto.marketdata.CoinexMarketDepth; import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1; @@ -34,6 +35,12 @@ CoinexResponse allMarketStatistics() CoinexResponse singleMarketStatistics( @QueryParam("market") String market) throws IOException, CoinexException; + @GET + @Path("v2/maintain/info") + CoinexResponse> maintainInfo() + throws IOException, CoinexException; + + @GET @Path("v2/spot/market") CoinexResponse> marketStatus(@QueryParam("market") String markets) diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/config/Config.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/config/Config.java new file mode 100644 index 00000000000..0d999b7866f --- /dev/null +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/config/Config.java @@ -0,0 +1,20 @@ +package org.knowm.xchange.coinex.config; + +import java.time.Clock; +import lombok.Data; + +@Data +public final class Config { + + private Clock clock; + + private static Config instance = new Config(); + + private Config() { + clock = Clock.systemDefaultZone(); + } + + public static Config getInstance() { + return instance; + } +} diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/dto/marketdata/CoinexMaintainInfo.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/dto/marketdata/CoinexMaintainInfo.java new file mode 100644 index 00000000000..b0c186e56bf --- /dev/null +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/dto/marketdata/CoinexMaintainInfo.java @@ -0,0 +1,37 @@ +package org.knowm.xchange.coinex.dto.marketdata; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.net.URI; +import java.time.Instant; +import java.util.List; +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class CoinexMaintainInfo { + + @JsonProperty("started_at") + private Instant startTime; + + @JsonProperty("ended_at") + private Instant endTime; + + @JsonProperty("scope") + private List scope; + + @JsonProperty("announce_enabled") + private Boolean announceEnabled; + + @JsonProperty("announce_url") + private URI announceUrl; + + @JsonProperty("protect_duration_start") + private Instant protectStart; + + @JsonProperty("protect_duration_end") + private Instant protectEnd; + +} diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataService.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataService.java index 985d202633f..80b8bea7def 100644 --- a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataService.java +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataService.java @@ -1,22 +1,27 @@ package org.knowm.xchange.coinex.service; import java.io.IOException; +import java.time.Instant; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.Validate; import org.knowm.xchange.coinex.CoinexAdapters; import org.knowm.xchange.coinex.CoinexErrorAdapter; import org.knowm.xchange.coinex.CoinexExchange; +import org.knowm.xchange.coinex.config.Config; import org.knowm.xchange.coinex.dto.CoinexException; import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1; +import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo; import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1; import org.knowm.xchange.coinex.service.params.CoinexOrderBookParams; import org.knowm.xchange.currency.CurrencyPair; import org.knowm.xchange.dto.marketdata.OrderBook; import org.knowm.xchange.dto.marketdata.Ticker; +import org.knowm.xchange.dto.meta.ExchangeHealth; import org.knowm.xchange.instrument.Instrument; import org.knowm.xchange.service.marketdata.MarketDataService; import org.knowm.xchange.service.marketdata.params.CurrencyPairsParam; @@ -30,6 +35,38 @@ public CoinexMarketDataService(CoinexExchange exchange) { super(exchange); } + @Override + public ExchangeHealth getExchangeHealth() { + try { + List coinexMaintainInfos = getCoinexMaintainInfo(); + + for (CoinexMaintainInfo coinexMaintainInfo: coinexMaintainInfos) { + Instant now = Instant.now(Config.getInstance().getClock()); + if (ObjectUtils.allNotNull(coinexMaintainInfo.getStartTime(), + coinexMaintainInfo.getEndTime())) { + if (now.isAfter(coinexMaintainInfo.getStartTime()) && now.isBefore( + coinexMaintainInfo.getEndTime())) { + return ExchangeHealth.OFFLINE; + } + } + + if (ObjectUtils.allNotNull(coinexMaintainInfo.getProtectStart(), + coinexMaintainInfo.getProtectEnd())) { + if (now.isAfter(coinexMaintainInfo.getProtectStart()) && now.isBefore( + coinexMaintainInfo.getProtectEnd())) { + return ExchangeHealth.OFFLINE; + } + } + } + + } catch (IOException e) { + return ExchangeHealth.OFFLINE; + } + + return ExchangeHealth.ONLINE; + } + + @Override public Ticker getTicker(CurrencyPair currencyPair, Object... args) throws IOException { return getTicker((Instrument) currencyPair, args); diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceRaw.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceRaw.java index 0d48ff84af1..ee0b69a11da 100644 --- a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceRaw.java +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceRaw.java @@ -10,6 +10,7 @@ import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1; import org.knowm.xchange.coinex.dto.marketdata.CoinexChainInfo; import org.knowm.xchange.coinex.dto.marketdata.CoinexCurrencyPairInfo; +import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo; import org.knowm.xchange.coinex.dto.marketdata.CoinexMarketDepth; import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1; import org.knowm.xchange.coinex.service.params.CoinexOrderBookParams; @@ -21,6 +22,12 @@ public CoinexMarketDataServiceRaw(CoinexExchange exchange) { super(exchange); } + + public List getCoinexMaintainInfo() throws IOException { + return coinex.maintainInfo().getData(); + } + + public List getAllCoinexChainInfos() throws IOException { return new ArrayList<>(coinex.allChainInfos().getData().values()); } diff --git a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexTradeService.java b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexTradeService.java index e17e873c5dc..48533755e5b 100644 --- a/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexTradeService.java +++ b/xchange-coinex/src/main/java/org/knowm/xchange/coinex/service/CoinexTradeService.java @@ -39,8 +39,8 @@ public Collection getOrder(OrderQueryParams... orderQueryParams) throws I OrderQueryParamInstrument params = (OrderQueryParamInstrument) orderQueryParams[0]; try { - CoinexOrder gateioOrder = orderStatus(params.getInstrument(), params.getOrderId()); - return Collections.singletonList(CoinexAdapters.toOrder(gateioOrder)); + CoinexOrder order = orderStatus(params.getInstrument(), params.getOrderId()); + return Collections.singletonList(CoinexAdapters.toOrder(order)); } catch (CoinexException e) { throw CoinexErrorAdapter.adapt(e); } diff --git a/xchange-coinex/src/test/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceIntegration.java b/xchange-coinex/src/test/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceIntegration.java index 9de6ac4629c..aaa1ea14335 100644 --- a/xchange-coinex/src/test/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceIntegration.java +++ b/xchange-coinex/src/test/java/org/knowm/xchange/coinex/service/CoinexMarketDataServiceIntegration.java @@ -11,11 +11,18 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.marketdata.OrderBook; import org.knowm.xchange.dto.marketdata.Ticker; +import org.knowm.xchange.dto.meta.ExchangeHealth; class CoinexMarketDataServiceIntegration { CoinexExchange exchange = ExchangeFactory.INSTANCE.createExchange(CoinexExchange.class); + @Test + public void exchange_health() { + assertThat(exchange.getMarketDataService().getExchangeHealth()).isEqualTo(ExchangeHealth.ONLINE); + } + + @Test void valid_tickers() throws IOException { List tickers = exchange.getMarketDataService().getTickers(null); diff --git a/xchange-coinex/src/test/resources/rest/common.http b/xchange-coinex/src/test/resources/rest/common.http new file mode 100644 index 00000000000..b4822bb6134 --- /dev/null +++ b/xchange-coinex/src/test/resources/rest/common.http @@ -0,0 +1,4 @@ +### Get Maintenance Information +GET {{api_host}}/v2/maintain/info + +