Hey all!
We have just open sourced swift-okhttp - which provides Swift bindings for OkHttp, generated with swift-java, plus an OpenAPIOkHttp transport implementing ClientTransport for Swift OpenAPI.
The primary motivation for this project is Swift on Android. When choosing your network stack on Android, you have a few options:
URLSessionfromFoundationNetworking- async-http-client
- Roll your own JNI/Java bindings
The concerns for us with the two first are binary sizes. Brining in FoundationNetworking or AsyncHTTPClient brought on quite a big chunk of MBs in binary size. With binary sizes already being a problem with Swift on Android, this is not great for us.
OkHttp is a well-established Http client used on Java platforms, and in many Android projects. This is already what our Android team already used. Using Swift–Java bindings to interact with this library adds no binary size beyond the Swift bindings themselves, and no additional libraries are required.
On top of that, we use swift-openapi-generator for all our API endpoints, and the project provides a ClientTransport implementation using OkHttp, which meant it was very easy for us to change the underlying transport protocol for Android:
import OkHttp
import OpenAPIOkHttp
func makeTransport() -> some ClientTransport {
#if canImport(Darwin)
return URLSessionTransport()
#elseif os(Android)
let client = OkHttpClient()
return OkHttpClientTransport(
configuration: .init(client: client)
)
#else
return AsyncHTTPClientTransport()
#endif
}
let apiClient = try Client(serverURL: Servers.Server1.url(), transport: makeTransport())
let response = try await apiClient.getRequest()
I hope that this library might be helpful for other developers using Swift on Android!