request header callback

This commit is contained in:
Dustin Brody 2022-03-04 19:13:29 +00:00
parent ad0c3fb6e0
commit b455958d8a
No known key found for this signature in database
GPG Key ID: 3D7A11A0156519DC
3 changed files with 30 additions and 9 deletions

View File

@ -19,6 +19,8 @@ type
Response* = JsonNode Response* = JsonNode
GetJsonRpcRequestHeaders* = proc(): seq[(string, string)] {.gcsafe.}
proc getNextId*(client: RpcClient): ClientId = proc getNextId*(client: RpcClient): ClientId =
client.lastId += 1 client.lastId += 1
client.lastId client.lastId

View File

@ -19,11 +19,14 @@ type
httpSession: HttpSessionRef httpSession: HttpSessionRef
httpAddress: HttpResult[HttpAddress] httpAddress: HttpResult[HttpAddress]
maxBodySize: int maxBodySize: int
getHeaders: GetJsonRpcRequestHeaders
const const
MaxHttpRequestSize = 128 * 1024 * 1024 # maximum size of HTTP body in octets 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: let httpSessionFlags = if secure:
{ {
HttpClientFlag.NoVerifyHost, HttpClientFlag.NoVerifyHost,
@ -34,11 +37,14 @@ proc new(T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false
T( T(
maxBodySize: maxBodySize, maxBodySize: maxBodySize,
httpSession: HttpSessionRef.new(flags = httpSessionFlags) httpSession: HttpSessionRef.new(flags = httpSessionFlags),
getHeaders: getHeaders
) )
proc newRpcHttpClient*(maxBodySize = MaxHttpRequestSize, secure = false): RpcHttpClient = proc newRpcHttpClient*(
RpcHttpClient.new(maxBodySize, secure) maxBodySize = MaxHttpRequestSize, secure = false,
getHeaders: GetJsonRpcRequestHeaders = nil): RpcHttpClient =
RpcHttpClient.new(maxBodySize, secure, getHeaders)
method call*(client: RpcHttpClient, name: string, method call*(client: RpcHttpClient, name: string,
params: JsonNode): Future[Response] params: JsonNode): Future[Response]
@ -47,13 +53,20 @@ method call*(client: RpcHttpClient, name: string,
if client.httpAddress.isErr: if client.httpAddress.isErr:
raise newException(RpcAddressUnresolvableError, client.httpAddress.error) raise newException(RpcAddressUnresolvableError, client.httpAddress.error)
var headers =
if not isNil(client.getHeaders):
client.getHeaders()
else:
@[]
headers.add(("Content-Type", "application/json"))
let let
id = client.getNextId() id = client.getNextId()
reqBody = $rpcCallNode(name, params, id) reqBody = $rpcCallNode(name, params, id)
req = HttpClientRequestRef.post(client.httpSession, req = HttpClientRequestRef.post(client.httpSession,
client.httpAddress.get, client.httpAddress.get,
body = reqBody.toOpenArrayByte(0, reqBody.len - 1), body = reqBody.toOpenArrayByte(0, reqBody.len - 1),
headers = [("Content-Type", "application/json")]) headers = headers)
res = res =
try: try:
await req.send() await req.send()

View File

@ -18,6 +18,7 @@ when useNews:
transport*: WebSocket transport*: WebSocket
uri*: string uri*: string
loop*: Future[void] loop*: Future[void]
getHeaders*: GetJsonRpcRequestHeaders
else: else:
import std/[uri, strutils] import std/[uri, strutils]
@ -28,13 +29,16 @@ else:
transport*: WSSession transport*: WSSession
uri*: Uri uri*: Uri
loop*: Future[void] loop*: Future[void]
getHeaders*: GetJsonRpcRequestHeaders
proc new*(T: type RpcWebSocketClient): T = proc new*(
T() T: type RpcWebSocketClient, getHeaders: GetJsonRpcRequestHeaders = nil): T =
T(getHeaders: getHeaders)
proc newRpcWebSocketClient*: RpcWebSocketClient = proc newRpcWebSocketClient*(
getHeaders: GetJsonRpcRequestHeaders = nil): RpcWebSocketClient =
## Creates a new client instance. ## Creates a new client instance.
RpcWebSocketClient.new() RpcWebSocketClient.new(getHeaders)
method call*(self: RpcWebSocketClient, name: string, method call*(self: RpcWebSocketClient, name: string,
params: JsonNode): Future[Response] {. 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 # TODO: This is a hack, because the table might be case sensitive. Ideally strtabs module has
# to be extended with case insensitive accessors. # to be extended with case insensitive accessors.
headers["Origin"] = "http://localhost" headers["Origin"] = "http://localhost"
for header in client.getHeaders():
headers[header[0]] = header[1]
client.transport = await newWebSocket(uri, headers) client.transport = await newWebSocket(uri, headers)
client.uri = uri client.uri = uri
client.loop = processData(client) client.loop = processData(client)