Skip to content

Commit

Permalink
1.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Jun 15, 2024
1 parent b894f2f commit cc20ea0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
44 changes: 33 additions & 11 deletions Sources/SwiftAPIClient/Types/HTTPRequestComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ public struct HTTPRequestComponents: Sendable, Hashable {
/// - headers: The headers of the request.
/// - body: The body of the request.
public init(
url: URL,
url: URL?,
method: HTTPRequest.Method = .get,
headers: HTTPFields = [:],
body: RequestBody? = nil
) {
if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
if let url, let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
self.init(urlComponents: urlComponents, method: method, headers: headers, body: body)
} else {
} else if let url {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
self.init(
scheme: url.scheme ?? "https",
scheme: url.scheme,
user: url.user(percentEncoded: false),
password: url.password(percentEncoded: false),
host: url.host(percentEncoded: false) ?? "",
host: url.host(percentEncoded: false),
port: url.port,
path: url.path(percentEncoded: false),
query: url.query(percentEncoded: false),
Expand All @@ -103,7 +103,7 @@ public struct HTTPRequestComponents: Sendable, Hashable {
)
} else {
self.init(
scheme: url.scheme ?? "https",
scheme: url.scheme,
user: url.user,
password: url.password,
host: url.host ?? "",
Expand All @@ -119,10 +119,10 @@ public struct HTTPRequestComponents: Sendable, Hashable {
}
#else
self.init(
scheme: url.scheme ?? "https",
scheme: url.scheme,
user: url.user,
password: url.password,
host: url.host ?? "",
host: url.host,
port: url.port,
path: url.path,
query: url.query,
Expand All @@ -133,7 +133,9 @@ public struct HTTPRequestComponents: Sendable, Hashable {
body: body
)
#endif
}
} else {
self.init(scheme: nil, host: nil, query: nil)
}
}

/// Initialize a new `HTTPRequestComponents` instance from the given components.
Expand All @@ -153,10 +155,10 @@ public struct HTTPRequestComponents: Sendable, Hashable {
///
/// - Warning: IETF STD 66 (rfc3986) says the use of the format “user:password” in the userinfo subcomponent of a URI is deprecated because passing authentication information in clear text has proven to be a security risk.
public init(
scheme: String,
scheme: String?,
user: String? = nil,
password: String? = nil,
host: String,
host: String?,
port: Int? = nil,
path: String = "/",
query: String?,
Expand Down Expand Up @@ -195,6 +197,26 @@ public struct HTTPRequestComponents: Sendable, Hashable {
self.init(urlComponents: urlComponents, method: method, headers: headers, body: body)
}

public init(httpRequest: HTTPRequest) {
self.init(
url: httpRequest.url,
method: httpRequest.method,
headers: httpRequest.headerFields
)
}

public init?(urlRequest: URLRequest) {
guard urlRequest.httpBodyStream == nil, let httpRequest = urlRequest.httpRequest else {
return nil
}
self.init(
url: urlRequest.url,
method: httpRequest.method,
headers: httpRequest.headerFields,
body: urlRequest.httpBody.map { .data($0) }
)
}

public mutating func appendPath(
_ pathComponent: String,
percentEncoded: Bool = false
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftAPIClient/Types/RedirectBehaviour.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import HTTPTypes
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Expand All @@ -13,5 +14,5 @@ public enum RedirectBehaviour {
case doNotFollow

/// Modify the redirect request defined in the response.
case modify((URLRequest, HTTPURLResponse) -> URLRequest?)
case modify((HTTPRequestComponents, HTTPResponse) -> HTTPRequestComponents?)
}
11 changes: 10 additions & 1 deletion Sources/SwiftAPIClient/Utils/URLSessionDelegateWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ extension SessionDelegateProxy: URLSessionTaskDelegate {
case .doNotFollow:
completionHandler(nil)
case let .modify(modifier):
completionHandler(modifier(request, response))
guard
let request = HTTPRequestComponents(urlRequest: request),
let response = response.httpResponse
else {
completionHandler(nil)
return
}
completionHandler(
modifier(request, response)?.urlRequest
)
}
}

Expand Down

0 comments on commit cc20ea0

Please sign in to comment.