Skip to content

Commit

Permalink
add advanced example
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Sep 17, 2023
1 parent 79799fe commit be4c845
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 24 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,26 @@ jobs:
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
- name: Install deps
run: |
sudo apt update && sudo apt install -y valgrind libcurl4-openssl-dev
- name: Run examples
run: zig build run-basic
run: |
zig build run-basic
zig build run-advanced
- name: Memory leak detect
run: |
zig build -Dcpu=baseline --verbose
TEST_BINARY=./zig-out/bin/basic
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${TEST_BINARY}
BINS=("./zig-out/bin/basic" "./zig-out/bin/advanced")
for bin in ${BINS[@]}; do
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${bin}
done
test-macos:
timeout-minutes: 10
Expand All @@ -46,10 +51,11 @@ jobs:
matrix:
os: [macos-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Run examples
run: |
zig build run-basic
zig build run-advanced
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ docgen_tmp/
# End of https://www.toptal.com/developers/gitignore/api/zig

.vagrant
Vagrantfile
Vagrantfile
zig-linux*
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#+TITLE: zig-curl
#+DATE: 2023-09-16T23:16:15+0800
#+LASTMOD: 2023-09-17T12:15:26+0800
#+LASTMOD: 2023-09-17T12:43:08+0800
#+OPTIONS: toc:nil num:nil
#+STARTUP: content

Expand Down Expand Up @@ -29,7 +29,7 @@ pub fn main() !void {
});
}
#+end_src
See [[file:examples/basic.zig][basic.zig]] for more usage.
See [[file:examples/basic.zig]], [[file:examples/advanced.zig]] for more usage.

* Installation
=zig-curl= support [[https://ziglang.org/download/0.11.0/release-notes.html#Package-Management][module]] introduced in Zig 0.11.
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn build(b: *std.Build) void {
});

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

const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
Expand Down
55 changes: 55 additions & 0 deletions examples/advanced.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");
const mem = std.mem;
const Allocator = mem.Allocator;
const curl = @import("curl");
const Easy = curl.Easy;

fn post_with_custom_header(allocator: Allocator, easy: Easy) !void {
var payload = std.io.fixedBufferStream(
\\{"name": "John", "age": 15}
);

const header = blk: {
var h = curl.RequestHeader.init(allocator);
errdefer h.deinit();
try h.add(curl.HEADER_CONTENT_TYPE, "application/json");
try h.add("User-Agent", "zig-curl/0.1.0");
break :blk h;
};
var req = curl.request(
"http://httpbin.org/anything",
payload.reader(),
);
req.method = .PUT;
req.header = header;
defer req.deinit();

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

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

// Get response header `date`.
const date_header = try resp.get_header("date");
if (date_header) |h| {
std.debug.print("date header: {s}\n", .{h.get()});
} else {
std.debug.print("date header not found\n", .{});
}
}

pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();

const easy = try Easy.init(allocator);
defer easy.deinit();

const sep = "-" ** 20;
std.debug.print("{s}POST with custom header demo{s}\n", .{ sep, sep });
try post_with_custom_header(allocator, easy);
}
7 changes: 0 additions & 7 deletions examples/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ fn get(easy: Easy) !void {
resp.status_code,
resp.body.items,
});

const date_header = try resp.get_header("date");
if (date_header) |h| {
std.debug.print("date header: {s}\n", .{h.get()});
} else {
std.debug.print("date header not found\n", .{});
}
}

fn post(easy: Easy) !void {
Expand Down
11 changes: 6 additions & 5 deletions src/easy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ handle: *c.CURL,
/// The maximum time in milliseconds that the entire transfer operation to take.
timeout_ms: usize = 30_000,

const HEADER_CONTENT_TYPE: []const u8 = "Content-Type";
pub const HEADER_CONTENT_TYPE: []const u8 = "Content-Type";

pub const Method = enum {
GET,
Expand Down Expand Up @@ -105,7 +105,8 @@ pub fn Request(comptime ReaderType: type) type {
};
}

pub fn request(body: anytype, url: []const u8) Request(@TypeOf(body)) {
/// Request builds a Request object with given body and url.
pub fn request(url: []const u8, body: anytype) Request(@TypeOf(body)) {
return .{
.url = url,
.body = body,
Expand Down Expand Up @@ -226,15 +227,15 @@ pub fn do(self: Self, req: anytype) !Response {

/// Get issues a GET to the specified URL.
pub fn get(self: Self, url: []const u8) !Response {
var req = request({}, url);
var req = request(url, {});
defer req.deinit();

return self.do(req);
}

/// Head issues a HEAD to the specified URL.
pub fn head(self: Self, url: []const u8) !Response {
var req = request({}, url);
var req = request(url, {});
req.method = .HEAD;
defer req.deinit();

Expand All @@ -249,7 +250,7 @@ pub fn post(self: Self, url: []const u8, content_type: []const u8, body: anytype
try h.add(HEADER_CONTENT_TYPE, content_type);
break :blk h;
};
var req = request(body, url);
var req = request(url, body);
req.method = .POST;
req.header = header;
defer req.deinit();
Expand Down
8 changes: 5 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub const Easy = @import("easy.zig");
pub const request = Easy.request;
pub const Request = Easy.Request;
pub const Response = Easy.Response;
// pub const request = Easy.request;
// pub const Request = Easy.Request;
// pub const Response = Easy.Response;

pub usingnamespace Easy;

pub const print_libcurl_version = @import("c.zig").print_libcurl_version;

0 comments on commit be4c845

Please sign in to comment.