Skip to content

Commit 842045f

Browse files
authored
feat: client api split (#166)
1 parent 9157c20 commit 842045f

24 files changed

Lines changed: 541 additions & 342 deletions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.arkecosystem.client;
2+
3+
import java.util.Map;
4+
import org.arkecosystem.client.api.Api;
5+
import org.arkecosystem.client.http.Client;
6+
7+
public class ArkClient {
8+
private final Api api;
9+
private final Client client;
10+
11+
/**
12+
* Constructor to create an instance of ArkClient.
13+
*
14+
* @param hostOrHosts Can be a string representing the host URL or a map with different types of
15+
* hosts.
16+
*/
17+
public ArkClient(Object hostOrHosts) {
18+
this.client = new Client(hostOrHosts);
19+
this.api = new Api(this.client);
20+
}
21+
22+
/**
23+
* Method to get the API instance.
24+
*
25+
* @return The API instance.
26+
*/
27+
public Api api() {
28+
return this.api;
29+
}
30+
31+
/**
32+
* Method to get the HTTP client instance.
33+
*
34+
* @return The HTTP client instance.
35+
*/
36+
public Client client() {
37+
return this.client;
38+
}
39+
40+
/**
41+
* Method to set a new host for a specific type.
42+
*
43+
* @param host The host URL.
44+
* @param type The type of host (api, transactions, evm).
45+
*/
46+
public void setHost(String host, String type) {
47+
this.client.setHost(host, type);
48+
}
49+
50+
/**
51+
* Method to get the current hosts.
52+
*
53+
* @return A map with the current hosts.
54+
*/
55+
public Map<String, String> getHosts() {
56+
return this.client.getHosts();
57+
}
58+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.arkecosystem.client;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ClientManager {
7+
private final Map<String, ArkClient> clients;
8+
private String defaultClient = "main";
9+
10+
public ClientManager() {
11+
this.clients = new HashMap<>();
12+
}
13+
14+
/**
15+
* Get the default client name.
16+
*
17+
* @return The name of the default client.
18+
*/
19+
public String getDefaultClient() {
20+
return this.defaultClient;
21+
}
22+
23+
/**
24+
* Set the default client name.
25+
*
26+
* @param name The name of the default client to set.
27+
*/
28+
public void setDefaultClient(String name) {
29+
this.defaultClient = name;
30+
}
31+
32+
/**
33+
* Return all created clients.
34+
*
35+
* @return A map of all created clients.
36+
*/
37+
public Map<String, ArkClient> getClients() {
38+
return this.clients;
39+
}
40+
41+
/**
42+
* Connect to a given client.
43+
*
44+
* @param host The host URL for the client.
45+
* @param name The name to assign to this client instance.
46+
* @return The newly created ArkClient instance.
47+
*/
48+
public ArkClient connect(String host, String name) {
49+
if (this.clients.containsKey(name)) {
50+
throw new IllegalArgumentException("Client [" + name + "] is already configured.");
51+
}
52+
53+
this.clients.put(name, new ArkClient(host));
54+
55+
return this.clients.get(name);
56+
}
57+
58+
/**
59+
* Connect to the default client.
60+
*
61+
* @param host The host URL for the client.
62+
* @return The newly created ArkClient instance.
63+
*/
64+
public ArkClient connect(String host) {
65+
return connect(host, "main");
66+
}
67+
68+
/**
69+
* Disconnect from a given client.
70+
*
71+
* @param name The name of the client to disconnect.
72+
*/
73+
public void disconnect(String name) {
74+
if (name == null || name.isEmpty()) {
75+
name = getDefaultClient();
76+
}
77+
78+
this.clients.remove(name);
79+
}
80+
81+
/** Disconnect from the default client. */
82+
public void disconnect() {
83+
disconnect(null);
84+
}
85+
86+
/**
87+
* Get a client instance by name.
88+
*
89+
* @param name The name of the client to retrieve.
90+
* @return The corresponding ArkClient instance.
91+
*/
92+
public ArkClient client(String name) {
93+
if (name == null || name.isEmpty()) {
94+
name = getDefaultClient();
95+
}
96+
97+
if (!this.clients.containsKey(name)) {
98+
throw new IllegalArgumentException("Client [" + name + "] not configured.");
99+
}
100+
101+
return this.clients.get(name);
102+
}
103+
104+
/**
105+
* Get the default client instance.
106+
*
107+
* @return The default ArkClient instance.
108+
*/
109+
public ArkClient client() {
110+
return client(null);
111+
}
112+
}

src/main/java/org/arkecosystem/client/Connection.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/org/arkecosystem/client/ConnectionManager.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/org/arkecosystem/client/api/Api.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.arkecosystem.client.http.Client;
44

55
public class Api {
6+
public final ApiNodes apiNodes;
67
public final Blockchain blockchain;
78
public final Blocks blocks;
9+
public final Commits commits;
810
public final Delegates delegates;
911
public final Entities entities;
1012
public final Node node;
@@ -13,12 +15,12 @@ public class Api {
1315
public final Transactions transactions;
1416
public final Votes votes;
1517
public final Wallets wallets;
16-
public final ApiNodes apiNodes;
17-
public final Commits commits;
1818

1919
public Api(Client client) {
20+
this.apiNodes = new ApiNodes(client);
2021
this.blockchain = new Blockchain(client);
2122
this.blocks = new Blocks(client);
23+
this.commits = new Commits(client);
2224
this.delegates = new Delegates(client);
2325
this.entities = new Entities(client);
2426
this.node = new Node(client);
@@ -27,7 +29,5 @@ public Api(Client client) {
2729
this.transactions = new Transactions(client);
2830
this.votes = new Votes(client);
2931
this.wallets = new Wallets(client);
30-
this.apiNodes = new ApiNodes(client);
31-
this.commits = new Commits(client);
3232
}
3333
}

src/main/java/org/arkecosystem/client/api/Transactions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Map<String, Object> all() throws IOException {
2929
public Map<String, Object> create(List<Map<String, ?>> transactions) throws IOException {
3030
Map<String, Object> params = new HashMap<>();
3131
params.put("transactions", transactions);
32-
return this.client.post("transactions", params);
32+
return this.client.withApi("transactions").post("transactions", params);
3333
}
3434

3535
public Map<String, Object> show(String id) throws IOException {

0 commit comments

Comments
 (0)