Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Nov 30, 2024
1 parent 6675151 commit 801d676
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ public final class AuthClient: Sendable {

if let jwt {
request.headerFields[.authorization] = "Bearer \(jwt)"
let (data, _) = try await api.execute(for: request, from: nil)
let (data, _) = try await api.execute(for: request, from: nil)
return try configuration.decoder.decode(User.self, from: data)
} else {
let (data, _) = try await api.authorizedExecute(for: request, from: nil)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Auth/Internal/SessionStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ extension StorageMigration {
let storedSession = try? AuthClient.Configuration.jsonDecoder.decode(
StoredSession.self,
from: data
) {
)
{
let session = try AuthClient.Configuration.jsonEncoder.encode(storedSession.session)
try storage.store(key: key, value: session)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ public final class FunctionsClient: Sendable {
.appendingQueryItems(options.query),
headerFields: mutableState.headers.merging(options.headers) { $1 }
)
if options.body != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}

if options.body != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}

if let region = options.region ?? region {
request.headerFields[.xRegion] = region
Expand Down
11 changes: 6 additions & 5 deletions Sources/Helpers/HTTP/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ package actor HTTPClient: HTTPClientType {
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse) {
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = { request, bodyData in
var request = request
if bodyData != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = {
request, bodyData in
var request = request
if bodyData != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}
return try await self.fetch(request, bodyData)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Helpers/HTTP/HTTPFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension HTTPFields {
) rethrows {
self = try self.merging(other, uniquingKeysWith: combine)
}

package func merging(
_ other: Self,
uniquingKeysWith combine: (String, String) throws -> String
Expand Down
107 changes: 55 additions & 52 deletions Sources/Helpers/HTTP/URLSession+HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@ import Foundation
import HTTPTypes

#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif

#if !os(WASI)

extension URLSessionTask {
extension URLSessionTask {
/// The original HTTP request this task was created with.
public var originalHTTPRequest: HTTPRequest? {
self.originalRequest?.httpRequest
self.originalRequest?.httpRequest
}

/// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection.
public var currentHTTPRequest: HTTPRequest? {
self.currentRequest?.httpRequest
self.currentRequest?.httpRequest
}

/// The HTTP response received from the server.
public var httpResponse: HTTPResponse? {
(self.response as? HTTPURLResponse)?.httpResponse
(self.response as? HTTPURLResponse)?.httpResponse
}
}
}

private enum HTTPTypeConversionError: Error {
private enum HTTPTypeConversionError: Error {
case failedToConvertHTTPRequestToURLRequest
case failedToConvertURLResponseToHTTPResponse
}
}

#endif

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

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension URLSession {
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
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.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func data(
for request: HTTPRequest,
delegate: (any URLSessionTaskDelegate)? = nil
for request: HTTPRequest,
delegate: (any URLSessionTaskDelegate)? = nil
) async throws -> (Data, HTTPResponse) {
guard let urlRequest = URLRequest(httpRequest: request) else {
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
}
let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate)
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
}
return (data, response)
guard let urlRequest = URLRequest(httpRequest: request) else {
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
}
let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate)
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.
Expand All @@ -61,53 +61,56 @@ extension URLSession {
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func upload(
for request: HTTPRequest,
from bodyData: Data,
delegate: (any URLSessionTaskDelegate)? = nil
for request: HTTPRequest,
from bodyData: Data,
delegate: (any URLSessionTaskDelegate)? = nil
) 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, delegate: delegate)
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
}
return (data, response)
guard let urlRequest = URLRequest(httpRequest: request) else {
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
}
let (data, urlResponse) = try await self.upload(
for: urlRequest, from: bodyData, delegate: delegate)
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
}
return (data, response)
}
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension URLSession {
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
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)
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)
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
4 changes: 2 additions & 2 deletions Sources/Realtime/PhoenixTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
url: url,
headerFields: headers
)

let urlRequest = URLRequest(httpRequest: request)!

task = session?.webSocketTask(with: urlRequest)

// Start the task
Expand Down
9 changes: 5 additions & 4 deletions Sources/Realtime/V2/RealtimeChannelV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ struct Socket: Sendable {
var addChannel: @Sendable (_ channel: RealtimeChannelV2) -> Void
var removeChannel: @Sendable (_ channel: RealtimeChannelV2) async -> Void
var push: @Sendable (_ message: RealtimeMessageV2) async -> Void
var httpSend: @Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)
var httpSend:
@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)
}

extension Socket {
Expand Down
21 changes: 13 additions & 8 deletions Sources/Realtime/V2/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ public struct RealtimeClientOptions: Sendable {
var timeoutInterval: TimeInterval
var disconnectOnSessionLoss: Bool
var connectOnSubscribe: Bool
var fetch: (@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse))?
var fetch:
(
@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)
)?
package var logger: (any SupabaseLogger)?

public static let defaultHeartbeatInterval: TimeInterval = 15
Expand All @@ -40,10 +43,12 @@ public struct RealtimeClientOptions: Sendable {
timeoutInterval: TimeInterval = Self.defaultTimeoutInterval,
disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss,
connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe,
fetch: (@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse))? = nil,
fetch: (
@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)
)? = nil,
logger: (any SupabaseLogger)? = nil
) {
self.headers = headers
Expand Down
9 changes: 5 additions & 4 deletions Sources/Storage/StorageHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import HTTPTypesFoundation
#endif

public struct StorageHTTPSession: Sendable {
public var fetch: @Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)
public var fetch:
@Sendable (
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse)

public init(
fetch: @escaping @Sendable (
Expand Down
6 changes: 3 additions & 3 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public final class SupabaseClient: Sendable {
return $0.functions!
}
}

/// Headers provided to the inner clients on initialization.
///
/// - Note: This collection is non-mutable, if you want to provide different headers, pass it in ``SupabaseClientOptions/GlobalOptions/headers``.
Expand Down Expand Up @@ -181,7 +181,7 @@ public final class SupabaseClient: Sendable {

_auth = AuthClient(
url: supabaseURL.appendingPathComponent("/auth/v1"),
headers: self.headers,
headers: self.headers,
flowType: options.auth.flowType,
redirectToURL: options.auth.redirectToURL,
storageKey: options.auth.storageKey ?? defaultStorageKey,
Expand Down Expand Up @@ -209,7 +209,7 @@ public final class SupabaseClient: Sendable {
)

var realtimeOptions = options.realtime
realtimeOptions.headers.merge(self.headers) { $1 }
realtimeOptions.headers.merge(self.headers) { $1 }
if realtimeOptions.logger == nil {
realtimeOptions.logger = options.global.logger
}
Expand Down
9 changes: 6 additions & 3 deletions Sources/TestHelpers/HTTPClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import ConcurrencyExtras
import Foundation
import HTTPTypes
import Helpers
import XCTestDynamicOverlay
import HTTPTypes

package actor HTTPClientMock: HTTPClientType {

package struct MockNotFound: Error {}

private var mocks = [@Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse)?]()
Expand Down Expand Up @@ -46,7 +46,10 @@ package actor HTTPClientMock: HTTPClientType {
when({ _, _ in true }, return: response)
}

package func send(_ request: HTTPRequest, _ bodyData: Data?) async throws -> (Data, HTTPResponse) {
package func send(
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse) {
receivedRequests.append((request, bodyData))

for mock in mocks {
Expand Down

0 comments on commit 801d676

Please sign in to comment.