-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Funkatronics/readme-usage
add usage to readme
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# RpcCore | ||
Multiplatform JSON RPC Library using Kotlin Serialization | ||
|
||
## Usage | ||
|
||
### Network Driver | ||
RpcCore does not provide a multiplatform networking implementaion, you must provide your own by implementing the `HttpNetworkingDriver` interface: | ||
```kotlin | ||
class MyNetworkDriver : HttpNetworkDriver { | ||
override suspend fun makeHttpRequest(request: HttpRequest): String { | ||
val url = request.url | ||
val httpMethod = request.method (should be "POST" for RPC 2.0 requests) | ||
val requestHeaders = request.properties | ||
val requestBody = reqest.body | ||
// networking implementation to submit the HTTP request to the url | ||
... | ||
return response string | ||
} | ||
} | ||
``` | ||
|
||
### RPC 2.0 Requests | ||
```kotlin | ||
// setup RPC driver | ||
val rpcUrl = "https://api.endpoint.com" | ||
val rpcDriver = Rpc20Driver(rpcUrl, MyNetworkDriver()) | ||
|
||
// build RPC request | ||
val requestId = UUID.generate() | ||
val requestMehtod = "getTheThing" // the RPC method you want to call | ||
val requestParameters = buildJsonObject { // this will depend on the method you are calling | ||
put("param1", "Hello") | ||
put("param2", 1234) | ||
} | ||
val rpcRequest = JsonRpc20Request(requestId, requestParamaters, requestId) | ||
|
||
// send the request and get response | ||
// using JsonElement.serializer() will return the JSON RPC response. you can use your own serializer to get back a specific object | ||
val rpcResponse = rpcDriver.makeRequest(rpcRequest, JsonElement.serializer()) | ||
``` | ||
|
||
|
||
|
||
|