Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/resourcepool/jarpic-client
A Simple JSON-RPC 2.0 Java Client using Jackson2 and OkHttp
https://github.com/resourcepool/jarpic-client
android client java json json-rpc json-rpc-client json-rpc2
Last synced: about 1 month ago
JSON representation
A Simple JSON-RPC 2.0 Java Client using Jackson2 and OkHttp
- Host: GitHub
- URL: https://github.com/resourcepool/jarpic-client
- Owner: resourcepool
- License: apache-2.0
- Created: 2016-03-11T09:46:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-05T11:58:41.000Z (over 7 years ago)
- Last Synced: 2024-09-27T00:01:51.207Z (about 2 months ago)
- Topics: android, client, java, json, json-rpc, json-rpc-client, json-rpc2
- Language: Java
- Homepage:
- Size: 38.1 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jarpic-client
A Simple JSON-RPC 2.0 Java Client using Jackson and OkHttpThis library works on Android as well
It contains both synchronous and asynchronous APIs.
[![Build Status](https://travis-ci.org/resourcepool/jarpic-client.svg?branch=master)](https://travis-ci.org/resourcepool/jarpic-client)
## Add it to your project
Maven:
```xmlio.resourcepool
jarpic-client
1.1.0```
Gradle:
```groovy
compile 'io.resourcepool:jarpic-client:1.1.0'
```## Usage
Send single JSON-RPC request:
```java
JsonRpcClient client = new HttpJsonRpcClient(endpoint);
JsonRpcRequest req = JsonRpcRequest.builder()
.method("cmd::execCmd")
.param("param1", "myvalue1")
.param("param2", "myvalue2")
.build();
// With your own Result class POJO
JsonRpcResponse res = client.send(req, Result.class);
System.out.println("Response is:");
System.out.println(res);
```Send multiple JSON-RPC requests:
```java
JsonRpcClient client = new HttpJsonRpcClient(endpoint);
JsonRpcRequest req1 = JsonRpcRequest.builder()
.method("cmd::execCmd")
.param("param1", "myvalue1")
.param("param2", "myvalue2")
.build();JsonRpcRequest req2 = JsonRpcRequest.builder()
.method("cmd::resumeCmd")
.param("key", "value")
.build();
List reqs = JsonRpcRequest.combine(req1, req2);
// With your own Result class POJO
List> res = client.send(reqs, Result.class);
System.out.println("Response is:");
System.out.println(res);
```Send single JSON-RPC Notification
```java
JsonRpcClient client = new HttpJsonRpcClient(endpoint);
JsonRpcRequest req = JsonRpcRequest.notifBuilder()
.method("cmd::execCmd")
.param("param1", "myvalue1")
.param("param2", "myvalue2")
.build();
// With your own Result class POJO
JsonRpcResponse res = client.send(req, Result.class);
System.out.println("Response is:");
System.out.println(res);
```**All these methods can also be called asynchronously by providing an extra parameter.**
Simple Example:
```java
JsonRpcClient client = new HttpJsonRpcClient(endpoint);
JsonRpcRequest req = JsonRpcRequest.builder()
.method("cmd::execCmd")
.param("param1", "myvalue1")
.param("param2", "myvalue2")
.build();
client.send(req, String.class, new JsonRpcCallback() {
@Override
public void onResponse(@Nullable JsonRpcResponse res) {
System.out.println("Response is:");
System.out.println(res);
}@Override
public void onFailure(IOException ex) {
System.err.println("Something bad happened: " + ex.getMessage());
}
});```
Example with custom deserializer:
```java
JsonRpcRequest req = JsonRpcRequest.builder()
.method("cmd::start")
.param("apiKey", apiKey)
.build();client.send(req, Result.class, new JsonRpcCallback() {
@Override
public void onResponse(@Nullable JsonRpcResponse res) {
// With your own Result class POJO
System.out.println(res.getResult());
}@Override
public void onFailure(IOException ex) {
System.err.println("Something bad happened: " + ex.getMessage());
}
});
```## License
Copyright 2017 ResourcepoolLicensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.