Search in sources :

Example 16 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 IOException {
    // Only display important log messages.
    Logger.getLogger("").setLevel(Level.WARNING);
    if (args.length != 1) {
        System.out.println("");
        System.out.println("Usage: COMMAND <auth-file>");
        System.out.println("");
        System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
        System.out.println("    an authorized Dropbox API request.  Generate this file using the \"authorize\"");
        System.out.println("    example program.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAuthFile = args[0];
    // Read auth info file.
    DbxAuthInfo authInfo;
    try {
        authInfo = DbxAuthInfo.Reader.readFromFile(argAuthFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    // Create a DbxClientV1, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-account-info");
    DbxClientV2 dbxClient = new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
    // Make the /account/info API call.
    FullAccount dbxAccountInfo;
    try {
        dbxAccountInfo = dbxClient.users().getCurrentAccount();
    } catch (DbxException ex) {
        System.err.println("Error making API call: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.print(dbxAccountInfo.toStringMultiline());
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader) FullAccount(com.dropbox.core.v2.users.FullAccount) DbxException(com.dropbox.core.DbxException)

Example 17 with DbxRequestConfig

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

the class DbxClientV1Test method testRetrySuccess.

@Test
public void testRetrySuccess() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    String json = "{\"reset\":true,\"entries\":[],\"cursor\":\"fakeCursor\",\"has_more\":true}";
    // 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(json));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    long start = System.currentTimeMillis();
    DbxDelta<DbxEntry> actual = client.getDelta(null);
    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.reset, true);
    assertEquals(actual.cursor, "fakeCursor");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 18 with DbxRequestConfig

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

the class DbxClientV1Test method testRetryDownload.

@Test
public void testRetryDownload() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // load File metadata json
    InputStream in = getClass().getResourceAsStream("/file-with-photo-info.json");
    assertNotNull(in);
    String metadataJson = IOUtil.toUtf8String(in);
    byte[] expected = new byte[] { 1, 2, 3, 4 };
    // 503 once, then return 200
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503)).thenReturn(createDownloaderResponse(expected, "X-Dropbox-Metadata", metadataJson));
    Downloader downloader = client.startGetThumbnail(DbxThumbnailSize.w64h64, DbxThumbnailFormat.JPEG, "/foo/bar.jpg", null);
    // should have been attempted twice
    verify(mockRequestor, times(2)).doGet(anyString(), anyHeaders());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtil.copyStreamToStream(downloader.body, bout);
    byte[] actual = bout.toByteArray();
    assertEquals(actual, expected);
    assertEquals(downloader.metadata.path, "/Photos/Sample Album/Boston City Flow.jpg");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Downloader(com.dropbox.core.v1.DbxClientV1.Downloader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 19 with DbxRequestConfig

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

the class DbxClientV1Test method testRetryRetriesExceeded.

@Test(expectedExceptions = RetryException.class)
public void testRetryRetriesExceeded() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // 503 always and forever
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503));
    try {
        client.getAccountInfo();
    } finally {
        // should only have been called 4: initial call + max number of retries (3)
        verify(mockRequestor, times(4)).doGet(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 20 with DbxRequestConfig

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

the class DbxClientV1Test method testRetryOtherFailure.

@Test(expectedExceptions = BadRequestException.class)
public void testRetryOtherFailure() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // 503 once, then return 400
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(400));
    try {
        client.getAccountInfo();
    } finally {
        // should only have been called 2 times: initial call + one retry
        verify(mockRequestor, times(2)).doGet(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) 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