diff --git a/src/main/java/com/casper/sdk/service/CasperService.java b/src/main/java/com/casper/sdk/service/CasperService.java index 707506bd1..d1ee312df 100644 --- a/src/main/java/com/casper/sdk/service/CasperService.java +++ b/src/main/java/com/casper/sdk/service/CasperService.java @@ -26,6 +26,7 @@ import com.casper.sdk.model.uref.URef; import com.casper.sdk.model.validator.ValidatorChangeData; import com.googlecode.jsonrpc4j.*; +import org.apache.cxf.common.util.StringUtils; import java.net.MalformedURLException; import java.net.URL; @@ -42,6 +43,31 @@ */ public interface CasperService { + /** + * Builds a CasperService for the node url. + * + * @param url the peer url to connect to + * @param additionalHeaders additional headers to be added to the request + * @return A Dynamic Proxy to CasperService + * @throws MalformedURLException is thrown if ip/port are not compliant + */ + static CasperService usingPeer(URL url, final Map additionalHeaders) throws MalformedURLException { + if (StringUtils.isEmpty(url.getPath())) { + url = new URL(url.getProtocol(), url.getHost(), url.getPort(), "/rpc"); + } + + final Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + if (additionalHeaders != null) { + headers.putAll(additionalHeaders); + } + + final JsonRpcHttpClient client = new JsonRpcHttpClient(new CasperObjectMapper(), url, headers); + client.setExceptionResolver(new CasperClientExceptionResolver()); + + return ProxyUtil.createClientProxy(CasperService.class.getClassLoader(), CasperService.class, client); + } + /** * Builds a CasperService for the node ip/port pair * @@ -51,16 +77,7 @@ public interface CasperService { * @throws MalformedURLException is thrown if ip/port are not compliant */ static CasperService usingPeer(String ip, int port) throws MalformedURLException { - CasperObjectMapper objectMapper = new CasperObjectMapper(); - Map newHeaders = new HashMap<>(); - newHeaders.put("Content-Type", "application/json"); - JsonRpcHttpClient client = new JsonRpcHttpClient(objectMapper, new URL("http", ip, port, "/rpc"), - newHeaders); - - ExceptionResolver exceptionResolver = new CasperClientExceptionResolver(); - client.setExceptionResolver(exceptionResolver); - - return ProxyUtil.createClientProxy(CasperService.class.getClassLoader(), CasperService.class, client); + return usingPeer(new URL("http", ip, port, "/rpc"), null); } //region INFORMATIONAL METHODS @@ -213,13 +230,13 @@ DictionaryData getStateDictionaryItem(@JsonRpcParam("state_root_hash") String st */ @JsonRpcMethod("speculative_exec") SpeculativeDeployData speculativeExec(@JsonRpcParam("block_identifier") BlockIdentifier blockIdentifier, - @JsonRpcParam("deploy") Deploy deploy); + @JsonRpcParam("deploy") Deploy deploy); /** * The speculative_exec endpoint provides a method to execute a Deploy * without committing it to global state * - * @param deploy the deploy object to send to the network + * @param deploy the deploy object to send to the network * @return Object holding the api version and the deploy hash */ @JsonRpcMethod("speculative_exec") @@ -287,6 +304,7 @@ QueryBalanceData queryBalance(@JsonRpcParam("state_identifier") GlobalStateIdent //endregion //region DEPRECATED METHODS + /** * Returns a stored value from the network. This RPC is deprecated, use `query_global_state` instead" *