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)
```
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.
## Working with client transports

View File

@ -42,7 +42,7 @@ proc createBatchCallProc(procName, parameters, callBody: NimNode): NimNode =
# export this proc
result[0] = nnkPostfix.newTree(ident"*", newIdentNode($procName))
proc setupConversion(reqParams, params: NimNode): NimNode =
# populate json params
# even rpcs with no parameters have an empty json array node sent
@ -67,6 +67,13 @@ proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimN
## reqParams.positional.add encode(JrpcConv, paramB).JsonString
## let res = await client.call("rpcApi", reqParams)
## 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
# to json using JrpcConv.encode.
@ -90,7 +97,7 @@ proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimN
if returnType.noWrap: quote do:
`procRes` = `rpcResult`
else: doDecode
batchParams = params.copy
batchIdent = ident "batch"
@ -120,22 +127,22 @@ proc createRpcFromSig*(clientType, rpcDecl: NimNode, alias = NimNode(nil)): NimN
ident "RpcBatchCallRef",
newEmptyNode()
))
# remove return type
batchParams[0] = newEmptyNode()
let batchCallBody = quote do:
`setup`
`batchIdent`.batch.add RpcBatchItem(
meth: `pathStr`,
params: `reqParams`
)
# create rpc proc
result = newStmtList()
result = newStmtList()
result.add createRpcProc(procName, params, callBody)
result.add createBatchCallProc(procName, batchParams, batchCallBody)
when defined(nimDumpRpcs):
echo pathStr, ":\n", result.repr