Repository navigation
Breaking changes
- http-client now needs Swift 6.4 (Xcode 27) or later: its manifest is swift-tools-version 6.4. Its minimum platforms rise to macOS 12, iOS 15, watchOS 9, tvOS 15, and Mac Catalyst 15, the oldest that swift-tools-version 6.4 supports (visionOS 1 and Linux are unchanged).
HTTPClientProtocolnow refinesSendable, so that an API client can call one HTTP client from many tasks at once (for example, from the child tasks of a task group). Your own conforming types (fakes, wrappers) must beSendable.- The conformance of AsyncHTTPClient's
HTTPClienttoHTTPClientProtocol, and.asyncHTTPClient(_:), are now behind theAsyncHTTPClientpackage trait, which is off by default. Packages that only useURLSessionno longer fetch or build AsyncHTTPClient and SwiftNIO.
Packages that require http-client with from: and a version before 0.5.0 (for example, from: "0.3.0"), including API client libraries such as cloudflare-swift, slack-swift, and sentry-swift, get 0.5.0 at their next update without changing their manifests. If you use them with AsyncHTTPClient, the build then fails with errors such as these, which don't mention the trait:
type 'HTTPClient' does not conform to protocol 'HTTPClientProtocol'
generic struct 'Client' requires that 'HTTPClient' conform to 'HTTPClientProtocol'
type 'HTTPClientProtocol' has no member 'asyncHTTPClient'
The first comes when you write the type out (Images.Client<AsyncHTTPClient.HTTPClient>), the second when it's inferred (Images.Client(accountId: id, apiToken: token, httpClient: HTTPClient.shared), where Client is the API client's type), and the third from .asyncHTTPClient(_:). Enable the trait as below to fix them.
Migration
If you use AsyncHTTPClient
Enable the trait where you depend on http-client:
// Package.swift (swift-tools-version: 6.4 or later)
dependencies: [
.package(
url: "https://github.com/zunda-pixel/http-client",
from: "0.5.0",
traits: ["AsyncHTTPClient"]
),
.package(url: "https://github.com/swift-server/async-http-client", from: "1.33.0"),
],
targets: [
.target(
name: "App",
dependencies: [
.product(name: "HTTPClient", package: "http-client"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]
)
]SwiftPM enables a trait for the whole package graph when any package enables it. So if you get http-client only through an API client library (for example, Images.Client<AsyncHTTPClient.HTTPClient> from cloudflare-swift), add http-client to your own dependencies with the trait, as above. API client libraries shouldn't enable the trait themselves, since that would bring SwiftNIO back to all of their users.
If you conform your own types
Make them Sendable: a struct with Sendable stored properties, an actor, or a final class with immutable Sendable state. A fake that records the requests it gets has mutable state, so make it an actor (or a final class that keeps that state in a Mutex from Synchronization, which needs macOS 15 / iOS 18 or later):
actor RecordingHTTPClient: HTTPClientProtocol {
private(set) var requests: [HTTPRequest] = []
func execute(for request: HTTPRequest, from body: Data?) async throws -> (Data, HTTPResponse) {
requests.append(request)
return (Data(), HTTPResponse(status: .ok))
}
}Your tests read what it recorded with await fake.requests. As a final class with that var, the fake doesn't build in the Swift 6 language mode:
stored property 'requests' of 'Sendable'-conforming class 'RecordingHTTPClient' is mutable
In the Swift 5 language mode, that's only a warning, so it builds, and the data race it warns about stays.
Generic API clients such as struct Client<HTTPClient: HTTPClientProtocol> keep compiling, and so do extension Client: Sendable where HTTPClient: Sendable {} and struct Client<HTTPClient: HTTPClientProtocol & Sendable>.
With 0.5.0, the condition and & Sendable are always true. Drop them (to extension Client: Sendable {}, or struct Client<HTTPClient: HTTPClientProtocol>: Sendable) only if your package also requires http-client from: "0.5.0". With a lower bound such as from: "0.3.0", your users can still resolve 0.4.x (for example, when their Package.resolved pins it), and in the Swift 6 language mode the build then fails with:
stored property 'httpClient' of 'Sendable'-conforming generic struct 'Client' has non-Sendable type 'HTTPClient'
If you use URLSession
Nothing changes besides the Swift and platform versions above. URLSession conforms on macOS 12, iOS 15, watchOS 9, tvOS 15, visionOS 1, Mac Catalyst 15, and Linux (import FoundationNetworking on Linux to name URLSession).
Other changes
- With AsyncHTTPClient, cancelling the task now cancels the request and throws
CancellationError. Before, cancellation was ignored: the call waited for the response or the client's timeouts.URLSessionalready cancelled the request, and it still throwsURLError(.cancelled), so the error for cancellation differs by conformance. - With AsyncHTTPClient, a response body larger than the limit (
UInt32.maxbytes, as before) now throwsNIOTooManyBytesErrorinstead ofResponseAccumulator.ResponseTooBigError. execute(for:from:)documents its contract: an error status such as 4xx or 5xx doesn't throw (it returns the response whatever its status), and it throws when it can't send the request or receive the whole response with its body, for example when the connection drops, a timeout passes, or redirects loop. The errors, and the cases that throw, are those of the conforming type's HTTP stack, so they differ by conformance.- The README shows the real API and how to enable the trait.
What's Changed
- Bump actions/checkout from 6 to 6.0.2 by @dependabot[bot] in #15
- Bump github.com/swift-server/async-http-client from 1.33.1 to 1.34.0 by @dependabot[bot] in #16
- Bump github.com/apple/swift-http-types from 1.5.1 to 1.6.0 by @dependabot[bot] in #17
- Bump actions/checkout from 6.0.2 to 6.0.3 by @dependabot[bot] in #18
- Bump actions/checkout from 6.0.3 to 7.0.0 by @dependabot[bot] in #19
- Bump github.com/swift-server/async-http-client from 1.34.0 to 1.35.0 by @dependabot[bot] in #20
- Bump github.com/swift-server/async-http-client from 1.35.0 to 1.36.0 by @dependabot[bot] in #22
- Bump actions/checkout from 7.0.0 to 7.0.1 by @dependabot[bot] in #21
- Bump github.com/swift-server/async-http-client from 1.36.0 to 1.36.1 by @dependabot[bot] in #24
- Bump github.com/apple/swift-http-types from 1.6.0 to 1.7.0 by @dependabot[bot] in #23
- Bump github.com/apple/swift-http-types from 1.7.0 to 1.8.0 by @dependabot[bot] in #25
- Bump github.com/swift-server/async-http-client from 1.36.1 to 1.36.2 by @dependabot[bot] in #26
- Bump github.com/swift-server/async-http-client from 1.36.2 to 1.37.0 by @dependabot[bot] in #27
- Make HTTPClientProtocol Sendable, put AsyncHTTPClient behind a trait, and require Swift 6.4 by @zunda-pixel in #28
Full Changelog: 0.4.0...0.5.0