@@ -10,18 +10,23 @@ public struct EtherscanTransactionChecker: TransactionChecker {
10
10
private let urlSession : URLSessionProxy
11
11
private let apiKey : String
12
12
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 ) {
14
19
self . urlSession = urlSession
15
20
self . apiKey = apiKey
16
21
}
17
22
18
23
public func hasTransactions( address: String ) async throws -> Bool {
19
24
let urlString = " https://api.etherscan.io/api?module=account&action=txlist&address= \( address) &startblock=0&page=1&offset=1&sort=asc&apikey= \( apiKey) "
20
25
guard let url = URL ( string: urlString) else {
21
- throw EtherscanTransactionCheckerError . invalidUrl
26
+ throw EtherscanTransactionCheckerError . invalidUrl ( url : urlString )
22
27
}
23
28
let request = URLRequest ( url: url)
24
- let result = try await urlSession. data ( request : request)
29
+ let result = try await urlSession. data ( for : request)
25
30
let response = try JSONDecoder ( ) . decode ( Response . self, from: result. 0 )
26
31
return !response. result. isEmpty
27
32
}
@@ -34,16 +39,25 @@ extension EtherscanTransactionChecker {
34
39
struct Transaction : Codable { }
35
40
}
36
41
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
+ }
39
51
}
40
52
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 )
43
55
}
44
56
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)
48
62
}
49
63
}
0 commit comments