Skip to content

Commit

Permalink
feat: ethereum http client accept http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick-ahnlabio committed Feb 6, 2024
1 parent c7a0348 commit 8286dfb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion web3swift/src/Client/HTTP/EthereumHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EthereumHttpClient: BaseEthereumClient {

public init(
url: URL,
headers: [String: String]? = nil,
sessionConfig: URLSessionConfiguration = URLSession.shared.configuration,
logger: Logger? = nil,
network: EthereumNetwork
Expand All @@ -25,6 +26,6 @@ public class EthereumHttpClient: BaseEthereumClient {
self.networkQueue = networkQueue

let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: networkQueue)
super.init(networkProvider: HttpNetworkProvider(session: session, url: url), url: url, logger: logger, network: network)
super.init(networkProvider: HttpNetworkProvider(session: session, url: url, headers: headers), url: url, logger: logger, network: network)
}
}
10 changes: 8 additions & 2 deletions web3swift/src/Client/NetworkProviders/HttpNetworkProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import Foundation
public class HttpNetworkProvider: NetworkProviderProtocol {
public let session: URLSession
private let url: URL

public init(session: URLSession, url: URL) {
private let headers: [String: String]

public init(session: URLSession, url: URL, headers: [String: String]? = nil) {
self.session = session
self.url = url
self.headers = headers ?? [:]
}

deinit {
Expand All @@ -33,6 +35,10 @@ public class HttpNetworkProvider: NetworkProviderProtocol {
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

headers.forEach { key, value in
request.addValue(value, forHTTPHeaderField: key)
}

let id = 1
let rpcRequest = JSONRPCRequest(jsonrpc: "2.0", method: method, params: params, id: id)
guard let encoded = try? JSONEncoder().encode(rpcRequest) else {
Expand Down

0 comments on commit 8286dfb

Please sign in to comment.