From 5ac0d14d880428756908f2b1ee216d244c7a78e2 Mon Sep 17 00:00:00 2001 From: jangko Date: Sun, 18 Feb 2024 11:14:13 +0700 Subject: [PATCH] Improve batch call example and wrapper comments --- README.md | 14 ++++++++++++++ json_rpc/private/client_handler_wrapper.nim | 21 ++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c4763fb..8723042 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/json_rpc/private/client_handler_wrapper.nim b/json_rpc/private/client_handler_wrapper.nim index 95c7be1..c65be73 100644 --- a/json_rpc/private/client_handler_wrapper.nim +++ b/json_rpc/private/client_handler_wrapper.nim @@ -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