Skip to content

Commit

Permalink
1.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed May 20, 2024
1 parent d3dea5d commit 2dc1f3b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Defines request execution with several built-in callers for various request type
- `.httpDownload` for HTTP download requests using `try await` syntax.
- `.mock` for mock requests using `try await` syntax.

All built-in HTTP callers use the `.httpClient` configuration, which can be customized with the `.httpClient()` modifier. The default `.httpClient` is `URLSession`. It's possible to customize the current `.httpClient` instance.
All built-in HTTP callers use the `.httpClient` configuration, which can be customized with the `.httpClient()` modifier. The default `.httpClient` is `URLSession`. It's possible to customize the current `.httpClient` instance, for example, to use a custom `URLSession` configuration or [async-http-client](https://github.com/swift-server/async-http-client).

Custom callers can be created for different types of requests, such as WebSocket, GraphQL, etc.

Expand Down
38 changes: 36 additions & 2 deletions Sources/SwiftAPIClient/Modifiers/RequestModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,45 @@ public extension RequestBuilder where Request == HTTPRequestComponents {
/// - Parameter url: The new URL to set.
/// - Returns: An instance with the updated URL.
func url(_ url: URL) -> Self {
modifyRequest {
urlComponents {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
throw Errors.custom("Invalid URL \(url.absoluteString) components")
}
$0.urlComponents = components
return components
}
}

/// Sets the URL for the request.
/// - Parameter url: The new URL string to set.
/// - Returns: An instance with the updated URL.
func url(_ url: String) -> Self {
urlComponents {
let components: URLComponents?
if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
components = URLComponents(string: url, encodingInvalidCharacters: true)
} else {
components = URLComponents(string: url)
}
guard let components else {
throw Errors.custom("Invalid URL \(url) components")
}
return components
}
}

/// Sets the URL components for the request.
/// - Parameter components: The new URL components to set.
/// - Returns: An instance with the updated URL.
func urlComponents(_ components: URLComponents) -> Self {
urlComponents { components }
}

/// Sets the URL components for the request.
/// - Parameter components: The new URL components to set.
/// - Returns: An instance with the updated URL.
func urlComponents(_ components: @escaping () throws -> URLComponents) -> Self {
modifyRequest {
$0.urlComponents = try components()
}
}

Expand Down

0 comments on commit 2dc1f3b

Please sign in to comment.