Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Socket
long li_MaxWaitMs
oleobject loo_Rest
oleobject loo_Json
oleobject loo_FileStream
string ls_ResponseStr
oleobject loo_JsonResp
long li_Size
string ls_Rev
string ls_ClientModified
oleobject loo_Ckdt
long li_BLocalTime
oleobject loo_Dt

li_Success = 0

//  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.

loo_Socket = create oleobject
li_rc = loo_Socket.ConnectToNewObject("Chilkat.Socket")
if li_rc < 0 then
    destroy loo_Socket
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_MaxWaitMs = 5000
li_Success = loo_Socket.Connect("content.dropboxapi.com",443,1,li_MaxWaitMs)
if li_Success <> 1 then
    Write-Debug loo_Socket.LastErrorText
    Write-Debug "Connect Fail Reason: " + string(loo_Socket.ConnectFailReason)
    destroy loo_Socket
    return
end if

//  Set the upload bandwidth throttle rate to 50000 bytes per second.
loo_Socket.BandwidthThrottleUp = 50000

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

//  Tell the REST object to use the connected socket.
loo_Rest.UseConnection(loo_Socket,1)

//  The remainder of this example is identical to the example at:
//  Dropbox File Stream Upload.

//  Add request headers.
loo_Rest.AddHeader("Content-Type","application/octet-stream")
loo_Rest.AddHeader("Authorization","Bearer DROPBOX_ACCESS_TOKEN")

//  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
//  }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.AppendString("path","/Homework/lit/hamlet.xml")
loo_Json.AppendString("mode","add")
loo_Json.AppendBool("autorename",1)
loo_Json.AppendBool("mute",0)
loo_Rest.AddHeader("Dropbox-API-Arg",loo_Json.Emit())

//  Almost ready to go...
//  Let's setup a file stream to point to a file.
loo_FileStream = create oleobject
li_rc = loo_FileStream.ConnectToNewObject("Chilkat.Stream")

loo_FileStream.SourceFile = "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.)
ls_ResponseStr = loo_Rest.FullRequestStream("POST","/2/files/upload",loo_FileStream)
if loo_Rest.LastMethodSuccess = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Socket
    destroy loo_Rest
    destroy loo_Json
    destroy loo_FileStream
    return
end if

//  When successful, Dropbox responds with a 200 response code.
if loo_Rest.ResponseStatusCode <> 200 then
    //  Examine the request/response to see what happened.
    Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "response status text = " + loo_Rest.ResponseStatusText
    Write-Debug "response header: " + loo_Rest.ResponseHeader
    Write-Debug "response body (if any): " + ls_ResponseStr
    Write-Debug "---"
    Write-Debug "LastRequestStartLine: " + loo_Rest.LastRequestStartLine
    Write-Debug "LastRequestHeader: " + loo_Rest.LastRequestHeader
    destroy loo_Socket
    destroy loo_Rest
    destroy loo_Json
    destroy loo_FileStream
    return
end if

//  The response is JSON.
loo_JsonResp = create oleobject
li_rc = loo_JsonResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonResp.EmitCompact = 0
loo_JsonResp.Load(ls_ResponseStr)

//  Show the JSON response.
Write-Debug loo_JsonResp.Emit()

//  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:
li_Size = loo_JsonResp.IntOf("size")
Write-Debug "size = " + string(li_Size)

ls_Rev = loo_JsonResp.StringOf("rev")
Write-Debug "rev = " + ls_Rev

ls_ClientModified = loo_JsonResp.StringOf("client_modified")
loo_Ckdt = create oleobject
li_rc = loo_Ckdt.ConnectToNewObject("Chilkat.CkDateTime")

loo_Ckdt.SetFromTimestamp(ls_ClientModified)
li_BLocalTime = 1
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.DtObj")

loo_Ckdt.ToDtObj(li_BLocalTime,loo_Dt)

Write-Debug string(loo_Dt.Day) + "/" + string(loo_Dt.Month) + "/" + string(loo_Dt.Year) + " " + string(loo_Dt.Hour) + ":" + string(loo_Dt.Minute)


destroy loo_Socket
destroy loo_Rest
destroy loo_Json
destroy loo_FileStream
destroy loo_JsonResp
destroy loo_Ckdt
destroy loo_Dt