From ca8b923e7debf7771e924937594c342079b32dbe Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Wed, 20 Sep 2023 21:12:15 +0800 Subject: [PATCH] add data_cb comments --- .github/workflows/CI.yml | 3 +++ Makefile | 5 +++++ examples/advanced.zig | 12 +++++------- src/easy.zig | 11 ++++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index ad5fd54..23777ff 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -29,6 +29,9 @@ jobs: - name: Install deps run: | sudo apt update && sudo apt install -y valgrind libcurl4-openssl-dev + - name: Run tests + run: | + make test - name: Run examples run: | make run-examples diff --git a/Makefile b/Makefile index 4e7257e..139cfbe 100644 --- a/Makefile +++ b/Makefile @@ -2,3 +2,8 @@ run-examples: zig build run-basic -freference-trace zig build run-advanced -freference-trace + +test: + zig build test + +.PHONY: test run-examples diff --git a/examples/advanced.zig b/examples/advanced.zig index 3b97a9e..b419c11 100644 --- a/examples/advanced.zig +++ b/examples/advanced.zig @@ -44,7 +44,7 @@ fn put_with_custom_header(allocator: Allocator, easy: Easy) !void { const resp = try easy.do(req); defer resp.deinit(); - std.debug.print("Status code: {d}\nBody: [{s}]\n", .{ + std.debug.print("Status code: {d}\nBody: {s}\n", .{ resp.status_code, resp.body.items, }); @@ -82,12 +82,10 @@ fn put_with_custom_header(allocator: Allocator, easy: Easy) !void { } } -fn post_mutli_part(allocator: Allocator, easy: Easy) !void { - _ = allocator; - +fn post_mutli_part(easy: Easy) !void { const multi_part = try easy.add_multi_part(); - try multi_part.add_part("foo", .{ .memory = "hello foo" }); - try multi_part.add_part("bar", .{ .memory = "hello bar" }); + try multi_part.add_part("foo", .{ .data = "hello foo" }); + try multi_part.add_part("bar", .{ .data = "hello bar" }); try multi_part.add_part("build.zig", .{ .file = "build.zig" }); try multi_part.add_part("readme", .{ .file = "README.org" }); @@ -116,5 +114,5 @@ pub fn main() !void { println("PUT with custom header demo"); try put_with_custom_header(allocator, easy); - try post_mutli_part(allocator, easy); + try post_mutli_part(easy); } diff --git a/src/easy.zig b/src/easy.zig index 6ee1c3e..cf1ba3f 100644 --- a/src/easy.zig +++ b/src/easy.zig @@ -187,9 +187,14 @@ pub const MultiPart = struct { allocator: Allocator, pub const DataSource = union(enum) { - memory: []const u8, + /// Set a mime part's body content from memory data. + /// Data will get copied when send request. + /// Setting large data is memory consuming: one might consider using `data_callback` in such a case. + data: []const u8, + /// Set a mime part's body data from a file contents. file: []const u8, - // read_callback : + // TODO: https://curl.se/libcurl/c/curl_mime_data_cb.html + // data_callback: u8, }; pub fn deinit(self: @This()) void { @@ -204,7 +209,7 @@ pub const MultiPart = struct { try checkCode(c.curl_mime_name(part, namez)); switch (source) { - .memory => |slice| { + .data => |slice| { try checkCode(c.curl_mime_data(part, slice.ptr, slice.len)); }, .file => |filepath| {