On Mar 5, 2020, at 2:00 PM, Mike Kerner wrote:

> is anyone working with dropbox via 4D?  how?

I have done a lot of Dropbox work with 4D. It’s fairly easy. Dropbox REST API 
is pretty good and very well documented. You can use 4D’s HTTP Request command 
to do make it all happen. 

So then you are thinking… I’d sure like an example. OK, here’s one:

  // ===========================================
  // PROJECT METHOD: Dropbox_UploadFile

  // PARAMETERS: $0 = results object
  // $1 = dropbox access token
  // $2 = dropbox path
  // $3 = file path

  // DESCRIPTION: Uploads a file to Dropbox using the v2 API
  // Max upload size in a single transfer is 150MB.

  // https://www.dropbox.com/developers/documentation/http/documentation

  // HTTP Request could fail and cause a runtime error. Check for this and try 
to
  // do the upload up to 5 times before giving up. 

  // CREATED BY: Tim Nevels, Innovative Solutions ©2019
  // DATE: 3/8/19
  // LAST MODIFIED: 
  // ============================================
C_OBJECT($0;$results_o)
C_TEXT($1;$dropboxAccessToken_t)
C_TEXT($2;$dropboxPath_t)
C_TEXT($3;$filePath_t)
$dropboxAccessToken_t:=$1
$dropboxPath_t:=$2
$filePath_t:=$3

  // declare local variables
C_OBJECT($headerValue_o;$response_o)
C_BLOB($content_x)
C_TEXT($headerValue_t;$requestURL_t;$response_t)
C_LONGINT($maxRequestTries_l;$requestSentCount_l;$statusCode_l)
ARRAY TEXT($headerName_at;0)
ARRAY TEXT($headerValue_at;0)

  // init result object
$results_o:=New object(\
"success";False;\
"statusCode";0;\
"errorMessage";"")

  // try HTTP Request up to 5 times
$maxRequestTries_l:=5

  // setup http header
APPEND TO ARRAY($headerName_at;"Authorization")
APPEND TO ARRAY($headerValue_at;"Bearer "+$dropboxAccessToken_t)

  // set upload header values
APPEND TO ARRAY($headerName_at;"Content-Type")
APPEND TO ARRAY($headerValue_at;"application/octet-stream")

APPEND TO ARRAY($headerName_at;"Dropbox-API-Arg")
$headerValue_o:=New object(\
"path";$dropboxPath_t;\
"mode";"overwrite")
$headerValue_t:=JSON Stringify($headerValue_o)
APPEND TO ARRAY($headerValue_at;$headerValue_t)

  // set url
$requestURL_t:="https://content.dropboxapi.com/2/files/upload";

  // check that file exists
If (Test path name($filePath_t)#Is a document)  // problem, it does not exist
        $results_o.statusCode:=-43
        $results_o.errorMessage:="File does not exist"
Else 
        
          // load the file
        DOCUMENT TO BLOB($filePath_t;$content_x)
        
          // check file size
        If (BLOB size($content_x)>(1024*1024*149))  // over 149MB, too close to 
limit to try
                $results_o.statusCode:=408
                $results_o.errorMessage:="File is to large to upload in one 
piece"
        Else 
                
                  // set upload options
                HTTP SET OPTION(HTTP compression;1)  // do compression
                HTTP SET OPTION(HTTP timeout;60*10)  // 10 minute timeout
                
                  // upload the file - try up to 5 times before giving up
                $requestSentCount_l:=0
                Repeat 
                        $requestSentCount_l:=$requestSentCount_l+1
                        $statusCode_l:=HTTP Request(HTTP POST 
method;$requestURL_t;$content_x;$response_t;$headerName_at;$headerValue_at)
                Until (($statusCode_l=200) | ($statusCode_l=409) | 
($requestSentCount_l>=$maxRequestTries_l))
                
                  // build results object
                Case of 
                        : ($statusCode_l=200)  // upload was a success
                                $results_o.success:=True
                                $results_o.statusCode:=$statusCode_l
                                $results_o.errorMessage:="File upload was 
successful"
                                
                        : ($statusCode_l=409)  // endpoint specific problem
                                $response_o:=JSON Parse($response_t)
                                $results_o.statusCode:=$statusCode_l
                                
$results_o.errorMessage:=$response_o.error_summary
                                
                        Else   // other problem
                                $results_o.statusCode:=$statusCode_l
                                $results_o.errorMessage:="HTTP Request 
failed"+Char(Carriage return)+Char(Carriage return)+$response_t
                End case 
        End if 
End if 

  // return results
$0:=$results_o

Tim

*****************************************
Tim Nevels
Innovative Solutions
785-749-3444
[email protected]
*****************************************


**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to