Search in sources :

Example 11 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetryDownload.

@Test
public void testRetryDownload() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expectedMetadata = constructFileMetadate();
    byte[] expectedBytes = new byte[] { 1, 2, 3, 4 };
    // 503 once, then return 200
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expectedBytes, serialize(expectedMetadata)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    DbxDownloader<FileMetadata> downloader = client.files().download(expectedMetadata.getId());
    // should have been attempted twice
    verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    FileMetadata actualMetadata = downloader.download(bout);
    byte[] actualBytes = bout.toByteArray();
    assertEquals(actualBytes, expectedBytes);
    assertEquals(actualMetadata, expectedMetadata);
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 12 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project drill by apache.

the class DropboxFileSystem method getClient.

private DbxClientV2 getClient() {
    if (this.client != null) {
        return client;
    }
    // read preferred client identifier from config or use "Apache/Drill"
    String clientIdentifier = this.getConf().get("clientIdentifier", "Apache/Drill");
    logger.info("Creating dropbox client with client identifier: {}", clientIdentifier);
    DbxRequestConfig config = DbxRequestConfig.newBuilder(clientIdentifier).build();
    // read access token from config or credentials provider
    logger.info("Reading dropbox access token from configuration or credentials provider");
    String accessToken = this.getConf().get("dropboxAccessToken", "");
    this.client = new DbxClientV2(config, accessToken);
    return this.client;
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 13 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project camel by apache.

the class DropboxConfiguration method createClient.

/**
     * Obtain a new instance of DbxClient and store it in configuration.
     */
public void createClient() {
    DbxRequestConfig config = new DbxRequestConfig(clientIdentifier, Locale.getDefault().toString());
    this.client = new DbxClient(config, accessToken);
}
Also used : DbxClient(com.dropbox.core.DbxClient) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 14 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class Main method main.

public static void main(String[] args) throws DbxException, IOException {
    // Create Dropbox client
    DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
    DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
    // Get current account info
    FullAccount account = client.users().getCurrentAccount();
    System.out.println(account.getName().getDisplayName());
    // Get files and folder metadata from Dropbox root directory
    ListFolderResult result = client.files().listFolder("");
    while (true) {
        for (Metadata metadata : result.getEntries()) {
            System.out.println(metadata.getPathLower());
        }
        if (!result.getHasMore()) {
            break;
        }
        result = client.files().listFolderContinue(result.getCursor());
    }
    // Upload "test.txt" to Dropbox
    try (InputStream in = new FileInputStream("test.txt")) {
        FileMetadata metadata = client.files().uploadBuilder("/test.txt").uploadAndFinish(in);
    }
    DbxDownloader<FileMetadata> downloader = client.files().download("/test.txt");
    try {
        FileOutputStream out = new FileOutputStream("test.txt");
        downloader.download(out);
        out.close();
    } catch (DbxException ex) {
        System.out.println(ex.getMessage());
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) FullAccount(com.dropbox.core.v2.users.FullAccount) FileInputStream(java.io.FileInputStream) DbxException(com.dropbox.core.DbxException)

Example 15 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetrySuccessWithBackoff.

@Test
public void testRetrySuccessWithBackoff() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = constructFileMetadate();
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(// no backoff
    createEmptyResponse(503)).thenReturn(// backoff 1 sec
    createRateLimitResponse(1)).thenReturn(// backoff 2 sec
    createRateLimitResponse(2)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    long start = System.currentTimeMillis();
    Metadata actual = client.files().getMetadata(expected.getId());
    long end = System.currentTimeMillis();
    // no way easy way to properly test this, but request should
    // have taken AT LEAST 3 seconds due to backoff.
    assertTrue((end - start) >= 3000L, "duration: " + (end - start) + " millis");
    // should have been called 4 times: initial call + 3 retries
    verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Test(org.testng.annotations.Test)

Aggregations

DbxRequestConfig (com.dropbox.core.DbxRequestConfig)26 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)12 HttpRequestor (com.dropbox.core.http.HttpRequestor)11 Test (org.testng.annotations.Test)11 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)5 DbxException (com.dropbox.core.DbxException)5 JsonReader (com.dropbox.core.json.JsonReader)5 FileMetadata (com.dropbox.core.v2.files.FileMetadata)4 Metadata (com.dropbox.core.v2.files.Metadata)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)2 FullAccount (com.dropbox.core.v2.users.FullAccount)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)1 DbxClient (com.dropbox.core.DbxClient)1 DbxOAuth1AccessToken (com.dropbox.core.DbxOAuth1AccessToken)1 DbxOAuth1Upgrader (com.dropbox.core.DbxOAuth1Upgrader)1 DbxWebAuth (com.dropbox.core.DbxWebAuth)1 OkHttp3Requestor (com.dropbox.core.http.OkHttp3Requestor)1