Skip to content

Commit 1dab161

Browse files
author
Pedro Pablo Bustamante Barrera
committed
Merge branch 'feat/client-reusability' into develop
2 parents 29e1993 + 8d52d70 commit 1dab161

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

src/main/java/com/cryptomarket/sdk/HMAC.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public HMAC(String apiKey, String apiSecret, Integer window) {
2525
this.window = window;
2626
}
2727

28+
public Integer getWindow() {
29+
return window;
30+
}
31+
2832
public String getCredential(String method, String body, String url) {
2933
String timestamp = String.format("%d", System.currentTimeMillis());
3034
String message = new StringBuffer()

src/main/java/com/cryptomarket/sdk/rest/CloseableHttpClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
public interface CloseableHttpClient extends Closeable {
1111

12+
/**
13+
* Changes the user credentials used for authentication in calls
14+
* @param apiKey the user public key used in new calls
15+
* @param apiSecret the user secret key used in new calls
16+
*/
17+
public void changeCredentials(String apiKey, String apiSecret);
18+
1219
/**
1320
* Does an http get without authentication
1421
*

src/main/java/com/cryptomarket/sdk/rest/CryptomarketRestClient.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
import java.util.List;
55
import java.util.Map;
66

7+
import org.jetbrains.annotations.Nullable;
8+
79
import com.cryptomarket.params.AccountType;
810
import com.cryptomarket.params.ContingencyType;
9-
import com.cryptomarket.params.SortBy;
10-
import com.cryptomarket.params.SubAccountStatus;
11+
import com.cryptomarket.params.IdentifyBy;
12+
import com.cryptomarket.params.OrderBuilder;
1113
import com.cryptomarket.params.OrderType;
1214
import com.cryptomarket.params.ParamsBuilder;
1315
import com.cryptomarket.params.Period;
1416
import com.cryptomarket.params.Side;
1517
import com.cryptomarket.params.Sort;
18+
import com.cryptomarket.params.SortBy;
19+
import com.cryptomarket.params.SubAccountTransferType;
1620
import com.cryptomarket.params.TimeInForce;
1721
import com.cryptomarket.params.TransactionStatus;
1822
import com.cryptomarket.params.TransactionSubtype;
1923
import com.cryptomarket.params.TransactionType;
20-
import com.cryptomarket.params.SubAccountTransferType;
21-
import com.cryptomarket.params.IdentifyBy;
22-
import com.cryptomarket.params.OrderBuilder;
2324
import com.cryptomarket.params.UseOffchain;
2425
import com.cryptomarket.sdk.exceptions.CryptomarketSDKException;
25-
import com.cryptomarket.sdk.models.SubAccountSettings;
2626
import com.cryptomarket.sdk.models.Address;
2727
import com.cryptomarket.sdk.models.AmountLock;
2828
import com.cryptomarket.sdk.models.Balance;
@@ -36,19 +36,26 @@
3636
import com.cryptomarket.sdk.models.PublicTrade;
3737
import com.cryptomarket.sdk.models.SubAccount;
3838
import com.cryptomarket.sdk.models.SubAccountBalances;
39+
import com.cryptomarket.sdk.models.SubAccountSettings;
3940
import com.cryptomarket.sdk.models.Symbol;
4041
import com.cryptomarket.sdk.models.Ticker;
4142
import com.cryptomarket.sdk.models.TickerPrice;
4243
import com.cryptomarket.sdk.models.Trade;
4344
import com.cryptomarket.sdk.models.Transaction;
4445

45-
import org.jetbrains.annotations.Nullable;
46-
4746
/**
4847
* Rest Client Interface for cryptomarket API V3.
4948
*/
5049
public interface CryptomarketRestClient extends Closeable {
5150

51+
/**
52+
* Changes the user credentials used for authentication in calls
53+
*
54+
* @param apiKey the user public key used in new calls
55+
* @param apiSecret the user secret key used in new calls
56+
*/
57+
public void changeCredentials(String apiKey, String apiSecret);
58+
5259
/// PUBLIC CALLS///
5360

5461
/**

src/main/java/com/cryptomarket/sdk/rest/CryptomarketRestClientImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
import java.util.List;
66
import java.util.Map;
77

8+
import org.apache.http.impl.client.HttpClients;
9+
810
import com.cryptomarket.params.AccountType;
911
import com.cryptomarket.params.ContingencyType;
10-
import com.cryptomarket.params.SortBy;
11-
import com.cryptomarket.params.SubAccountStatus;
12+
import com.cryptomarket.params.IdentifyBy;
13+
import com.cryptomarket.params.OrderBuilder;
1214
import com.cryptomarket.params.OrderType;
1315
import com.cryptomarket.params.ParamsBuilder;
1416
import com.cryptomarket.params.Period;
1517
import com.cryptomarket.params.Side;
1618
import com.cryptomarket.params.Sort;
19+
import com.cryptomarket.params.SortBy;
20+
import com.cryptomarket.params.SubAccountTransferType;
1721
import com.cryptomarket.params.TimeInForce;
1822
import com.cryptomarket.params.TransactionStatus;
1923
import com.cryptomarket.params.TransactionSubtype;
2024
import com.cryptomarket.params.TransactionType;
21-
import com.cryptomarket.params.SubAccountTransferType;
22-
import com.cryptomarket.params.IdentifyBy;
23-
import com.cryptomarket.params.OrderBuilder;
2425
import com.cryptomarket.params.UseOffchain;
2526
import com.cryptomarket.sdk.Adapter;
2627
import com.cryptomarket.sdk.ArgNames;
@@ -52,15 +53,29 @@ public class CryptomarketRestClientImpl implements CryptomarketRestClient {
5253
Adapter adapter = new Adapter();
5354

5455
public CryptomarketRestClientImpl(String apiKey, String apiSecret) {
56+
this(apiKey, apiSecret, HttpClients.createDefault());
57+
}
58+
59+
public CryptomarketRestClientImpl(String apiKey, String apiSecret,
60+
org.apache.http.impl.client.CloseableHttpClient client) {
5561
String url = "https://api.exchange.cryptomkt.com";
5662
String apiVersion = "/api/3/";
57-
httpClient = new HttpClientImpl(url, apiVersion, apiKey, apiSecret);
63+
httpClient = new HttpClientImpl(client, url, apiVersion, apiKey, apiSecret);
5864
}
5965

6066
public CryptomarketRestClientImpl() {
6167
this("", "");
6268
}
6369

70+
public CryptomarketRestClientImpl(org.apache.http.impl.client.CloseableHttpClient client) {
71+
this("", "", client);
72+
}
73+
74+
@Override
75+
public void changeCredentials(String apiKey, String apiSecret) {
76+
httpClient.changeCredentials(apiKey, apiSecret);
77+
}
78+
6479
// PUBLIC
6580

6681
@Override

src/main/java/com/cryptomarket/sdk/rest/HttpClientImpl.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public class HttpClientImpl implements CloseableHttpClient {
4949
private final JsonAdapter<ErrorResponse> errorJsonAdapter = moshi.adapter(ErrorResponse.class);
5050
private final ParameterizedType mapStringString = Types.newParameterizedType(Map.class, String.class, String.class);
5151
private final JsonAdapter<Map<String, String>> mapStrStrJsonAdapter = moshi.adapter(mapStringString);
52-
org.apache.http.impl.client.CloseableHttpClient client = HttpClients.createDefault();
52+
private final org.apache.http.impl.client.CloseableHttpClient client;
5353
private HMAC hmac;
5454

55-
5655
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
5756
public static final String METHOD_NAME = "DELETE";
5857

@@ -79,12 +78,31 @@ public HttpClientImpl(String url, String apiVersion, String apiKey, String apiSe
7978
this(url, apiVersion, apiKey, apiSecret, 0);
8079
}
8180

81+
public HttpClientImpl(org.apache.http.impl.client.CloseableHttpClient client, String url, String apiVersion,
82+
String apiKey, String apiSecret) {
83+
this(client, url, apiVersion, apiKey, apiSecret, 0);
84+
}
85+
8286
public HttpClientImpl(String url, String apiVersion, String apiKey, String apiSecret, Integer window) {
87+
this.client = HttpClients.createDefault();
8388
this.hmac = new HMAC(apiKey, apiSecret, window);
8489
this.url = url;
8590
this.apiVersion = apiVersion;
8691
}
8792

93+
public HttpClientImpl(org.apache.http.impl.client.CloseableHttpClient client, String url, String apiVersion,
94+
String apiKey, String apiSecret, Integer window) {
95+
this.client = client;
96+
this.hmac = new HMAC(apiKey, apiSecret, window);
97+
this.url = url;
98+
this.apiVersion = apiVersion;
99+
}
100+
101+
@Override
102+
public void changeCredentials(String apiKey, String apiSecret) {
103+
this.hmac = new HMAC(apiKey, apiSecret, hmac.getWindow());
104+
}
105+
88106
public void close() throws IOException {
89107
client.close();
90108
}

0 commit comments

Comments
 (0)