Zig
Zig
REST Upload Bandwidth Throttle
See more REST Examples
Demonstrates how to use upload bandwidth throttling with the REST API. This example will upload a file to Drobox using a file stream, with a limit on the bandwidth that can be used for the transfer.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.
// To use bandwidth throttling, the connection should be made using the socket API.
// This provides numerous properties to customize the connection, such as
// BandwidthThrottleDown, BandwidthThrottleUp, ClientIpAddress, ClintPort, Http Proxy,
// KeepAlive, PreferIpv6, RequireSslCertVerify, SoRcvBuf, SoSndBuf, SoReuseAddr,
// SOCKS proxy, TcpNoSDelay, TlsPinSet, TlsCipherSuite, SslAllowedCiphers, etc.
const socket = try chilkat.Socket.init();
defer socket.deinit();
const max_wait_ms = 5000;
socket.connect("content.dropboxapi.com", 443, true, max_wait_ms) catch {
std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
std.debug.print("Connect Fail Reason: {d}\n", .{socket.getConnectFailReason()});
return;
};
// Set the upload bandwidth throttle rate to 50000 bytes per second.
socket.setBandwidthThrottleUp(50000);
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Tell the REST object to use the connected socket.
rest.useConnection(socket, true) catch {};
// The remainder of this example is identical to the example at:
// Dropbox File Stream Upload.
// Add request headers.
rest.addHeader("Content-Type", "application/octet-stream") catch {};
rest.addHeader("Authorization", "Bearer DROPBOX_ACCESS_TOKEN") catch {};
// The upload "parameters" contained in JSON passed in an HTTP request header.
// This is the JSON to be added in this example:
// {
// "path": "/Homework/lit/hamlet.xml",
// "mode": "add",
// "autorename": true,
// "mute": false
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.appendString("path", "/Homework/lit/hamlet.xml") catch {};
json.appendString("mode", "add") catch {};
json.appendBool("autorename", true) catch {};
json.appendBool("mute", false) catch {};
rest.addHeader("Dropbox-API-Arg", try json.emit(alloc)) catch {};
// Almost ready to go...
// Let's setup a file stream to point to a file.
const file_stream = try chilkat.Stream.init();
defer file_stream.deinit();
file_stream.setSourceFile("qa_data/xml/hamlet.xml");
// Do the upload. The URL is https://content.dropboxapi.com/2/files/upload.
// We already connected to content.dropboxapi.com using TLS (i.e. HTTPS),
// so now we only need to specify the path "/2/files/upload".
// Note: The file is streamed directly from disk. (The entire
// file will not be loaded into memory.)
const response_str = rest.fullRequestStream(alloc, "POST", "/2/files/upload", file_stream) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// When successful, Dropbox responds with a 200 response code.
if (rest.getResponseStatusCode() != 200) {
// Examine the request/response to see what happened.
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body (if any): {s}\n", .{response_str});
std.debug.print("---\n", .{});
std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
return;
}
// The response is JSON.
const json_resp = try chilkat.JsonObject.init();
defer json_resp.deinit();
json_resp.setEmitCompact(false);
json_resp.load(response_str) catch {};
// Show the JSON response.
std.debug.print("{s}\n", .{try json_resp.emit(alloc)});
// Returns JSON that looks like this:
// {
// "name": "hamlet.xml",
// "path_lower": "/homework/lit/hamlet.xml",
// "path_display": "/Homework/lit/hamlet.xml",
// "id": "id:74FkdeNuyKAAAAAAAAAAAQ",
// "client_modified": "2016-06-02T23:19:00Z",
// "server_modified": "2016-06-02T23:19:00Z",
// "rev": "9482db15f",
// "size": 279658
// }
// Sample code to get data from the JSON response:
const size = json_resp.intOf("size");
std.debug.print("size = {d}\n", .{size});
const rev = try json_resp.stringOf(alloc, "rev");
std.debug.print("rev = {s}\n", .{rev});
const client_modified = try json_resp.stringOf(alloc, "client_modified");
const ckdt = try chilkat.DateTime.init();
defer ckdt.deinit();
ckdt.setFromTimestamp(client_modified) catch {};
const b_local_time = true;
const dt = try chilkat.DtObj.init();
defer dt.deinit();
ckdt.toDtObj(b_local_time, dt);
std.debug.print("{d}/{d}/{d} {d}:{d}\n", .{ dt.getDay(), dt.getMonth(), dt.getYear(), dt.getHour(), dt.getMinute() });
}