Skip to content

Commit 9e28c97

Browse files
committed
EtherscanTransactionCheckerError conforms to LicalizedError, changed URLSessionProxy from public to internal
1 parent 73e2c1b commit 9e28c97

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

Sources/Web3Core/KeystoreManager/EtherscanTransactionChecker.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/web3swiftTests/remoteTests/EtherscanTransactionCheckerTests.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
4040
XCTAssertTrue(true)
4141
}
4242
}
43+
44+
func testInitURLError() async throws {
45+
do {
46+
let sut = EtherscanTransactionChecker(urlSession: URLSessionMock(), apiKey: testApiKey)
47+
48+
_ = try await sut.hasTransactions(address: " ")
49+
50+
XCTFail("URL init must throw an error")
51+
} catch {
52+
XCTAssertTrue(error is EtherscanTransactionCheckerError)
53+
}
54+
}
4355

4456
func testWrongApiKey() async throws {
4557
do {
@@ -54,10 +66,22 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
5466
}
5567
}
5668

57-
final class URLSessionMock: URLSessionProxy {
58-
var response: (Data, HTTPURLResponse) = (Data(), HTTPURLResponse())
5969

60-
func data(request: URLRequest) async throws -> (Data, HTTPURLResponse) {
70+
// MARK: - EtherscanTransactionCheckerErrorTests
71+
72+
final class EtherscanTransactionCheckerErrorTests: XCTestCase {
73+
func testLocalizedDescription() {
74+
let error = EtherscanTransactionCheckerError.invalidUrl(url: "mock url")
75+
XCTAssertEqual(error.localizedDescription, "Couldn't create URL(string: mock url)")
76+
}
77+
}
78+
79+
// MARK: - test double
80+
81+
final private class URLSessionMock: URLSessionProxy {
82+
var response: (Data, URLResponse) = (Data(), URLResponse())
83+
84+
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
6185
return response
6286
}
6387
}

0 commit comments

Comments
 (0)