Client API
HTTP/HTTPS download client.
Type
zig
pub const Client = struct {
allocator: Allocator,
config: Config,
write_buffer: []u8,
};Constructor
init
Initialize a new download client.
zig
pub fn init(allocator: Allocator, config: Config) !ClientParameters:
allocator- Memory allocator for internal buffersconfig- Download configuration
Returns: Initialized Client or error
Example:
zig
var client = try downloader.Client.init(allocator, .{
.max_retries = 5,
.resume_downloads = true,
});
defer client.deinit();Destructor
deinit
Release all resources.
zig
pub fn deinit(self: *Client) voidExample:
zig
var client = try Client.init(allocator, .{});
defer client.deinit();Methods
download
Download a file from URL.
zig
pub fn download(
self: *Client,
url: []const u8,
output_path: []const u8,
callback: ?ProgressCallback,
) !u64Parameters:
url- HTTP or HTTPS URLoutput_path- Local file pathcallback- Progress callback (null for silent)
Returns: Total bytes downloaded
Errors:
ConnectionFailed- Cannot connectServerError- Server error responseFileOpenError- Cannot write fileRetriesExhausted- All retries failedCancelled- Callback returned false
Example:
zig
const bytes = try client.download(
"https://example.com/file.zip",
"file.zip",
progressCallback
);downloadSimple
Static convenience method.
zig
pub fn downloadSimple(
allocator: Allocator,
url: []const u8,
output_path: []const u8,
) !u64Example:
zig
const bytes = try Client.downloadSimple(allocator, url, "output.pdf");Standalone Function
downloadFile
Module-level convenience function.
zig
pub fn downloadFile(
allocator: Allocator,
url: []const u8,
output_path: []const u8,
) !u64Thread Safety
Client instances are not thread-safe. Create one Client per thread for concurrent downloads.