Skip to content

Repository files navigation

RoundTrip

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

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.

About

A simple/intuitive swift-based http rest library

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages