Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ethereum http client accept http headers #361

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
8 changes: 4 additions & 4 deletions web3swift/src/Client/Models/EthereumBlockInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Copyright © 2022 Argent Labs Limited. All rights reserved.
//

import Foundation
import BigInt
import Foundation

public struct EthereumBlockInfo: Equatable {
public var number: EthereumBlock
Expand Down Expand Up @@ -40,15 +40,15 @@ extension EthereumBlockInfo: Codable {
guard let transactions = try? container.decode([String].self, forKey: .transactions) else {
throw JSONRPCError.decodingError
}

guard let gasLimit = try? container.decode(String.self, forKey: .gasLimit) else {
throw JSONRPCError.decodingError
}

guard let gasUsed = try? container.decode(String.self, forKey: .gasUsed) else {
throw JSONRPCError.decodingError
}

let baseFeePerGas = try? container.decode(String.self, forKey: .baseFeePerGas)

self.number = number
Expand Down
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
private let headers: [String: String]

public init(session: URLSession, url: URL) {
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
Loading