summaryrefslogtreecommitdiff
path: root/src/transmission.zig
diff options
context:
space:
mode:
authorChristian Segundo2023-06-17 02:54:34 +0200
committerChristian Segundo2023-06-17 02:54:34 +0200
commitc7412022c3c607c809f774db2459f9259ba95038 (patch)
tree72b7c416c02f5aa189388bcf7851050ce75f3325 /src/transmission.zig
parent831740fcacfe67b8124436460afa77af15493f93 (diff)
downloadzmission-c7412022c3c607c809f774db2459f9259ba95038.tar.gz
one step closer
Diffstat (limited to 'src/transmission.zig')
-rw-r--r--src/transmission.zig68
1 files changed, 42 insertions, 26 deletions
diff --git a/src/transmission.zig b/src/transmission.zig
index dd058bb..409b4b1 100644
--- a/src/transmission.zig
+++ b/src/transmission.zig
@@ -1,10 +1,10 @@
const std = @import("std");
+const http = std.http;
+
const util = @import("util.zig");
-const Request = @import("request.zig").Request;
-const SessionGet = @import("request.zig").SessionGet;
-const SessionSet = @import("request.zig").SessionSet;
-const TorrentGet = @import("request.zig").TorrentGet;
+const Request = @import("request.zig");
+const Response = @import("response.zig");
pub const ClientOptions = extern struct {
host: [*:0]const u8,
@@ -17,8 +17,8 @@ pub const ClientOptions = extern struct {
pub const Client = struct {
uri: std.Uri,
allocator: std.mem.Allocator,
- http_client: std.http.Client,
- http_headers: std.http.Headers,
+ http_client: http.Client,
+ http_headers: http.Headers,
current_session_id: []u8 = "",
pub fn init(allocator: std.mem.Allocator, opts: ClientOptions) Client {
@@ -42,8 +42,8 @@ pub const Client = struct {
return Client{
.uri = base_url,
.allocator = allocator,
- .http_client = std.http.Client{ .allocator = allocator },
- .http_headers = std.http.Headers{ .allocator = allocator },
+ .http_client = http.Client{ .allocator = allocator },
+ .http_headers = http.Headers{ .allocator = allocator },
};
}
@@ -53,18 +53,18 @@ pub const Client = struct {
self.allocator.free(self.current_session_id);
}
- fn setSessionId(self: *Client, r: std.http.Client.Response) !void {
+ fn setSessionId(self: *Client, r: http.Client.Response) !void {
self.allocator.free(self.current_session_id);
const session_id = r.headers.getFirstValue("X-Transmission-Session-Id") orelse return error.NoSessionId;
self.current_session_id = try std.fmt.allocPrint(self.allocator, "{s}", .{session_id});
}
- fn newRequest(self: *Client) !std.http.Client.Request {
+ fn newRequest(self: *Client) !http.Client.Request {
try self.http_headers.append("X-Transmission-Session-Id", self.current_session_id);
return try self.http_client.request(.POST, self.uri, self.http_headers, .{});
}
- pub fn do(self: *Client, req: Request) ![]u8 {
+ pub fn do(self: *Client, req: Request.Object) ![]u8 {
var real_req = try self.newRequest();
defer real_req.deinit();
@@ -73,7 +73,7 @@ pub const Client = struct {
try std.json.stringify(req, .{}, payload.writer());
- real_req.transfer_encoding = std.http.Client.RequestTransfer{
+ real_req.transfer_encoding = http.Client.RequestTransfer{
.content_length = payload.items.len,
};
@@ -82,19 +82,17 @@ pub const Client = struct {
return error.InvalidSize;
}
- std.debug.print("request: {s}\n", .{payload.items});
-
try real_req.finish();
try real_req.wait();
- if (real_req.response.status == std.http.Status.conflict) {
+ if (real_req.response.status == http.Status.conflict) {
try self.setSessionId(real_req.response);
return self.do(req);
}
var body = std.ArrayList(u8).init(self.allocator);
- // TODO making max_append_size this large
+ // TODO: making max_append_size this large
// all the time doesn't feel right
try real_req.reader().readAllArrayList(&body, 9000000000);
@@ -102,12 +100,12 @@ pub const Client = struct {
}
};
-pub fn session_get_raw(client: *Client, session_get: ?SessionGet) ![]u8 {
- const default: SessionGet = .{
- .fields = SessionGet.all_fields,
+pub fn session_get_raw(client: *Client, session_get: ?Request.SessionGet) ![]u8 {
+ const default: Request.SessionGet = .{
+ .fields = Request.SessionGet.all_fields,
};
- const r = Request{
+ const r = Request.Object{
.method = .session_get,
.arguments = .{ .session_get = session_get orelse default },
};
@@ -115,12 +113,15 @@ pub fn session_get_raw(client: *Client, session_get: ?SessionGet) ![]u8 {
return body;
}
-pub fn torrent_get_raw(client: *Client, torrent_get: ?TorrentGet) ![]u8 {
- const default: TorrentGet = .{
- .fields = TorrentGet.all_fields,
+pub fn torrent_get_raw(client: *Client, torrent_get: ?Request.TorrentGet) ![]u8 {
+ const default: Request.TorrentGet = .{
+ .fields = Request.TorrentGet.all_fields,
};
+ //const default: Request.TorrentGet = .{
+ //.fields = &[_]Request.TorrentGet.Fields{.name},
+ //};
- const r = Request{
+ const r = Request.Object{
.method = .torrent_get,
.arguments = .{ .torrent_get = torrent_get orelse default },
};
@@ -128,8 +129,23 @@ pub fn torrent_get_raw(client: *Client, torrent_get: ?TorrentGet) ![]u8 {
return body;
}
-pub fn session_set_raw(client: *Client, session_set: SessionSet) ![]u8 {
- const r = Request{
+pub fn torrent_get_(client: *Client, torrent_get: ?Request.TorrentGet) !Response.Object {
+ @setEvalBranchQuota(100000);
+ const body = try torrent_get_raw(client, torrent_get);
+ const decoded: Response.TorrentGet = try std.json.parseFromSlice(
+ Response.TorrentGet,
+ client.allocator,
+ body,
+ std.json.ParseOptions{
+ .ignore_unknown_fields = true,
+ .duplicate_field_behavior = .@"error",
+ },
+ );
+ return Response.Object.init(.{ .torrent_get = decoded.arguments }, decoded.result);
+}
+
+pub fn session_set_raw(client: *Client, session_set: Request.SessionSet) ![]u8 {
+ const r = Request.Object{
.method = .session_set,
.arguments = .{ .session_set = session_set },
};