Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor request type #5

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
make test
- name: Run examples
run: |
make run-examples
make run
- name: Memory leak detect
# Wait https://github.com/ziglang/zig/issues/15547
if: false
Expand All @@ -61,4 +61,4 @@ jobs:
version: master
- name: Run examples
run: |
make run-examples
make run
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

run-examples:
run:
zig build run-basic -freference-trace
zig build run-advanced -freference-trace

Expand All @@ -9,4 +9,4 @@ test:
docs:
zig build-lib -femit-docs src/main.zig

.PHONY: test run-examples docs
.PHONY: test run docs
27 changes: 15 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const module = b.addModule(MODULE_NAME, .{
.source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .path = "src/root.zig" },
});

const libcurl = b.dependency("libcurl", .{ .target = target, .optimize = optimize });
b.installArtifact(libcurl.artifact("curl"));
// FIXME: libcurl doesn't work with zig master yet.
// const libcurl = b.dependency("libcurl", .{ .target = target, .optimize = optimize });
// b.installArtifact(libcurl.artifact("curl"));

try addExample(b, "basic", module, libcurl, target, optimize);
try addExample(b, "advanced", module, libcurl, target, optimize);
try addExample(b, "basic", module, target, optimize);
try addExample(b, "advanced", module, target, optimize);

const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .path = "src/root.zig" },
.target = target,
.optimize = optimize,
});
main_tests.addModule(MODULE_NAME, module);
main_tests.linkLibrary(libcurl.artifact("curl"));
main_tests.linkLibC();
main_tests.linkSystemLibrary("curl");

const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");
Expand All @@ -35,8 +36,8 @@ fn addExample(
b: *std.Build,
comptime name: []const u8,
curl_module: *Module,
libcurl: *Build.Dependency,
target: std.zig.CrossTarget,
// libcurl: *Build.Dependency,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) !void {
const exe = b.addExecutable(.{
Expand All @@ -47,8 +48,10 @@ fn addExample(
});

b.installArtifact(exe);
exe.addModule(MODULE_NAME, curl_module);
exe.linkLibrary(libcurl.artifact("curl"));
exe.root_module.addImport(MODULE_NAME, curl_module);
// exe.linkLibrary(libcurl.artifact("curl"));
exe.linkSystemLibrary("curl");
exe.linkLibC();

const run_step = b.step("run-" ++ name, std.fmt.comptimePrint("Run {s} example", .{name}));
run_step.dependOn(&b.addRunArtifact(exe).step);
Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"LICENSE",
},
.dependencies = .{
.libcurl = .{
.url = "https://github.com/Cloudef/zigcurl/archive/e9e726131d3eb80c02d20b7ee5ca6935324d6697.tar.gz",
.hash = "1220970ad7d9b96b3464fc12c1e67998881bc4d07c03b8b7d75a5367e4c9991ef099",
},
// .libcurl = .{
// .url = "https://github.com/star-tek-mb/zigcurl/archive/41379c8.tar.gz",
// .hash = "1220424bef91a3ffe97980b7a45062a2c1b26c3bbcefbda301419ad14c3641abcf57",
// },
},
}
50 changes: 26 additions & 24 deletions examples/advanced.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Easy = curl.Easy;

const UA = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0";

const Resposne = struct {
const Response = struct {
headers: struct {
@"User-Agent": []const u8,
Authorization: []const u8,
Expand All @@ -21,42 +21,42 @@ const Resposne = struct {
};

fn put_with_custom_header(allocator: Allocator, easy: Easy) !void {
var stream = std.io.fixedBufferStream(
const body =
\\ {"name": "John", "age": 15}
);
const body = stream.reader();
;

const header = blk: {
var h = curl.RequestHeader.init(allocator);
const headers = blk: {
var h = try easy.create_headers();
errdefer h.deinit();
try h.add(curl.HEADER_CONTENT_TYPE, "application/json");
try h.add("content-type", "application/json");
try h.add("user-agent", UA);
try h.add("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l");
break :blk h;
};
var req = curl.Request(@TypeOf(body)).init("https://httpbin.org/anything/zig-curl", body, .{
.method = .PUT,
.header = header,
.verbose = true,
});
defer req.deinit();
defer headers.deinit();

try easy.set_url("https://httpbin.org/anything/zig-curl");
try easy.set_headers(headers);
try easy.set_method(.PUT);
try easy.set_verbose(true);
try easy.set_post_fields(body);

const resp = try easy.do(req);
const resp = try easy.perform();
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});

const parsed = try std.json.parseFromSlice(Resposne, allocator, resp.body.items, .{
const parsed = try std.json.parseFromSlice(Response, allocator, resp.body.items, .{
.ignore_unknown_fields = true,
});
defer parsed.deinit();

try std.testing.expectEqualDeep(
parsed.value,
.{
Response{
.headers = .{
.@"User-Agent" = UA,
.Authorization = "Basic YWxhZGRpbjpvcGVuc2VzYW1l",
Expand All @@ -83,20 +83,22 @@ fn put_with_custom_header(allocator: Allocator, easy: Easy) !void {
}

fn post_mutli_part(easy: Easy) !void {
const multi_part = try easy.add_multi_part();
// Reset old options, e.g. headers.
easy.reset();

const multi_part = try easy.create_multi_part();
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" });
defer multi_part.deinit();

var req = curl.Request(void).init("https://httpbin.org/anything/mp", {}, .{
.method = .PUT,
.multi_part = multi_part,
.verbose = true,
});
defer req.deinit();
try easy.set_url("https://httpbin.org/anything/mp");
try easy.set_method(.PUT);
try easy.set_multi_part(multi_part);
try easy.set_verbose(true);

const resp = try easy.do(req);
const resp = try easy.perform();
defer resp.deinit();

std.debug.print("resp:{s}\n", .{resp.body.items});
Expand Down
49 changes: 42 additions & 7 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,62 @@ const Allocator = mem.Allocator;
const curl = @import("curl");
const Easy = curl.Easy;

fn get(easy: Easy) !void {
fn get(allocator: Allocator, easy: Easy) !void {
const resp = try easy.get("https://httpbin.org/anything");
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});

const Response = struct {
headers: struct {
Host: []const u8,
},
method: []const u8,
};
const parsed = try std.json.parseFromSlice(Response, allocator, resp.body.items, .{
.ignore_unknown_fields = true,
});
defer parsed.deinit();

try std.testing.expectEqualDeep(parsed.value, Response{
.headers = .{ .Host = "httpbin.org" },
.method = "GET",
});
}

fn post(easy: Easy) !void {
var payload = std.io.fixedBufferStream(
fn post(allocator: Allocator, easy: Easy) !void {
const payload =
\\{"name": "John", "age": 15}
);
const resp = try easy.post("https://httpbin.org/anything", "application/json", payload.reader());
;
const resp = try easy.post("https://httpbin.org/anything", "application/json", payload);
defer resp.deinit();

std.debug.print("Status code: {d}\nBody: {s}\n", .{
resp.status_code,
resp.body.items,
});

const Response = struct {
headers: struct {
@"Content-Type": []const u8,
},
json: struct {
name: []const u8,
age: u32,
},
method: []const u8,
};
const parsed = try std.json.parseFromSlice(Response, allocator, resp.body.items, .{ .ignore_unknown_fields = true });
defer parsed.deinit();

try std.testing.expectEqualDeep(parsed.value, Response{
.headers = .{ .@"Content-Type" = "application/json" },
.json = .{ .name = "John", .age = 15 },
.method = "POST",
});
}

pub fn main() !void {
Expand All @@ -37,8 +72,8 @@ pub fn main() !void {
defer easy.deinit();

println("GET demo");
try get(easy);
try get(allocator, easy);

println("POST demo");
try post(easy);
try post(allocator, easy);
}
2 changes: 1 addition & 1 deletion src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ comptime {
}
}

pub fn url_encode(string: []const u8) ?[]const u8 {
pub fn url_encode(string: [:0]const u8) ?[]const u8 {
const r = c.curl_easy_escape(null, string.ptr, @intCast(string.len));
return std.mem.sliceTo(r.?, 0);
}
Expand Down
Loading
Loading