@@ -10,8 +10,6 @@ import BigInt
10
10
11
11
/// Providers abstraction for custom providers (websockets, other custom private key managers). At the moment should not be used.
12
12
public protocol Web3Provider {
13
- // func sendAsync(_ request: JSONRPCrequest) async throws -> JSONRPCresponse
14
- // func sendAsync(_ requests: JSONRPCrequestBatch) async throws -> JSONRPCresponseBatch
15
13
var network : Networks ? { get set }
16
14
var attachedKeystoreManager : KeystoreManager ? { get set }
17
15
var url : URL { get }
@@ -28,79 +26,25 @@ public class Web3HttpProvider: Web3Provider {
28
26
let urlSession = URLSession ( configuration: config)
29
27
return urlSession
30
28
} ( )
31
- public init ? ( _ httpProviderURL: URL , network net: Networks , keystoreManager manager: KeystoreManager ? = nil ) {
29
+ public init ? ( _ httpProviderURL: URL , network net: Networks ? , keystoreManager manager: KeystoreManager ? = nil ) async {
32
30
guard httpProviderURL. scheme == " http " || httpProviderURL. scheme == " https " else { return nil }
33
31
url = httpProviderURL
34
- network = net
35
- attachedKeystoreManager = manager
36
- }
37
-
38
- // fileprivate static func dataFrom(session: URLSession, request urlRequest: URLRequest) async throws -> Data{
39
- // let (data, _) = try await session.data(for: urlRequest)
40
- // return data
41
- // }
42
- //
43
- // static func post<T: Decodable, U: Encodable>(_ request: U, providerURL: URL, session: URLSession) async throws -> T {
44
- // let requestData = try JSONEncoder().encode(request)
45
- // var urlRequest = URLRequest(url: providerURL, cachePolicy: .reloadIgnoringCacheData)
46
- // urlRequest.httpMethod = "POST"
47
- // urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
48
- // urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
49
- // urlRequest.httpBody = requestData
50
- //
51
- // let data = try await dataFrom(session: session, request: urlRequest)
52
- //
53
- // let parsedResponse = try JSONDecoder().decode(T.self, from: data)
54
- //
55
- // if let response = parsedResponse as? JSONRPCresponse, response.error != nil {
56
- // throw Web3Error.nodeError(desc: "Received an error message from node\n" + String(describing: response.error!))
57
- // }
58
- // return parsedResponse
59
- //
60
- // }
61
- //
62
- // public func sendAsync(_ request: JSONRPCrequest) async throws -> JSONRPCresponse {
63
- // guard request.method != nil else {
64
- // throw Web3Error.nodeError(desc: "RPC method is nill")
65
- // }
66
- //
67
- // return try await Web3HttpProvider.post(request, providerURL: self.url, session: self.session)
68
- // }
69
-
70
- // public func sendAsync(_ requests: JSONRPCrequestBatch) async throws -> JSONRPCresponseBatch {
71
- // return try await Web3HttpProvider.post(requests, providerURL: self.url, session: self.session)
72
- // }
73
- }
74
-
75
-
76
- @available ( iOS, obsoleted: 15.0 , message: " Use the built-in API instead " )
77
- @available ( macOS, obsoleted: 12.0 , message: " Use the built-in API instead " )
78
- extension URLSession {
79
- func data( fromUrl url: URL ) async throws -> ( Data , URLResponse ) {
80
- try await withCheckedThrowingContinuation { continuation in
81
- let task = self . dataTask ( with: url) { data, response, error in
82
- guard let data = data, let response = response else {
83
- let error = error ?? URLError ( . badServerResponse)
84
- return continuation. resume ( throwing: error)
85
- }
86
- continuation. resume ( returning: ( data, response) )
87
- }
88
- task. resume ( )
89
- }
90
- }
91
-
92
- func data( for request: URLRequest ) async throws -> ( Data , URLResponse ) {
93
- var dataTask : URLSessionDataTask ?
94
-
95
- return try await withCheckedThrowingContinuation { continuation in
96
- dataTask = self . dataTask ( with: request) { data, response, error in
97
- guard let data = data, let response = response else {
98
- let error = error ?? URLError ( . badServerResponse)
99
- return continuation. resume ( throwing: error)
100
- }
101
- continuation. resume ( returning: ( data, response) )
32
+ if let net = net {
33
+ network = net
34
+ } else {
35
+ var urlRequest = URLRequest ( url: url, cachePolicy: . reloadIgnoringCacheData)
36
+ urlRequest. setValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
37
+ urlRequest. setValue ( " application/json " , forHTTPHeaderField: " Accept " )
38
+ urlRequest. httpMethod = APIRequest . getNetwork. call
39
+ urlRequest. httpBody = APIRequest . getNetwork. encodedBody
40
+ do {
41
+ let response : APIResponse < UInt > = try await APIRequest . send ( uRLRequest: urlRequest, with: session)
42
+ let network = Networks . fromInt ( response. result)
43
+ self . network = network
44
+ } catch {
45
+ return nil
102
46
}
103
- dataTask? . resume ( )
104
47
}
48
+ attachedKeystoreManager = manager
105
49
}
106
50
}
0 commit comments