Skip to content

Commit

Permalink
fix URLSession
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Oct 23, 2024
1 parent c583270 commit 6617fc1
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Sources/Auth/AuthClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ extension AuthClient {
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
Expand Down Expand Up @@ -125,9 +125,9 @@ extension AuthClient {
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
Expand Down
8 changes: 4 additions & 4 deletions Sources/Auth/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ extension AuthClient.Configuration {
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping AuthClient.FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
} ) {
self.init(
Expand Down Expand Up @@ -121,9 +121,9 @@ extension AuthClient {
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping AuthClient.FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
}
) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public final class FunctionsClient: Sendable {
logger: (any SupabaseLogger)? = nil,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
}
) {
Expand Down Expand Up @@ -101,9 +101,9 @@ public final class FunctionsClient: Sendable {
logger: (any SupabaseLogger)? = nil,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
}
) {
Expand Down
53 changes: 52 additions & 1 deletion Sources/Helpers/URLSession+AsyncAwait.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
import Foundation

#if (os(Linux) || os(Windows)) && compiler(<6)
import HTTPTypes
import HTTPTypesFoundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

private enum HTTPTypeConversionError: Error {
case failedToConvertHTTPRequestToURLRequest
case failedToConvertURLResponseToHTTPResponse
}

extension URLSession {
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
///
/// - Parameter request: The `HTTPRequest` for which to load data.
/// - Returns: Data and response.
public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) {
guard let urlRequest = URLRequest(httpRequest: request) else {
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
}
let (data, urlResponse) = try await self.data(for: urlRequest)
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
}
return (data, response)
}

/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
///
/// - Parameter request: The `HTTPRequest` for which to upload data.
/// - Parameter bodyData: Data to upload.
/// - Returns: Data and response.
public func upload(
for request: HTTPRequest,
from bodyData: Data
) async throws -> (Data, HTTPResponse) {
guard let urlRequest = URLRequest(httpRequest: request) else {
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
}
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData)
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
}
return (data, response)
}
}
#endif

#if canImport(FoundationNetworking) && compiler(<6)
import Foundation
import FoundationNetworking

/// A set of errors that can be returned from the
Expand Down
9 changes: 5 additions & 4 deletions Sources/PostgREST/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import Helpers
import HTTPTypes
import HTTPTypesFoundation

Expand Down Expand Up @@ -33,9 +34,9 @@ extension PostgrestClient.Configuration {
headers: [String: String] = [:],
fetch: @escaping PostgrestClient.FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
Expand Down Expand Up @@ -73,9 +74,9 @@ extension PostgrestClient {
headers: [String: String] = [:],
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
Expand Down
8 changes: 4 additions & 4 deletions Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public final class PostgrestClient: Sendable {
logger: (any SupabaseLogger)? = nil,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
Expand Down Expand Up @@ -92,9 +92,9 @@ public final class PostgrestClient: Sendable {
logger: (any SupabaseLogger)? = nil,
fetch: @escaping FetchHandler = { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ public class RealtimeClient: PhoenixTransportDelegate {
http = HTTPClient(
fetch: { request, body in
if let body {
try await URLSession.shared.upload(for: request, from: body)
return try await URLSession.shared.upload(for: request, from: body)
} else {
try await URLSession.shared.data(for: request)
return try await URLSession.shared.data(for: request)
}
},
interceptors: []
Expand Down
5 changes: 3 additions & 2 deletions Sources/Storage/StorageHTTPClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import HTTPTypes
import Helpers

#if canImport(FoundationNetworking)
import FoundationNetworking
Expand All @@ -21,9 +22,9 @@ public struct StorageHTTPSession: Sendable {
self.init(
fetch: { request, body in
if let body {
try await session.upload(for: request, from: body)
return try await session.upload(for: request, from: body)
} else {
try await session.data(for: request)
return try await session.data(for: request)
}
}
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public final class SupabaseClient: Sendable {
fetch: { request, body in
// DON'T use `fetchWithAuth` method within the AuthClient as it may cause a deadlock.
if let body {
try await options.global.session.upload(for: request, from: body)
return try await options.global.session.upload(for: request, from: body)
} else {
try await options.global.session.data(for: request)
return try await options.global.session.data(for: request)
}
},
autoRefreshToken: options.auth.autoRefreshToken
Expand Down
1 change: 1 addition & 0 deletions Tests/IntegrationTests/AuthClientIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ConcurrencyExtras
import CustomDump
import HTTPTypes
import Helpers
import TestHelpers
import XCTest

Expand Down

0 comments on commit 6617fc1

Please sign in to comment.