use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.
the class DropboxClientFactory method init.
public static void init(String accessToken) {
if (sDbxClient == null) {
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("examples-v2-demo").withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())).build();
sDbxClient = new DbxClientV2(requestConfig, accessToken);
}
}
use of com.dropbox.core.DbxRequestConfig in project syndesis-qe by syndesisio.
the class DropBoxUtils method getClient.
private DbxClientV2 getClient() throws DbxException {
if (this.client == null) {
Optional<Account> optional = AccountsDirectory.getInstance().getAccount("QE Dropbox");
if (optional.isPresent()) {
DbxRequestConfig config = new DbxRequestConfig(optional.get().getProperty("clientIdentifier"));
this.client = new DbxClientV2(config, optional.get().getProperty("accessToken"));
} else {
log.error("Unable to create DropBox client - credentials not found.");
throw new IllegalArgumentException("DropBox credentials were not found.");
}
log.debug("DropBox client created, logged as: " + client.users().getCurrentAccount());
} else {
log.debug("DropBox client was already created, returning existing instance");
}
return this.client;
}
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 != 2) {
System.out.println("Usage: COMMAND <app-info-file> <auth-file-output>");
System.out.println("");
System.out.println("<app-info-file>: a JSON file with information about your API app. Example:");
System.out.println("");
System.out.println(" {");
System.out.println(" \"key\": \"Your Dropbox API app key...\",");
System.out.println(" \"secret\": \"Your Dropbox API app secret...\"");
System.out.println(" }");
System.out.println("");
System.out.println(" Get an API app key by registering with Dropbox:");
System.out.println(" https://dropbox.com/developers/apps");
System.out.println("");
System.out.println("<auth-file-output>: If authorization is successful, the resulting API");
System.out.println(" access token will be saved to this file, which can then be used with");
System.out.println(" other example programs, such as the one in \"examples/account-info\".");
System.out.println("");
System.exit(1);
return;
}
String argAppInfoFile = args[0];
String argAuthFileOutput = args[1];
// Read app info file (contains app key and app secret)
DbxAppInfo appInfo;
try {
appInfo = DbxAppInfo.Reader.readFromFile(argAppInfoFile);
} catch (JsonReader.FileLoadException ex) {
System.err.println("Error reading <app-info-file>: " + ex.getMessage());
System.exit(1);
return;
}
// Run through Dropbox API authorization process
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().build();
String authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first).");
System.out.println("3. Copy the authorization code.");
System.out.print("Enter the authorization code here: ");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1);
return;
}
code = code.trim();
DbxAuthFinish authFinish;
try {
authFinish = webAuth.finishFromCode(code);
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1);
return;
}
System.out.println("Authorization complete.");
System.out.println("- User ID: " + authFinish.getUserId());
System.out.println("- Account ID: " + authFinish.getAccountId());
System.out.println("- Access Token: " + authFinish.getAccessToken());
// Save auth information to output file.
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.getAccessToken(), appInfo.getHost());
File output = new File(argAuthFileOutput);
try {
DbxAuthInfo.Writer.writeToFile(authInfo, output);
System.out.println("Saved authorization information to \"" + output.getCanonicalPath() + "\".");
} catch (IOException ex) {
System.err.println("Error saving to <auth-file-out>: " + ex.getMessage());
System.err.println("Dumping to stderr instead:");
DbxAuthInfo.Writer.writeToStream(authInfo, System.err);
System.exit(1);
return;
}
}
use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.
the class Main method createClient.
/**
* Create a new Dropbox client using the given authentication
* information and HTTP client config.
*
* @param auth Authentication information
* @param config HTTP request configuration
*
* @return new Dropbox V2 client
*/
private static DbxClientV2 createClient(DbxAuthInfo auth, StandardHttpRequestor.Config config) {
String clientUserAgentId = "examples-longpoll";
StandardHttpRequestor requestor = new StandardHttpRequestor(config);
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(clientUserAgentId).withHttpRequestor(requestor).build();
return new DbxClientV2(requestConfig, auth.getAccessToken(), auth.getHost());
}
use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.
the class DbxClientV2Test method testRetrySuccess.
@Test
public void testRetrySuccess() 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(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
Metadata actual = client.files().getMetadata(expected.getId());
// should have only been called 3 times: initial call + 2 retries
verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
assertEquals(actual.getName(), expected.getName());
assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
Aggregations