mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-05-23 02:49:29 +00:00
Merge pull request #41 from jangko/add_http_method
fixes #37 add http method get/set to httpclient
This commit is contained in:
commit
a14e2dbcbb
@ -6,9 +6,13 @@ logScope:
|
|||||||
topic = "JSONRPC-HTTP-CLIENT"
|
topic = "JSONRPC-HTTP-CLIENT"
|
||||||
|
|
||||||
type
|
type
|
||||||
|
HttpClientOptions* = object
|
||||||
|
httpMethod: HttpMethod
|
||||||
|
|
||||||
RpcHttpClient* = ref object of RpcClient
|
RpcHttpClient* = ref object of RpcClient
|
||||||
transp*: StreamTransport
|
transp*: StreamTransport
|
||||||
addresses: seq[TransportAddress]
|
addresses: seq[TransportAddress]
|
||||||
|
options: HttpClientOptions
|
||||||
|
|
||||||
const
|
const
|
||||||
MaxHttpHeadersSize = 8192 # maximum size of HTTP headers in octets
|
MaxHttpHeadersSize = 8192 # maximum size of HTTP headers in octets
|
||||||
@ -18,8 +22,8 @@ const
|
|||||||
HeadersMark = @[byte(0x0D), byte(0x0A), byte(0x0D), byte(0x0A)]
|
HeadersMark = @[byte(0x0D), byte(0x0A), byte(0x0D), byte(0x0A)]
|
||||||
|
|
||||||
proc sendRequest(transp: StreamTransport,
|
proc sendRequest(transp: StreamTransport,
|
||||||
data: string): Future[bool] {.async.} =
|
data: string, httpMethod: HttpMethod): Future[bool] {.async.} =
|
||||||
var request = "GET / "
|
var request = $httpMethod & " / "
|
||||||
request.add($HttpVersion11)
|
request.add($HttpVersion11)
|
||||||
request.add("\r\n")
|
request.add("\r\n")
|
||||||
request.add("Date: " & httpDate() & "\r\n")
|
request.add("Date: " & httpDate() & "\r\n")
|
||||||
@ -134,13 +138,23 @@ proc recvData(transp: StreamTransport): Future[string] {.async.} =
|
|||||||
else:
|
else:
|
||||||
result = cast[string](buffer)
|
result = cast[string](buffer)
|
||||||
|
|
||||||
|
proc init(opts: var HttpClientOptions) =
|
||||||
|
opts.httpMethod = MethodGet
|
||||||
|
|
||||||
proc newRpcHttpClient*(): RpcHttpClient =
|
proc newRpcHttpClient*(): RpcHttpClient =
|
||||||
## Creates a new HTTP client instance.
|
## Creates a new HTTP client instance.
|
||||||
new result
|
new result
|
||||||
result.initRpcClient()
|
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,
|
proc call*(client: RpcHttpClient, name: string,
|
||||||
params: JsonNode): Future[Response] {.async.} =
|
params: JsonNode, httpMethod: HttpMethod): Future[Response] {.async.} =
|
||||||
## Remotely calls the specified RPC method.
|
## Remotely calls the specified RPC method.
|
||||||
let id = client.getNextId()
|
let id = client.getNextId()
|
||||||
|
|
||||||
@ -148,7 +162,7 @@ proc call*(client: RpcHttpClient, name: string,
|
|||||||
if isNil(client.transp) or client.transp.closed():
|
if isNil(client.transp) or client.transp.closed():
|
||||||
raise newException(ValueError,
|
raise newException(ValueError,
|
||||||
"Transport is not initialised or already closed")
|
"Transport is not initialised or already closed")
|
||||||
let res = await client.transp.sendRequest(value)
|
let res = await client.transp.sendRequest(value, httpMethod)
|
||||||
if not res:
|
if not res:
|
||||||
debug "Failed to send message to RPC server",
|
debug "Failed to send message to RPC server",
|
||||||
address = client.transp.remoteAddress(), msg_len = res
|
address = client.transp.remoteAddress(), msg_len = res
|
||||||
@ -164,6 +178,10 @@ proc call*(client: RpcHttpClient, name: string,
|
|||||||
client.awaiting[id] = newFut
|
client.awaiting[id] = newFut
|
||||||
result = await newFut
|
result = await newFut
|
||||||
|
|
||||||
|
template call*(client: RpcHttpClient, name: string,
|
||||||
|
params: JsonNode): untyped =
|
||||||
|
client.call(name, params, client.httpMethod)
|
||||||
|
|
||||||
proc processData(client: RpcHttpClient) {.async.} =
|
proc processData(client: RpcHttpClient) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
var value = await client.transp.recvData()
|
var value = await client.transp.recvData()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user