request header callback
This commit is contained in:
parent
ad0c3fb6e0
commit
b455958d8a
|
@ -19,6 +19,8 @@ type
|
|||
|
||||
Response* = JsonNode
|
||||
|
||||
GetJsonRpcRequestHeaders* = proc(): seq[(string, string)] {.gcsafe.}
|
||||
|
||||
proc getNextId*(client: RpcClient): ClientId =
|
||||
client.lastId += 1
|
||||
client.lastId
|
||||
|
|
|
@ -19,11 +19,14 @@ type
|
|||
httpSession: HttpSessionRef
|
||||
httpAddress: HttpResult[HttpAddress]
|
||||
maxBodySize: int
|
||||
getHeaders: GetJsonRpcRequestHeaders
|
||||
|
||||
const
|
||||
MaxHttpRequestSize = 128 * 1024 * 1024 # maximum size of HTTP body in octets
|
||||
|
||||
proc new(T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false): T =
|
||||
proc new(
|
||||
T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false,
|
||||
getHeaders: GetJsonRpcRequestHeaders = nil): T =
|
||||
let httpSessionFlags = if secure:
|
||||
{
|
||||
HttpClientFlag.NoVerifyHost,
|
||||
|
@ -34,11 +37,14 @@ proc new(T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false
|
|||
|
||||
T(
|
||||
maxBodySize: maxBodySize,
|
||||
httpSession: HttpSessionRef.new(flags = httpSessionFlags)
|
||||
httpSession: HttpSessionRef.new(flags = httpSessionFlags),
|
||||
getHeaders: getHeaders
|
||||
)
|
||||
|
||||
proc newRpcHttpClient*(maxBodySize = MaxHttpRequestSize, secure = false): RpcHttpClient =
|
||||
RpcHttpClient.new(maxBodySize, secure)
|
||||
proc newRpcHttpClient*(
|
||||
maxBodySize = MaxHttpRequestSize, secure = false,
|
||||
getHeaders: GetJsonRpcRequestHeaders = nil): RpcHttpClient =
|
||||
RpcHttpClient.new(maxBodySize, secure, getHeaders)
|
||||
|
||||
method call*(client: RpcHttpClient, name: string,
|
||||
params: JsonNode): Future[Response]
|
||||
|
@ -47,13 +53,20 @@ method call*(client: RpcHttpClient, name: string,
|
|||
if client.httpAddress.isErr:
|
||||
raise newException(RpcAddressUnresolvableError, client.httpAddress.error)
|
||||
|
||||
var headers =
|
||||
if not isNil(client.getHeaders):
|
||||
client.getHeaders()
|
||||
else:
|
||||
@[]
|
||||
headers.add(("Content-Type", "application/json"))
|
||||
|
||||
let
|
||||
id = client.getNextId()
|
||||
reqBody = $rpcCallNode(name, params, id)
|
||||
req = HttpClientRequestRef.post(client.httpSession,
|
||||
client.httpAddress.get,
|
||||
body = reqBody.toOpenArrayByte(0, reqBody.len - 1),
|
||||
headers = [("Content-Type", "application/json")])
|
||||
headers = headers)
|
||||
res =
|
||||
try:
|
||||
await req.send()
|
||||
|
|
|
@ -18,6 +18,7 @@ when useNews:
|
|||
transport*: WebSocket
|
||||
uri*: string
|
||||
loop*: Future[void]
|
||||
getHeaders*: GetJsonRpcRequestHeaders
|
||||
|
||||
else:
|
||||
import std/[uri, strutils]
|
||||
|
@ -28,13 +29,16 @@ else:
|
|||
transport*: WSSession
|
||||
uri*: Uri
|
||||
loop*: Future[void]
|
||||
getHeaders*: GetJsonRpcRequestHeaders
|
||||
|
||||
proc new*(T: type RpcWebSocketClient): T =
|
||||
T()
|
||||
proc new*(
|
||||
T: type RpcWebSocketClient, getHeaders: GetJsonRpcRequestHeaders = nil): T =
|
||||
T(getHeaders: getHeaders)
|
||||
|
||||
proc newRpcWebSocketClient*: RpcWebSocketClient =
|
||||
proc newRpcWebSocketClient*(
|
||||
getHeaders: GetJsonRpcRequestHeaders = nil): RpcWebSocketClient =
|
||||
## Creates a new client instance.
|
||||
RpcWebSocketClient.new()
|
||||
RpcWebSocketClient.new(getHeaders)
|
||||
|
||||
method call*(self: RpcWebSocketClient, name: string,
|
||||
params: JsonNode): Future[Response] {.
|
||||
|
@ -112,6 +116,8 @@ when useNews:
|
|||
# TODO: This is a hack, because the table might be case sensitive. Ideally strtabs module has
|
||||
# to be extended with case insensitive accessors.
|
||||
headers["Origin"] = "http://localhost"
|
||||
for header in client.getHeaders():
|
||||
headers[header[0]] = header[1]
|
||||
client.transport = await newWebSocket(uri, headers)
|
||||
client.uri = uri
|
||||
client.loop = processData(client)
|
||||
|
|
Loading…
Reference in New Issue