Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions json_rpc/clients/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import
std/[tables, uri],
stew/byteutils,
results,
chronos/apps/http/httpclient as chronosHttpClient,
chronicles, httputils, json_serialization/std/net,
chronos/apps/http/httpclient,
chronicles, httputils,
json_serialization/std/net as jsnet,
../client,
../errors,
../private/jrpc_sys

export
client, errors, HttpClientFlag, HttpClientFlags
client, errors, jsnet, HttpClientFlag, HttpClientFlags

logScope:
topics = "JSONRPC-HTTP-CLIENT"
Expand All @@ -31,7 +32,7 @@ type

RpcHttpClient* = ref object of RpcClient
httpSession: HttpSessionRef
httpAddress: HttpResult[HttpAddress]
httpAddress: HttpAddress
maxBodySize: int
getHeaders: GetJsonRpcRequestHeaders

Expand Down Expand Up @@ -76,8 +77,9 @@ template closeRefs(req, res: untyped) =

proc callImpl(client: RpcHttpClient, reqBody: string): Future[string] {.async.} =
doAssert client.httpSession != nil
if client.httpAddress.isErr:
raise newException(RpcAddressUnresolvableError, client.httpAddress.error)
if client.httpAddress.addresses.len == 0:
raise newException(RpcPostError, "Not connected")


var headers =
if not isNil(client.getHeaders):
Expand All @@ -90,7 +92,7 @@ proc callImpl(client: RpcHttpClient, reqBody: string): Future[string] {.async.}
var res: HttpClientResponseRef

req = HttpClientRequestRef.post(client.httpSession,
client.httpAddress.get,
client.httpAddress,
body = reqBody.toOpenArrayByte(0, reqBody.len - 1),
headers = headers)
res =
Expand Down Expand Up @@ -140,13 +142,12 @@ proc newRpcHttpClient*(
method call*(client: RpcHttpClient, name: string,
params: RequestParamsTx): Future[JsonString]
{.async.} =

let
id = client.getNextId()
reqBody = requestTxEncode(name, params, id)

debug "Sending JSON-RPC request",
address = client.httpAddress, len = len(reqBody), name, id
address = $client.httpAddress, len = len(reqBody), name, id
trace "Message", msg = reqBody

let resText = await client.callImpl(reqBody)
Expand Down Expand Up @@ -207,21 +208,17 @@ method callBatch*(client: RpcHttpClient,
raise newException(InvalidResponse, "Invalid response")

proc connect*(client: RpcHttpClient, url: string) {.async.} =
client.httpAddress = client.httpSession.getAddress(url)
if client.httpAddress.isErr:
raise newException(RpcAddressUnresolvableError, client.httpAddress.error)
client.httpAddress = client.httpSession.getAddress(url).valueOr:
raise newException(RpcAddressUnresolvableError, error)

proc connect*(client: RpcHttpClient, address: string, port: Port, secure: bool) {.async.} =
var uri = Uri(
let uri = Uri(
scheme: if secure: "https" else: "http",
hostname: address,
port: $port)

let res = getAddress(client.httpSession, uri)
if res.isOk:
client.httpAddress = res
else:
raise newException(RpcAddressUnresolvableError, res.error)
client.httpAddress = getAddress(client.httpSession, uri).valueOr:
raise newException(RpcAddressUnresolvableError, error)

method close*(client: RpcHttpClient) {.async.} =
if not client.httpSession.isNil:
Expand Down
3 changes: 2 additions & 1 deletion json_rpc/clients/socketclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import
chronicles,
results,
chronos,
json_serialization/std/net as jsnet,
../client,
../errors,
../private/jrpc_sys

export client, errors
export client, errors, jsnet

logScope:
topics = "JSONRPC-SOCKET-CLIENT"
Expand Down
6 changes: 2 additions & 4 deletions json_rpc/private/jrpc_sys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import
std/hashes,
results,
json_serialization,
json_serialization/pkg/results as jser_results
json_serialization/pkg/results as jsresults

export
results,
json_serialization
export results, json_serialization, jsresults

# This module implements JSON-RPC 2.0 Specification
# https://www.jsonrpc.org/specification
Expand Down
3 changes: 2 additions & 1 deletion json_rpc/servers/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import
std/sequtils,
chronicles, httputils, chronos,
chronos/apps/http/[httpserver, shttpserver],
json_serialization/std/net as jsnet,
../private/utils,
../errors,
../server

export
server, shttpserver
server, shttpserver, jsnet

logScope:
topics = "JSONRPC-HTTP-SERVER"
Expand Down
4 changes: 2 additions & 2 deletions json_rpc/servers/socketserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import
std/sequtils,
chronicles,
json_serialization/std/net,
json_serialization/std/net as jsnet,
../private/utils,
../errors,
../server

export errors, server
export errors, server, jsnet

type
RpcSocketServer* = ref object of RpcServer
Expand Down
11 changes: 6 additions & 5 deletions json_rpc/servers/websocketserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import
chronicles, chronos, websock/[websock, types],
websock/extensions/compression/deflate,
stew/byteutils, json_serialization/std/net,
".."/[errors, server]
stew/byteutils,
json_serialization/std/net as jsnet,

export errors, server, net
../[errors, server]

export errors, server, jsnet

logScope:
topics = "JSONRPC-WS-SERVER"
Expand Down Expand Up @@ -84,8 +86,7 @@ proc serveHTTP*(rpc: RpcWebSocketHandler, request: HttpRequest)

proc handleRequest(rpc: RpcWebSocketServer, request: HttpRequest)
{.async: (raises: [CancelledError]).} =
trace "Handling request:", uri = request.uri.path
trace "Initiating web socket connection."
trace "Handling request:", uri = $request.uri

# if hook result is false,
# it means we should return immediately
Expand Down
Loading