Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local socket = chilkat.Socket.new()
local maxWaitMs = 5000
local success = socket:connect("content.dropboxapi.com", 443, true, maxWaitMs)
if not success then
print(socket.last_error_text)
print("Connect Fail Reason: " .. socket.connect_fail_reason)
return
end
-- Set the download bandwidth throttle rate to 50000 bytes per second.
socket.bandwidth_throttle_down = 50000
local rest = chilkat.Rest.new()
-- Tell the REST object to use the connected socket.
rest:use_connection(socket, true)
-- The remainder of this example is identical to the example at:
-- Dropbox Download File.
-- Add request headers.
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",
-- }
local json = chilkat.JsonObject.new()
json:append_string("path", "/Homework/lit/hamlet.xml")
rest:add_header("Dropbox-API-Arg", json:emit())
-- Setup a file stream for the download
local fileStream = chilkat.Stream.new()
fileStream.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.
local expectedStatus = 200
rest:set_response_body_stream(expectedStatus, true, fileStream)
local responseStr = rest:full_request_no_body("POST", "/2/files/download")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
-- When successful, Dropbox responds with a 200 response code.
if rest.response_status_code ~= 200 then
-- Examine the request/response to see what happened.
print("response status code = " .. rest.response_status_code)
print("response status text = " .. rest.response_status_text)
print("response header: " .. rest.response_header)
print("response body (if any): " .. responseStr)
print("---")
print("LastRequestStartLine: " .. rest.last_request_start_line)
print("LastRequestHeader: " .. rest.last_request_header)
return
end
-- 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:
local apiResult = rest:response_hdr_by_name("dropbox-api-result")
print(apiResult)
-- 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...
local jsonResult = chilkat.JsonObject.new()
jsonResult.emit_compact = false
jsonResult:load(apiResult)
-- Show the JSON pretty-printed...
print(jsonResult:emit())
-- Sample code to get data from the JSON response:
local size = jsonResult:int_of("size")
print("size = " .. size)
local rev = jsonResult:string_of("rev")
print("rev = " .. rev)
local clientModified = jsonResult:string_of("client_modified")
local ckdt = chilkat.DateTime.new()
ckdt:set_from_timestamp(clientModified)
local bLocalTime = true
local dt = chilkat.DtObj.new()
ckdt:to_dt_obj(bLocalTime, dt)
print(dt.day .. "/" .. dt.month .. "/" .. dt.year .. " " .. dt.hour .. ":" .. dt.minute)