Search in sources :

Example 21 with DbxRequestConfig

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

the class DbxClientV2Test method testRetryDisabled.

@Test(expectedExceptions = RetryException.class)
public void testRetryDisabled() throws DbxException, IOException {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryDisabled().withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    // 503 every time
    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 once since we disabled retry
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
    }
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) Test(org.testng.annotations.Test)

Example 22 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 != 3) {
        System.out.println("");
        System.out.println("Usage: COMMAND <auth-file> <local-path> <dropbox-path>");
        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.out.println(" <local-path>: The path to a local file whose contents you want to upload.");
        System.out.println("");
        System.out.println(" <dropbox-path>: The path on Dropbox to save the file to.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAuthFile = args[0];
    String localPath = args[1];
    String dropboxPath = args[2];
    // 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;
    }
    String pathError = DbxPathV2.findError(dropboxPath);
    if (pathError != null) {
        System.err.println("Invalid <dropbox-path>: " + pathError);
        System.exit(1);
        return;
    }
    File localFile = new File(localPath);
    if (!localFile.exists()) {
        System.err.println("Invalid <local-path>: file does not exist.");
        System.exit(1);
        return;
    }
    if (!localFile.isFile()) {
        System.err.println("Invalid <local-path>: not a file.");
        System.exit(1);
        return;
    }
    // Create a DbxClientV2, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-upload-file");
    DbxClientV2 dbxClient = new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
    // deciding factor. This should really depend on your network.
    if (localFile.length() <= (2 * CHUNKED_UPLOAD_CHUNK_SIZE)) {
        uploadFile(dbxClient, localFile, dropboxPath);
    } else {
        chunkedUploadFile(dbxClient, localFile, dropboxPath);
    }
    System.exit(0);
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader) File(java.io.File)

Example 23 with DbxRequestConfig

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

the class Main method createClient.

private static DbxClientV2 createClient(String authFile) {
    DbxAuthInfo authInfo;
    try {
        authInfo = DbxAuthInfo.Reader.readFromFile(authFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return null;
    }
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-proguard");
    return new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) JsonReader(com.dropbox.core.json.JsonReader)

Example 24 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project orgzly-android by orgzly.

the class DropboxClient method getDbxClient.

private DbxClientV2 getDbxClient(String accessToken) {
    String userLocale = Locale.getDefault().toString();
    String clientId = String.format("%s/%s", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME);
    DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(clientId).withUserLocale(userLocale).build();
    return new DbxClientV2(requestConfig, accessToken);
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 25 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project syndesis by syndesisio.

the class DropBoxVerifierExtension method verifyCredentials.

@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) {
    String token = (String) parameters.get("accessToken");
    String clientId = (String) parameters.get("clientIdentifier");
    try {
        // Create Dropbox client
        DbxRequestConfig config = new DbxRequestConfig(clientId, Locale.getDefault().toString());
        DbxClientV2 client = new DbxClientV2(config, token);
        client.users().getCurrentAccount();
        client = null;
    } catch (Exception e) {
        builder.error(ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, "Invalid client identifier and/or access token").parameterKey("accessToken").parameterKey("clientIdentifier").build());
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

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