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

feat: client api split #166

Merged
merged 19 commits into from
Aug 23, 2024
Merged
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
58 changes: 58 additions & 0 deletions src/main/java/org/arkecosystem/client/ArkClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.arkecosystem.client;

import java.util.Map;
import org.arkecosystem.client.api.Api;
import org.arkecosystem.client.http.Client;

public class ArkClient {
private final Api api;
private final Client client;

/**
* Constructor to create an instance of ArkClient.
*
* @param hostOrHosts Can be a string representing the host URL or a map with different types of
* hosts.
*/
public ArkClient(Object hostOrHosts) {
this.client = new Client(hostOrHosts);
this.api = new Api(this.client);
}

/**
* Method to get the API instance.
*
* @return The API instance.
*/
public Api api() {
return this.api;
}

/**
* Method to get the HTTP client instance.
*
* @return The HTTP client instance.
*/
public Client client() {
return this.client;
}

/**
* Method to set a new host for a specific type.
*
* @param host The host URL.
* @param type The type of host (api, transactions, evm).
*/
public void setHost(String host, String type) {
this.client.setHost(host, type);
}

/**
* Method to get the current hosts.
*
* @return A map with the current hosts.
*/
public Map<String, String> getHosts() {
return this.client.getHosts();
}
}
112 changes: 112 additions & 0 deletions src/main/java/org/arkecosystem/client/ClientManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.arkecosystem.client;

import java.util.HashMap;
import java.util.Map;

public class ClientManager {
private final Map<String, ArkClient> clients;
private String defaultClient = "main";

public ClientManager() {
this.clients = new HashMap<>();
}

/**
* Get the default client name.
*
* @return The name of the default client.
*/
public String getDefaultClient() {
return this.defaultClient;
}

/**
* Set the default client name.
*
* @param name The name of the default client to set.
*/
public void setDefaultClient(String name) {
this.defaultClient = name;
}

/**
* Return all created clients.
*
* @return A map of all created clients.
*/
public Map<String, ArkClient> getClients() {
return this.clients;
}

/**
* Connect to a given client.
*
* @param host The host URL for the client.
* @param name The name to assign to this client instance.
* @return The newly created ArkClient instance.
*/
public ArkClient connect(String host, String name) {
if (this.clients.containsKey(name)) {
throw new IllegalArgumentException("Client [" + name + "] is already configured.");
}

this.clients.put(name, new ArkClient(host));

return this.clients.get(name);
}

/**
* Connect to the default client.
*
* @param host The host URL for the client.
* @return The newly created ArkClient instance.
*/
public ArkClient connect(String host) {
return connect(host, "main");
}

/**
* Disconnect from a given client.
*
* @param name The name of the client to disconnect.
*/
public void disconnect(String name) {
if (name == null || name.isEmpty()) {
name = getDefaultClient();
}

this.clients.remove(name);
}

/** Disconnect from the default client. */
public void disconnect() {
disconnect(null);
}

/**
* Get a client instance by name.
*
* @param name The name of the client to retrieve.
* @return The corresponding ArkClient instance.
*/
public ArkClient client(String name) {
if (name == null || name.isEmpty()) {
name = getDefaultClient();
}

if (!this.clients.containsKey(name)) {
throw new IllegalArgumentException("Client [" + name + "] not configured.");
}

return this.clients.get(name);
}

/**
* Get the default client instance.
*
* @return The default ArkClient instance.
*/
public ArkClient client() {
return client(null);
}
}
23 changes: 0 additions & 23 deletions src/main/java/org/arkecosystem/client/Connection.java

This file was deleted.

67 changes: 0 additions & 67 deletions src/main/java/org/arkecosystem/client/ConnectionManager.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/org/arkecosystem/client/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.arkecosystem.client.http.Client;

public class Api {
public final ApiNodes apiNodes;
public final Blockchain blockchain;
public final Blocks blocks;
public final Commits commits;
public final Delegates delegates;
public final Entities entities;
public final Node node;
Expand All @@ -13,12 +15,12 @@ public class Api {
public final Transactions transactions;
public final Votes votes;
public final Wallets wallets;
public final ApiNodes apiNodes;
public final Commits commits;

public Api(Client client) {
this.apiNodes = new ApiNodes(client);
this.blockchain = new Blockchain(client);
this.blocks = new Blocks(client);
this.commits = new Commits(client);
this.delegates = new Delegates(client);
this.entities = new Entities(client);
this.node = new Node(client);
Expand All @@ -27,7 +29,5 @@ public Api(Client client) {
this.transactions = new Transactions(client);
this.votes = new Votes(client);
this.wallets = new Wallets(client);
this.apiNodes = new ApiNodes(client);
this.commits = new Commits(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Map<String, Object> all() throws IOException {
public Map<String, Object> create(List<Map<String, ?>> transactions) throws IOException {
Map<String, Object> params = new HashMap<>();
params.put("transactions", transactions);
return this.client.post("transactions", params);
return this.client.withApi("transactions").post("transactions", params);
}

public Map<String, Object> show(String id) throws IOException {
Expand Down
Loading
Loading