From 965e2c16f58387650e72cb23b90c6f915aa76251 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 12 Nov 2018 11:43:51 +0700 Subject: [PATCH] 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