Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More specific types to JSON POJOs #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
@Getter
public class FundingRateEntry {
private String symbol;
private String fundingRate;
private String fundingRateTimestamp;
private double fundingRate;
private long fundingRateTimestamp;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
@JsonFormat(shape = JsonFormat.Shape.ARRAY)
public class MarketKlineEntry {
private long startTime;
private String openPrice;
private String highPrice;
private String lowPrice;
private String closePrice;
private String volume;
private String turnover;
private double openPrice;
private double highPrice;
private double lowPrice;
private double closePrice;
private double volume;
private double turnover;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class ServerTimeResult {
@JsonProperty("timeSecond")
private String timeSecond;
private long timeSecond;
@JsonProperty("timeNano")
private String timeNano;
private long timeNano;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.bybit.api.client.restApi.BybitApiCallback;
import com.bybit.api.client.restApi.BybitApiService;
import com.bybit.api.client.domain.market.request.MarketDataRequest;
import com.bybit.api.client.domain.GenericResponse;
import com.bybit.api.client.domain.market.response.serverTime.ServerTimeResult;

import static com.bybit.api.client.service.BybitApiServiceGenerator.createService;

Expand All @@ -15,7 +17,7 @@ public BybitApiMarketAsyncRestClientImpl(String baseUrl, boolean debugMode, long
}
// Market Data endpoints
@Override
public void getServerTime(BybitApiCallback<Object> callback) {
public void getServerTime(BybitApiCallback<GenericResponse<ServerTimeResult>> callback) {
bybitApiService.getServerTime().enqueue(new BybitApiCallbackAdapter<>(callback));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bybit.api.client.impl;

import com.bybit.api.client.domain.GenericResponse;
import com.bybit.api.client.domain.market.response.serverTime.ServerTimeResult;
import com.bybit.api.client.restApi.BybitApiMarketRestClient;
import com.bybit.api.client.restApi.BybitApiService;
import com.bybit.api.client.domain.market.request.MarketDataRequest;
Expand Down Expand Up @@ -29,7 +31,7 @@ public Object getAnnouncementInfo(MarketDataRequest announcementInfoRequest) {
}

@Override
public Object getServerTime() {
public GenericResponse<ServerTimeResult> getServerTime() {
return executeSync(
bybitApiService.getServerTime());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.bybit.api.client.restApi;

import com.bybit.api.client.domain.announcement.request.AnnouncementInfoRequest;
import com.bybit.api.client.domain.GenericResponse;
import com.bybit.api.client.domain.market.request.MarketDataRequest;
import com.bybit.api.client.domain.market.response.serverTime.ServerTimeResult;

public interface BybitApiAsyncMarketDataRestClient {
// Market endpoints
void getServerTime(BybitApiCallback<Object> callback);
void getServerTime(BybitApiCallback<GenericResponse<ServerTimeResult>> callback);
void getMarketLinesData(MarketDataRequest marketKlineRequest, BybitApiCallback<Object> callback);
void getMarketPriceLinesData(MarketDataRequest marketKlineRequest, BybitApiCallback<Object> callback);
void getIndexPriceLinesData(MarketDataRequest marketKlineRequest, BybitApiCallback<Object> callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.bybit.api.client.restApi;

import com.bybit.api.client.domain.announcement.request.AnnouncementInfoRequest;
import com.bybit.api.client.domain.GenericResponse;
import com.bybit.api.client.domain.market.request.MarketDataRequest;
import com.bybit.api.client.domain.market.response.serverTime.ServerTimeResult;

public interface BybitApiMarketRestClient {
// Market Data
Object getServerTime();
GenericResponse<ServerTimeResult> getServerTime();
Object getMarketLinesData(MarketDataRequest marketKlineRequest);
Object getMarketPriceLinesData(MarketDataRequest marketKlineRequest);
Object getIndexPriceLinesData(MarketDataRequest marketKlineRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.bybit.api.client.restApi;

import com.bybit.api.client.constant.BybitApiConstants;
import com.bybit.api.client.domain.GenericResponse;
import com.bybit.api.client.domain.account.request.*;
import com.bybit.api.client.domain.asset.request.*;
import com.bybit.api.client.domain.broker.request.BrokerGetIssuedVoucherRequest;
import com.bybit.api.client.domain.broker.request.BrokerIssueVoucherRequest;
import com.bybit.api.client.domain.broker.request.BrokerVoucherSpecRequest;
import com.bybit.api.client.domain.institution.clientLending.ClientLendingFundsRequest;
import com.bybit.api.client.domain.institution.insLending.UpdateInstitutionLoadUidRequest;
import com.bybit.api.client.domain.market.response.serverTime.ServerTimeResult;
import com.bybit.api.client.domain.position.request.ConfirmNewRiskLimitRequest;
import com.bybit.api.client.domain.position.request.*;
import com.bybit.api.client.domain.spot.leverageToken.SpotLeverageTokenRequest;
Expand Down Expand Up @@ -41,7 +43,7 @@ public interface BybitApiService {
* timeNano string Bybit server timestamp (nano)
*/
@GET("/v5/market/time")
Call<Object> getServerTime();
Call<GenericResponse<ServerTimeResult>> getServerTime();

/**
* Get Kline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static <S> S createService(Class<S> serviceClass, String apiKey, String s
/**
* Execute a REST call and block until the response is received.
*/
public static <T> Object executeSync(Call<T> call) {
public static <T> T executeSync(Call<T> call) {
try {
var response = call.execute();
if (response.isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import static org.junit.Assert.fail;

public class FundingRateResultDeserializerTest {
double delta = 1e-6;

@Test
public void testFundingRateResultDeserializer()
{
Expand Down Expand Up @@ -50,8 +52,8 @@ public void testFundingRateResultDeserializer()
List<FundingRateEntry> entries = fundingRateResult.getFundingRateEntries();
assertEquals(entries.size(), 2);
assertEquals(entries.get(0).getSymbol(), "BTCUSDT");
assertEquals(entries.get(0).getFundingRate(), "0.00020343");
assertEquals(entries.get(0).getFundingRateTimestamp(), "1699776000000");
assertEquals(entries.get(0).getFundingRate(), 0.00020343, delta);
assertEquals(entries.get(0).getFundingRateTimestamp(), 1699776000000L);
}catch (IOException e) {
System.out.println(e.getMessage());
fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.Assert.fail;

public class MarketKlineResultDeserializerTest {
double delta = 1e-6;

@Test
public void testMarketKlineResultDeserializer() {
Expand Down Expand Up @@ -61,19 +62,19 @@ public void testMarketKlineResultDeserializer() {
List<MarketKlineEntry> entries = marketKlineResult.getMarketKlineEntries();
assertEquals(entries.size(), 2);
assertEquals(Optional.of(entries.get(0).getStartTime()).get(), Long.valueOf("1691971200000"));
assertEquals(entries.get(0).getOpenPrice(), "28349.56");
assertEquals(entries.get(0).getHighPrice(), "29752.1178");
assertEquals(entries.get(0).getLowPrice(), "18000");
assertEquals(entries.get(0).getClosePrice(), "21335");
assertEquals(entries.get(0).getVolume(), "10531.829385");
assertEquals(entries.get(0).getTurnover(), "290473145.2723997623");
assertEquals(entries.get(0).getOpenPrice(), 28349.56, delta);
assertEquals(entries.get(0).getHighPrice(), 29752.1178, delta);
assertEquals(entries.get(0).getLowPrice(), 18000.0, delta);
assertEquals(entries.get(0).getClosePrice(), 21335.0, delta);
assertEquals(entries.get(0).getVolume(), 10531.829385, delta);
assertEquals(entries.get(0).getTurnover(), 290473145.2723997623, delta);
assertEquals(Optional.of(entries.get(1).getStartTime()).get(), Long.valueOf("1691366400000"));
assertEquals(entries.get(1).getOpenPrice(), "31847.42");
assertEquals(entries.get(1).getHighPrice(), "31895.5621");
assertEquals(entries.get(1).getLowPrice(), "6331.63");
assertEquals(entries.get(1).getClosePrice(), "28349.56");
assertEquals(entries.get(1).getVolume(), "7519.172778");
assertEquals(entries.get(1).getTurnover(), "188453792.2371207168");
assertEquals(entries.get(1).getOpenPrice(), 31847.42, delta);
assertEquals(entries.get(1).getHighPrice(), 31895.5621, delta);
assertEquals(entries.get(1).getLowPrice(), 6331.63, delta);
assertEquals(entries.get(1).getClosePrice(), 28349.56, delta);
assertEquals(entries.get(1).getVolume(), 7519.172778, delta);
assertEquals(entries.get(1).getTurnover(), 188453792.2371207168, delta);
} catch (IOException e) {
System.out.println(e.getMessage());
fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void testServerTimeResultDeserializer()
assertEquals(response.getRetMsg(), "OK");
// Accessing the fields of the result
ServerTimeResult serverTimeResult = response.getResult();
assertEquals(serverTimeResult.getTimeNano(), "1699816496197575040");
assertEquals(serverTimeResult.getTimeSecond(), "1699816496");
assertEquals(serverTimeResult.getTimeNano(), 1699816496197575040L);
assertEquals(serverTimeResult.getTimeSecond(), 1699816496L);
}catch (IOException e) {
System.out.println(e.getMessage());
fail();
Expand Down