Rust
Rust
REST Download Bandwidth Throttle
See more REST Examples
Demonstrates how to use download bandwidth throttling with the REST API. This example will download a file from Drobox using a file stream, with a limit on the bandwidth that can be used for the transfer.Chilkat Rust Downloads
// 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.
let socket = chilkat::Socket::new();
let max_wait_ms = 5000;
if socket.connect("content.dropboxapi.com", 443, true, max_wait_ms).is_err() {
println!("{}", socket.last_error_text());
println!("Connect Fail Reason: {}", socket.connect_fail_reason());
return;
}
// Set the download bandwidth throttle rate to 50000 bytes per second.
socket.set_bandwidth_throttle_down(50000);
let rest = chilkat::Rest::new();
// Tell the REST object to use the connected socket.
let _ = rest.use_connection(&socket, true);
// The remainder of this example is identical to the example at:
// Dropbox Download File.
// Add request headers.
let _ = rest.add_header("Authorization", "Bearer DROPBOX_ACCESS_TOKEN");
// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// {
// "path": "/Homework/lit/hamlet.xml",
// }
let json = chilkat::JsonObject::new();
let _ = json.append_string("path", "/Homework/lit/hamlet.xml");
let _ = rest.add_header("Dropbox-API-Arg", &json.emit().unwrap_or_default());
// Setup a file stream for the download
let file_stream = chilkat::Stream::new();
file_stream.set_sink_file("qa_output/hamletFromDropbox.xml");
// Indicate that the call to FullRequestNoBody should send the response body
// to fileStream if the response status code is 200.
// If a non-success response status code is received, then nothing
// is streamed to the output file and the error response is returned by FullRequestNoBody.
let expected_status = 200;
let _ = rest.set_response_body_stream(expected_status, true, &file_stream);
let Ok(response_str) = rest.full_request_no_body("POST", "/2/files/download") else {
println!("{}", rest.last_error_text());
return;
};
// When successful, Dropbox responds with a 200 response code.
if rest.response_status_code() != 200 {
// Examine the request/response to see what happened.
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response body (if any): {}", response_str);
println!("---");
println!("LastRequestStartLine: {}", rest.last_request_start_line());
println!("LastRequestHeader: {}", rest.last_request_header());
return;
}
// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information. For example:
let api_result = rest.response_hdr_by_name("dropbox-api-result").unwrap_or_default();
println!("{}", api_result);
// In this case, the pretty-formatted dropbox-api-result JSON 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
// }
// Load the JSON, pretty-print it, and demonstrate how to get some values...
let json_result = chilkat::JsonObject::new();
json_result.set_emit_compact(false);
let _ = json_result.load(&api_result);
// Show the JSON pretty-printed...
println!("{}", json_result.emit().unwrap_or_default());
// Sample code to get data from the JSON response:
let size = json_result.int_of("size");
println!("size = {}", size);
let rev = json_result.string_of("rev").unwrap_or_default();
println!("rev = {}", rev);
let client_modified = json_result.string_of("client_modified").unwrap_or_default();
let ckdt = chilkat::DateTime::new();
let _ = ckdt.set_from_timestamp(&client_modified);
let b_local_time = true;
let dt = chilkat::DtObj::new();
ckdt.to_dt_obj(b_local_time, &dt);
println!("{}/{}/{} {}:{}", dt.day(), dt.month(), dt.year(), dt.hour(), dt.minute());