Skip to content

Commit

Permalink
Improve batch call example and wrapper comments (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Feb 19, 2024
1 parent 171c747 commit 47cfc89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions json_rpc/private/client_handler_wrapper.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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"

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 47cfc89

Please sign in to comment.