modern-swift-dev/roundtrip-swift
RoundTrip provides HTTP request construction and async URLSession operations for Apple platforms. RoundTripREST adds a client for APIs with a shared base URL, default headers, response validation, and JSON decoding.
Platform support
RoundTrip supports iOS 18, macOS 15, tvOS 18, watchOS 11, and visionOS 2. Linux is not supported.
Install
Add RoundTrip to a Swift Package Manager manifest:
dependencies: [
.package(
url: "https://github.com/modern-swift-dev/roundtrip-swift.git",
from: "1.0.0"
)
]Add one or both products to the target that uses them:
.product(name: "RoundTrip", package: "roundtrip-swift"),
.product(name: "RoundTripREST", package: "roundtrip-swift")For local development against a checkout, use a path dependency instead:
.package(path: "../roundtrip-swift")Async request
import Foundation
import RoundTrip
struct User: Decodable {
let id: Int
let name: String
}
let request = URLRequestBuilder(string: "https://example.com/users/42")!
.setMethod(.get)
let client = HttpClient()
let response = try await client.execute(request: request)
try response.checkForStatusCodeValidity(validStatusCode: [200])
guard let user = response.payloadAs(User.self) else {
throw ApiError.responseDecodingFailed(response.data, ApiError.emptyResponseBody)
}
print(user.name)REST request
import Combine
import Foundation
import RoundTrip
import RoundTripREST
struct APIBaseURL: BaseURLProvider {
let baseURL = URL(string: "https://example.com")
}
struct NoAPIKey: ApiKeyProvider {
var apiKey: String? { get async { nil } }
}
struct UserRequest: URLRequestConvertible {
let id: Int
func buildRequest(baseUrl: URL?, encoder: JSONEncoder) throws -> URLRequest {
guard let baseUrl else { throw ApiError.invalidURL }
return URLRequest(url: baseUrl.appending(path: "users/\(id)"))
}
}
struct User: Decodable {
let id: Int
let name: String
}
let rest = RestClient(
baseURLProvider: APIBaseURL(),
apiKeyProvider: NoAPIKey(),
service: NetworkService(),
headerProvider: nil,
errorSubject: PassthroughSubject<ApiError, Never>()
)
let result: ApiOperationResult<User> = try await rest.execute(request: UserRequest(id: 42))
print(result.value.name)Documentation
The central documentation repository owns Astro, the shared theme, and website/API generation. It builds from main daily and on manual runs. Edit page Markdown in Documentation/Site/ and keep DocC catalogs beside the module sources. See the docs README for local build and preview commands. Do not commit generated HTML to this repository.
Read the RoundTrip documentation on GitHub Pages. Start with the installation and request guide, then use the generated DocC references for RoundTrip and RoundTripREST.
Contributing
See CONTRIBUTING.md. RoundTrip is available under the MIT license. See LICENSE.
Package Metadata
Repository: modern-swift-dev/roundtrip-swift
Default branch: main
README: README.md