From 965e2c16f58387650e72cb23b90c6f915aa76251 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 12 Nov 2018 11:43:51 +0700 Subject: [PATCH 1/3] fixes #38 add http method get/set to httpclient --- json_rpc/clients/httpclient.nim | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/json_rpc/clients/httpclient.nim b/json_rpc/clients/httpclient.nim index d3d5afd..74d6d63 100644 --- a/json_rpc/clients/httpclient.nim +++ b/json_rpc/clients/httpclient.nim @@ -6,9 +6,17 @@ logScope: topic = "JSONRPC-HTTP-CLIENT" type + HttpMethod* {.pure.} = enum + GET + POST + + HttpClientOptions* = object + httpMethod: HttpMethod + RpcHttpClient* = ref object of RpcClient transp*: StreamTransport addresses: seq[TransportAddress] + options: HttpClientOptions const MaxHttpHeadersSize = 8192 # maximum size of HTTP headers in octets @@ -18,8 +26,8 @@ const HeadersMark = @[byte(0x0D), byte(0x0A), byte(0x0D), byte(0x0A)] proc sendRequest(transp: StreamTransport, - data: string): Future[bool] {.async.} = - var request = "GET / " + data: string, options: HttpClientOptions): Future[bool] {.async.} = + var request = $options.httpMethod & " / " request.add($HttpVersion11) request.add("\r\n") request.add("Date: " & httpDate() & "\r\n") @@ -134,10 +142,20 @@ proc recvData(transp: StreamTransport): Future[string] {.async.} = else: result = cast[string](buffer) +proc init(opts: var HttpClientOptions) = + opts.httpMethod = HttpMethod.GET + proc newRpcHttpClient*(): RpcHttpClient = ## Creates a new HTTP client instance. new result result.initRpcClient() + result.options.init() + +proc httpMethod*(client: RpcHttpClient): HttpMethod = + client.options.httpMethod + +proc httpMethod*(client: RpcHttpClient, m: HttpMethod) = + client.options.httpMethod = m proc call*(client: RpcHttpClient, name: string, params: JsonNode): Future[Response] {.async.} = @@ -148,7 +166,7 @@ proc call*(client: RpcHttpClient, name: string, if isNil(client.transp) or client.transp.closed(): raise newException(ValueError, "Transport is not initialised or already closed") - let res = await client.transp.sendRequest(value) + let res = await client.transp.sendRequest(value, client.options) if not res: debug "Failed to send message to RPC server", address = client.transp.remoteAddress(), msg_len = res From 06637848de8bd7eb5c3f766d96663c8877fdb4ae Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 12 Nov 2018 21:02:54 +0700 Subject: [PATCH 2/3] remove httpmethod enum --- json_rpc/clients/httpclient.nim | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/json_rpc/clients/httpclient.nim b/json_rpc/clients/httpclient.nim index 74d6d63..f142a3c 100644 --- a/json_rpc/clients/httpclient.nim +++ b/json_rpc/clients/httpclient.nim @@ -6,10 +6,6 @@ logScope: topic = "JSONRPC-HTTP-CLIENT" type - HttpMethod* {.pure.} = enum - GET - POST - HttpClientOptions* = object httpMethod: HttpMethod @@ -143,7 +139,7 @@ proc recvData(transp: StreamTransport): Future[string] {.async.} = result = cast[string](buffer) proc init(opts: var HttpClientOptions) = - opts.httpMethod = HttpMethod.GET + opts.httpMethod = MethodGet proc newRpcHttpClient*(): RpcHttpClient = ## Creates a new HTTP client instance. From 3c4c5559f1ceb03fa09bb00f0d0f2ea41ad0c65e Mon Sep 17 00:00:00 2001 From: andri lim Date: Tue, 13 Nov 2018 10:22:43 +0700 Subject: [PATCH 3/3] add httpMethod param to httpclient.call --- json_rpc/clients/httpclient.nim | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/json_rpc/clients/httpclient.nim b/json_rpc/clients/httpclient.nim index f142a3c..3c695ac 100644 --- a/json_rpc/clients/httpclient.nim +++ b/json_rpc/clients/httpclient.nim @@ -22,8 +22,8 @@ const HeadersMark = @[byte(0x0D), byte(0x0A), byte(0x0D), byte(0x0A)] proc sendRequest(transp: StreamTransport, - data: string, options: HttpClientOptions): Future[bool] {.async.} = - var request = $options.httpMethod & " / " + data: string, httpMethod: HttpMethod): Future[bool] {.async.} = + var request = $httpMethod & " / " request.add($HttpVersion11) request.add("\r\n") request.add("Date: " & httpDate() & "\r\n") @@ -154,7 +154,7 @@ proc httpMethod*(client: RpcHttpClient, m: HttpMethod) = client.options.httpMethod = m proc call*(client: RpcHttpClient, name: string, - params: JsonNode): Future[Response] {.async.} = + params: JsonNode, httpMethod: HttpMethod): Future[Response] {.async.} = ## Remotely calls the specified RPC method. let id = client.getNextId() @@ -162,7 +162,7 @@ proc call*(client: RpcHttpClient, name: string, if isNil(client.transp) or client.transp.closed(): raise newException(ValueError, "Transport is not initialised or already closed") - let res = await client.transp.sendRequest(value, client.options) + let res = await client.transp.sendRequest(value, httpMethod) if not res: debug "Failed to send message to RPC server", address = client.transp.remoteAddress(), msg_len = res @@ -178,6 +178,10 @@ proc call*(client: RpcHttpClient, name: string, client.awaiting[id] = newFut result = await newFut +template call*(client: RpcHttpClient, name: string, + params: JsonNode): untyped = + client.call(name, params, client.httpMethod) + proc processData(client: RpcHttpClient) {.async.} = while true: var value = await client.transp.recvData()