Search in sources :

Example 1 with DbxRequestConfig

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

the class DbxClientV2Test 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();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    // 503 once, then return 400
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(400));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.users().getCurrentAccount();
    } finally {
        // should only have been called 2 times: initial call + retry
        verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 2 with DbxRequestConfig

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

the class DbxClientV1Test method testRetryDisabled.

@Test(expectedExceptions = RetryException.class)
public void testRetryDisabled() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryDisabled().withHttpRequestor(mockRequestor).build();
    DbxClientV1 client = new DbxClientV1(config, "fakeAccessToken");
    // 503 every time
    when(mockRequestor.doGet(anyString(), anyHeaders())).thenReturn(createEmptyResponse(503));
    try {
        client.getAccountInfo();
    } finally {
        // should only have been called once since we disabled retry
        verify(mockRequestor, times(1)).doGet(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 3 with DbxRequestConfig

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

the class DbxClientV2Test 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();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    // 503 always and forever
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.users().getCurrentAccount();
    } finally {
        // should only have been called 4 times: initial call plus our maximum retry limit
        verify(mockRequestor, times(1 + 3)).startPost(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 4 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);
    // Instantiate factory and set shared factory
    DbxRequestUtil.sharedCallbackFactory = new DbxExampleGlobalCallbackFactory();
    try {
        // Get files and folder metadata from Dropbox root directory
        client.files().listFolder("/does/not/exist/folder/");
    } catch (ListFolderErrorException ex) {
        System.err.println("STANDARD ROUTE ERROR HANDLER: " + ex.errorValue + "\n");
    } catch (DbxException ex) {
        System.err.println("STANDARD NETWORK ERROR HANDLER: " + ex + "\n");
    }
    try {
        // Get files and folder metadata from Dropbox root directory
        client.auth().tokenRevoke();
        client.files().listFolder("/does/not/exist");
    } catch (ListFolderErrorException ex) {
        System.err.println("STANDARD ROUTE ERROR HANDLER2: " + ex.errorValue + "\n");
    } catch (DbxException ex) {
        System.err.println("STANDARD NETWORK ERROR HANDLER2: " + ex + "\n");
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) DbxException(com.dropbox.core.DbxException)

Example 5 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.INFO);
    Config cfg = parseArgs(args);
    if (cfg == null) {
        System.exit(1);
        return;
    }
    // Read app info file (contains app key and app secret)
    DbxAppInfo appInfo;
    try {
        appInfo = DbxAppInfo.Reader.readFromFile(cfg.appInfoFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error reading <app-info-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    DbxOAuth1AccessToken oauth1AccessToken = new DbxOAuth1AccessToken(cfg.accessTokenKey, cfg.accessTokenSecret);
    // Get an OAuth 2 access token.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxOAuth1Upgrader upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo);
    String oauth2AccessToken;
    try {
        oauth2AccessToken = upgrader.createOAuth2AccessToken(oauth1AccessToken);
    } catch (DbxException ex) {
        System.err.println("Error getting OAuth 2 access token: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.println("OAuth 2 access token obtained.");
    DbxAuthInfo authInfo = new DbxAuthInfo(oauth2AccessToken, appInfo.getHost());
    DbxAuthInfo.Writer.writeToStream(authInfo, System.out);
    System.out.println();
    // Disable the OAuth 1 access token.
    if (cfg.disable) {
        try {
            upgrader.disableOAuth1AccessToken(oauth1AccessToken);
        } catch (DbxException ex) {
            System.err.println("Error disabling OAuth 1 access token: " + ex.getMessage());
            System.exit(1);
            return;
        }
        System.out.println("OAuth 1 access token disabled.");
    }
}
Also used : DbxOAuth1Upgrader(com.dropbox.core.DbxOAuth1Upgrader) DbxOAuth1AccessToken(com.dropbox.core.DbxOAuth1AccessToken) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAppInfo(com.dropbox.core.DbxAppInfo) JsonReader(com.dropbox.core.json.JsonReader) DbxException(com.dropbox.core.DbxException)

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