Skip to content

Commit

Permalink
1.7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Apr 11, 2024
1 parent 4694e7a commit 7e22dd3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.7.3")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.7.4")
],
targets: [
.target(
Expand Down
21 changes: 20 additions & 1 deletion Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public extension RequestBuilder where Request == HTTPRequestComponents {
/// - newBaseURL: The new base URL to set.
/// - Returns: An instance with the updated base URL.
///
/// - Note: The path, query, and fragment of the original URL are retained, while those of the new URL are ignored.
/// - Note: The query, and fragment of the original URL are retained, while those of the new URL are ignored.
func baseURL(_ newBaseURL: URL) -> Self {
modifyRequest {
$0.urlComponents.scheme = newBaseURL.scheme
Expand All @@ -284,19 +284,38 @@ public extension RequestBuilder where Request == HTTPRequestComponents {
if let host = newBaseURL.host(percentEncoded: false) {
$0.urlComponents.host = host
}
$0.prependPath(newBaseURL.path(percentEncoded: false))
} else {
if let host = newBaseURL.host {
$0.urlComponents.percentEncodedHost = host
}
if !newBaseURL.path.isEmpty {
$0.prependPath(newBaseURL.path, percentEncoded: true)
}
}
#else
if let host = newBaseURL.host {
$0.urlComponents.percentEncodedHost = host
}
if !newBaseURL.path.isEmpty {
$0.prependPath(newBaseURL.path, percentEncoded: true)
}
#endif
$0.urlComponents.port = newBaseURL.port
}
}

/// Sets the URL for the request.
/// - Parameter url: The new URL to set.
/// - Returns: An instance with the updated URL.
func url(_ url: URL) -> Self {
modifyRequest {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
throw Errors.custom("Invalid URL \(url.absoluteString) components")
}
$0.urlComponents = components
}
}

/// Sets the scheme for the request.
///
Expand Down
20 changes: 20 additions & 0 deletions Sources/SwiftAPIClient/Types/HTTPRequestComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ public struct HTTPRequestComponents: Sendable, Hashable {
urlComponents.path += "/"
}
}

public mutating func prependPath(
_ pathComponent: String,
percentEncoded: Bool = false
) {
var path = pathComponent
if path.hasSuffix("/"), urlComponents.path.hasPrefix("/") {
path.removeLast()
} else if !path.hasSuffix("/"), !urlComponents.path.hasPrefix("/") {
path += "/"
}
if percentEncoded {
urlComponents.percentEncodedPath = path + urlComponents.percentEncodedPath
} else {
urlComponents.path = path + urlComponents.path
}
if !urlComponents.path.isEmpty, !urlComponents.path.hasSuffix("/") {
urlComponents.path += "/"
}
}
}

extension HTTPRequestComponents {
Expand Down

0 comments on commit 7e22dd3

Please sign in to comment.