|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
| 9 | +#if canImport(FoundationNetworking) |
| 10 | +import FoundationNetworking |
| 11 | +#endif |
9 | 12 |
|
10 | 13 | public final class APIClient {
|
11 | 14 | typealias HTTPResponse = (data: Data, response: HTTPURLResponse)
|
@@ -95,12 +98,41 @@ public final class APIClient {
|
95 | 98 | request.httpBody = body
|
96 | 99 | }
|
97 | 100 |
|
| 101 | + #if canImport(FoundationNetworking) |
| 102 | + return try await requestWithFoundationNetworking(request) |
| 103 | + #else |
| 104 | + return try await requestWithFoundation(request) |
| 105 | + #endif |
| 106 | + } |
| 107 | + |
| 108 | + #if canImport(FoundationNetworking) |
| 109 | + private func requestWithFoundationNetworking(_ request: URLRequest) async throws -> HTTPResponse { |
| 110 | + var requestError: Error? |
| 111 | + let httpResponse = await withCheckedContinuation { continuation in |
| 112 | + URLSession.shared.dataTask(with: request) { data, urlResponse, error in |
| 113 | + guard let data = data, let reponse = urlResponse as? HTTPURLResponse else { |
| 114 | + requestError = error |
| 115 | + return |
| 116 | + } |
| 117 | + continuation.resume(returning: (data, reponse)) |
| 118 | + } |
| 119 | + .resume() |
| 120 | + } |
| 121 | + if let requestError = requestError { |
| 122 | + throw requestError |
| 123 | + } |
| 124 | + return httpResponse |
| 125 | + } |
| 126 | + |
| 127 | + #else |
| 128 | + private func requestWithFoundation(_ request: URLRequest) async throws -> HTTPResponse { |
98 | 129 | let (data, response) = try await URLSession.shared.data(for: request)
|
99 | 130 | guard let response = response as? HTTPURLResponse else {
|
100 | 131 | throw VRCKitError.invalidResponse
|
101 | 132 | }
|
102 | 133 | return (data, response)
|
103 | 134 | }
|
| 135 | + #endif |
104 | 136 | }
|
105 | 137 |
|
106 | 138 | extension APIClient.Method: CustomStringConvertible {
|
|
0 commit comments