---
source: boltffi_bindgen/src/render/swift/templates.rs
assertion_line: 1342
expression: "render_class(&cls, \"boltffi\")"
---
public final class HttpClient {
    let handle: UnsafeMutableRawPointer

    init(handle: UnsafeMutableRawPointer) {
        self.handle = handle
    }

    deinit {
        boltffi_http_client_free(handle)
    }

    public func fetch(url: String) async throws -> Data {
        var url = url
        let futureHandle = url.withUTF8 { urlBuf in boltffi_http_client_fetch_start(handle, urlBuf.baseAddress!, UInt(urlBuf.count)) }
        return try await boltffiAsyncCall(
            futureHandle: futureHandle,
            pollFn: boltffi_http_client_fetch_poll,
            completeFn: boltffi_http_client_fetch_complete,
            cancelFn: boltffi_http_client_fetch_cancel,
            freeFn: boltffi_http_client_fetch_free
        ) { buf, status in
            guard status.code == 0 else { boltffi_free_buf(buf); throw FfiError(message: "FFI failed in async completion with code \(status.code)") }
            defer { boltffi_free_buf(buf) }
            return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in reader.readBytes() }
        }
    }

}
