Improve batch call example and wrapper comments (#214)

This commit is contained in:
andri lim 2024-02-19 08:52:10 +07:00 committed by GitHub
parent 171c747584
commit 47cfc8916f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 7 deletions

View File

@ -394,6 +394,20 @@ You can use:
let bmiIndex = await client.bmi(120.5, 12.0) let bmiIndex = await client.bmi(120.5, 12.0)
``` ```
Or you can use batch call to send multiple request at once to the server.
```Nim
let batch = client.prepareBatch()
batch.bmi(120.5, 12.0)
batch.bmi(120.5, 13.0)
batch.bmi(120.5, 14.0)
let res = await batch.send()
# But you need to manually process the response e.g. decode from JSON to
# your expected type because you can mix various rpc method call in one batch
# with various return type.
```
This allows you to leverage Nim's static type checking whilst also aiding readability and providing a unified location to declare client side RPC definitions. This allows you to leverage Nim's static type checking whilst also aiding readability and providing a unified location to declare client side RPC definitions.
## Working with client transports ## Working with client transports

View File

@ -67,6 +67,13 @@ proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimN
## reqParams.positional.add encode(JrpcConv, paramB).JsonString ## reqParams.positional.add encode(JrpcConv, paramB).JsonString
## let res = await client.call("rpcApi", reqParams) ## let res = await client.call("rpcApi", reqParams)
## result = decode(JrpcConv, res.string, typeof RetType) ## result = decode(JrpcConv, res.string, typeof RetType)
##
## 2nd version to handle batch request after calling client.prepareBatch()
## proc rpcApi(batch: RpcBatchCallRef; paramA: TypeA; paramB: TypeB) =
## var reqParams = RequestParamsTx(kind: rpPositional)
## reqParams.positional.add encode(JrpcConv, paramA).JsonString
## reqParams.positional.add encode(JrpcConv, paramB).JsonString
## batch.batch.add RpcBatchItem(meth: "rpcApi", params: reqParams)
# Each input parameter in the rpc signature is converted # Each input parameter in the rpc signature is converted
# to json using JrpcConv.encode. # to json using JrpcConv.encode.