Skip to content

Commit 6617fc1

Browse files
committed
fix URLSession
1 parent c583270 commit 6617fc1

10 files changed

+81
-27
lines changed

Sources/Auth/AuthClientConfiguration.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ extension AuthClient {
7676
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
7777
fetch: @escaping FetchHandler = { request, body in
7878
if let body {
79-
try await URLSession.shared.upload(for: request, from: body)
79+
return try await URLSession.shared.upload(for: request, from: body)
8080
} else {
81-
try await URLSession.shared.data(for: request)
81+
return try await URLSession.shared.data(for: request)
8282
}
8383
},
8484
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
@@ -125,9 +125,9 @@ extension AuthClient {
125125
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
126126
fetch: @escaping FetchHandler = { request, body in
127127
if let body {
128-
try await URLSession.shared.upload(for: request, from: body)
128+
return try await URLSession.shared.upload(for: request, from: body)
129129
} else {
130-
try await URLSession.shared.data(for: request)
130+
return try await URLSession.shared.data(for: request)
131131
}
132132
},
133133
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken

Sources/Auth/Deprecated.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ extension AuthClient.Configuration {
7878
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
7979
fetch: @escaping AuthClient.FetchHandler = { request, body in
8080
if let body {
81-
try await URLSession.shared.upload(for: request, from: body)
81+
return try await URLSession.shared.upload(for: request, from: body)
8282
} else {
83-
try await URLSession.shared.data(for: request)
83+
return try await URLSession.shared.data(for: request)
8484
}
8585
} ) {
8686
self.init(
@@ -121,9 +121,9 @@ extension AuthClient {
121121
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
122122
fetch: @escaping AuthClient.FetchHandler = { request, body in
123123
if let body {
124-
try await URLSession.shared.upload(for: request, from: body)
124+
return try await URLSession.shared.upload(for: request, from: body)
125125
} else {
126-
try await URLSession.shared.data(for: request)
126+
return try await URLSession.shared.data(for: request)
127127
}
128128
}
129129
) {

Sources/Functions/FunctionsClient.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public final class FunctionsClient: Sendable {
5252
logger: (any SupabaseLogger)? = nil,
5353
fetch: @escaping FetchHandler = { request, body in
5454
if let body {
55-
try await URLSession.shared.upload(for: request, from: body)
55+
return try await URLSession.shared.upload(for: request, from: body)
5656
} else {
57-
try await URLSession.shared.data(for: request)
57+
return try await URLSession.shared.data(for: request)
5858
}
5959
}
6060
) {
@@ -101,9 +101,9 @@ public final class FunctionsClient: Sendable {
101101
logger: (any SupabaseLogger)? = nil,
102102
fetch: @escaping FetchHandler = { request, body in
103103
if let body {
104-
try await URLSession.shared.upload(for: request, from: body)
104+
return try await URLSession.shared.upload(for: request, from: body)
105105
} else {
106-
try await URLSession.shared.data(for: request)
106+
return try await URLSession.shared.data(for: request)
107107
}
108108
}
109109
) {

Sources/Helpers/URLSession+AsyncAwait.swift

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
1+
import Foundation
2+
3+
#if (os(Linux) || os(Windows)) && compiler(<6)
4+
import HTTPTypes
5+
import HTTPTypesFoundation
6+
7+
#if canImport(FoundationNetworking)
8+
import FoundationNetworking
9+
#endif
10+
11+
private enum HTTPTypeConversionError: Error {
12+
case failedToConvertHTTPRequestToURLRequest
13+
case failedToConvertURLResponseToHTTPResponse
14+
}
15+
16+
extension URLSession {
17+
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
18+
///
19+
/// - Parameter request: The `HTTPRequest` for which to load data.
20+
/// - Returns: Data and response.
21+
public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) {
22+
guard let urlRequest = URLRequest(httpRequest: request) else {
23+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
24+
}
25+
let (data, urlResponse) = try await self.data(for: urlRequest)
26+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
27+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
28+
}
29+
return (data, response)
30+
}
31+
32+
/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
33+
///
34+
/// - Parameter request: The `HTTPRequest` for which to upload data.
35+
/// - Parameter bodyData: Data to upload.
36+
/// - Returns: Data and response.
37+
public func upload(
38+
for request: HTTPRequest,
39+
from bodyData: Data
40+
) async throws -> (Data, HTTPResponse) {
41+
guard let urlRequest = URLRequest(httpRequest: request) else {
42+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
43+
}
44+
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData)
45+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
46+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
47+
}
48+
return (data, response)
49+
}
50+
}
51+
#endif
52+
153
#if canImport(FoundationNetworking) && compiler(<6)
2-
import Foundation
354
import FoundationNetworking
455

556
/// A set of errors that can be returned from the

Sources/PostgREST/Deprecated.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Foundation
9+
import Helpers
910
import HTTPTypes
1011
import HTTPTypesFoundation
1112

@@ -33,9 +34,9 @@ extension PostgrestClient.Configuration {
3334
headers: [String: String] = [:],
3435
fetch: @escaping PostgrestClient.FetchHandler = { request, body in
3536
if let body {
36-
try await URLSession.shared.upload(for: request, from: body)
37+
return try await URLSession.shared.upload(for: request, from: body)
3738
} else {
38-
try await URLSession.shared.data(for: request)
39+
return try await URLSession.shared.data(for: request)
3940
}
4041
},
4142
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
@@ -73,9 +74,9 @@ extension PostgrestClient {
7374
headers: [String: String] = [:],
7475
fetch: @escaping FetchHandler = { request, body in
7576
if let body {
76-
try await URLSession.shared.upload(for: request, from: body)
77+
return try await URLSession.shared.upload(for: request, from: body)
7778
} else {
78-
try await URLSession.shared.data(for: request)
79+
return try await URLSession.shared.data(for: request)
7980
}
8081
},
8182
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,

Sources/PostgREST/PostgrestClient.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public final class PostgrestClient: Sendable {
4646
logger: (any SupabaseLogger)? = nil,
4747
fetch: @escaping FetchHandler = { request, body in
4848
if let body {
49-
try await URLSession.shared.upload(for: request, from: body)
49+
return try await URLSession.shared.upload(for: request, from: body)
5050
} else {
51-
try await URLSession.shared.data(for: request)
51+
return try await URLSession.shared.data(for: request)
5252
}
5353
},
5454
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
@@ -92,9 +92,9 @@ public final class PostgrestClient: Sendable {
9292
logger: (any SupabaseLogger)? = nil,
9393
fetch: @escaping FetchHandler = { request, body in
9494
if let body {
95-
try await URLSession.shared.upload(for: request, from: body)
95+
return try await URLSession.shared.upload(for: request, from: body)
9696
} else {
97-
try await URLSession.shared.data(for: request)
97+
return try await URLSession.shared.data(for: request)
9898
}
9999
},
100100
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,

Sources/Realtime/RealtimeClient.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ public class RealtimeClient: PhoenixTransportDelegate {
238238
http = HTTPClient(
239239
fetch: { request, body in
240240
if let body {
241-
try await URLSession.shared.upload(for: request, from: body)
241+
return try await URLSession.shared.upload(for: request, from: body)
242242
} else {
243-
try await URLSession.shared.data(for: request)
243+
return try await URLSession.shared.data(for: request)
244244
}
245245
},
246246
interceptors: []

Sources/Storage/StorageHTTPClient.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import HTTPTypes
3+
import Helpers
34

45
#if canImport(FoundationNetworking)
56
import FoundationNetworking
@@ -21,9 +22,9 @@ public struct StorageHTTPSession: Sendable {
2122
self.init(
2223
fetch: { request, body in
2324
if let body {
24-
try await session.upload(for: request, from: body)
25+
return try await session.upload(for: request, from: body)
2526
} else {
26-
try await session.data(for: request)
27+
return try await session.data(for: request)
2728
}
2829
}
2930
)

Sources/Supabase/SupabaseClient.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ public final class SupabaseClient: Sendable {
180180
fetch: { request, body in
181181
// DON'T use `fetchWithAuth` method within the AuthClient as it may cause a deadlock.
182182
if let body {
183-
try await options.global.session.upload(for: request, from: body)
183+
return try await options.global.session.upload(for: request, from: body)
184184
} else {
185-
try await options.global.session.data(for: request)
185+
return try await options.global.session.data(for: request)
186186
}
187187
},
188188
autoRefreshToken: options.auth.autoRefreshToken

Tests/IntegrationTests/AuthClientIntegrationTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ConcurrencyExtras
99
import CustomDump
1010
import HTTPTypes
11+
import Helpers
1112
import TestHelpers
1213
import XCTest
1314

0 commit comments

Comments
 (0)