@@ -10,18 +10,23 @@ public struct EtherscanTransactionChecker: TransactionChecker {
1010 private let urlSession : URLSessionProxy
1111 private let apiKey : String
1212
13- public init ( urlSession: URLSessionProxy , apiKey: String ) {
13+ public init ( urlSession: URLSession , apiKey: String ) {
14+ self . urlSession = URLSessionProxyImplementation ( urlSession: urlSession)
15+ self . apiKey = apiKey
16+ }
17+
18+ internal init ( urlSession: URLSessionProxy , apiKey: String ) {
1419 self . urlSession = urlSession
1520 self . apiKey = apiKey
1621 }
1722
1823 public func hasTransactions( address: String ) async throws -> Bool {
1924 let urlString = " https://api.etherscan.io/api?module=account&action=txlist&address= \( address) &startblock=0&page=1&offset=1&sort=asc&apikey= \( apiKey) "
2025 guard let url = URL ( string: urlString) else {
21- throw EtherscanTransactionCheckerError . invalidUrl
26+ throw EtherscanTransactionCheckerError . invalidUrl ( url : urlString )
2227 }
2328 let request = URLRequest ( url: url)
24- let result = try await urlSession. data ( request : request)
29+ let result = try await urlSession. data ( for : request)
2530 let response = try JSONDecoder ( ) . decode ( Response . self, from: result. 0 )
2631 return !response. result. isEmpty
2732 }
@@ -34,16 +39,25 @@ extension EtherscanTransactionChecker {
3439 struct Transaction : Codable { }
3540}
3641
37- public enum EtherscanTransactionCheckerError : Error {
38- case invalidUrl
42+ public enum EtherscanTransactionCheckerError : LocalizedError , Equatable {
43+ case invalidUrl( url: String )
44+
45+ public var errorDescription : String ? {
46+ switch self {
47+ case let . invalidUrl( url) :
48+ return " Couldn't create URL(string: \( url) ) "
49+ }
50+ }
3951}
4052
41- public protocol URLSessionProxy {
42- func data( request: URLRequest ) async throws -> ( Data , HTTPURLResponse )
53+ internal protocol URLSessionProxy {
54+ func data( for request: URLRequest ) async throws -> ( Data , URLResponse )
4355}
4456
45- extension URLSession : URLSessionProxy {
46- public func data( request: URLRequest ) async throws -> ( Data , HTTPURLResponse ) {
47- return try await data ( for: request)
57+ internal struct URLSessionProxyImplementation : URLSessionProxy {
58+ let urlSession : URLSession
59+
60+ func data( for request: URLRequest ) async throws -> ( Data , URLResponse ) {
61+ try await urlSession. data ( for: request)
4862 }
4963}
0 commit comments