/** * Builds an instance of {@link DbxRequestConfig} with this builder's configured parameters * or defaults. * * @return new {@code DbxRequestConfig} instance. */ public DbxRequestConfig build() { return new DbxRequestConfig(clientIdentifier, userLocale, httpRequestor, maxRetries); } }
public static DbxRequestConfig getDbxConfig() { return DbxRequestConfig .newBuilder("Open LaTex Studio") .withUserLocale(Locale.getDefault().toString()) .build(); }
public static HttpRequestor.Header buildUserAgentHeader(DbxRequestConfig requestConfig, String sdkUserAgentIdentifier) { return new HttpRequestor.Header("User-Agent", requestConfig.getClientIdentifier() + " " + sdkUserAgentIdentifier + "/" + DbxSdkVersion.Version); }
/** * Convenience function for making HTTP GET requests. */ public static HttpRequestor.Response startGet(DbxRequestConfig requestConfig, String accessToken, String sdkUserAgentIdentifier, String host, String path, /*@Nullable*/String/*@Nullable*/[] params, /*@Nullable*/List<HttpRequestor.Header> headers) throws NetworkIOException { headers = copyHeaders(headers); headers = addUserAgentHeader(headers, requestConfig, sdkUserAgentIdentifier); headers = addAuthHeader(headers, accessToken); String url = buildUrlWithParams(requestConfig.getUserLocale(), host, path, params); try { return requestConfig.getHttpRequestor().doGet(url, headers); } catch (IOException ex) { throw new NetworkIOException(ex); } }
@Override protected void logout() throws BackgroundException { try { ((DropboxCommonsHttpRequestExecutor) client.getRequestConfig().getHttpRequestor()).close(); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map(e); } }
public static <T> T doPostNoAuth(final DbxRequestConfig requestConfig, final String sdkUserAgentIdentifier, final String host, final String path, final /*@Nullable*/String/*@Nullable*/[] params, final /*@Nullable*/List<HttpRequestor.Header> headers, final ResponseHandler<T> handler) throws DbxException { return runAndRetry(requestConfig.getMaxRetries(), new RequestMaker<T, DbxException>() { @Override public T run() throws DbxException { HttpRequestor.Response response = startPostNoAuth(requestConfig, sdkUserAgentIdentifier, host, path, params, headers); return finishResponse(response, handler); } }); }
public static List<HttpRequestor.Header> addUserLocaleHeader(/*@Nullable*/List<HttpRequestor.Header> headers, DbxRequestConfig requestConfig) { if (requestConfig.getUserLocale() == null) { return headers; } if (headers == null) headers = new ArrayList<HttpRequestor.Header>(); headers.add(new HttpRequestor.Header("Dropbox-API-User-Locale", requestConfig.getUserLocale())); return headers; }
/** * Convenience function for making HTTP PUT requests. */ public static HttpRequestor.Uploader startPut(DbxRequestConfig requestConfig, String accessToken, String sdkUserAgentIdentifier, String host, String path, /*@Nullable*/String/*@Nullable*/[] params, /*@Nullable*/List<HttpRequestor.Header> headers) throws NetworkIOException { headers = copyHeaders(headers); headers = addUserAgentHeader(headers, requestConfig, sdkUserAgentIdentifier); headers = addAuthHeader(headers, accessToken); String url = buildUrlWithParams(requestConfig.getUserLocale(), host, path, params); try { return requestConfig.getHttpRequestor().startPut(url, headers); } catch (IOException ex) { throw new NetworkIOException(ex); } }
public <ArgT> HttpRequestor.Uploader uploadStyle(String host, String path, ArgT arg, boolean noAuth, StoneSerializer<ArgT> argSerializer) throws DbxException { String uri = DbxRequestUtil.buildUri(host, path); List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>(); if (!noAuth) { addAuthHeaders(headers); } addUserLocaleHeader(headers, requestConfig); addPathRootHeader(headers, this.pathRoot); headers.add(new HttpRequestor.Header("Content-Type", "application/octet-stream")); headers = DbxRequestUtil.addUserAgentHeader(headers, requestConfig, USER_AGENT_ID); headers.add(new HttpRequestor.Header("Dropbox-API-Arg", headerSafeJson(argSerializer, arg))); try { return requestConfig.getHttpRequestor().startPost(uri, headers); } catch (IOException ex) { throw new NetworkIOException(ex); } }
public static <T> T doGet(final DbxRequestConfig requestConfig, final String accessToken, final String sdkUserAgentIdentifier, final String host, final String path, final /*@Nullable*/String/*@Nullable*/[] params, final /*@Nullable*/List<HttpRequestor.Header> headers, final ResponseHandler<T> handler) throws DbxException { return runAndRetry(requestConfig.getMaxRetries(), new RequestMaker<T, DbxException>() { @Override public T run() throws DbxException { HttpRequestor.Response response = startGet(requestConfig, accessToken, sdkUserAgentIdentifier, host, path, params, headers); try { return handler.handle(response); } finally { try { response.getBody().close(); } catch (IOException ex) { //noinspection ThrowFromFinallyBlock throw new NetworkIOException(ex); } } } }); }
private String authorizeImpl(Request request) { Map<String, String> params = new HashMap<String, String>(); params.put("client_id", appInfo.getKey()); params.put("response_type", "code"); if (request.redirectUri != null) { params.put("redirect_uri", request.redirectUri); params.put("state", appendCsrfToken(request)); } if (request.requireRole != null) { params.put("require_role", request.requireRole); } if (request.forceReapprove != null) { params.put("force_reapprove", Boolean.toString(request.forceReapprove).toLowerCase()); } if (request.disableSignup != null) { params.put("disable_signup", Boolean.toString(request.disableSignup).toLowerCase()); } return DbxRequestUtil.buildUrlWithParams( requestConfig.getUserLocale(), appInfo.getHost().getWeb(), "oauth2/authorize", DbxRequestUtil.toParamsArray(params) ); }
DbxOAuth1Upgrader upgrader; DbxOAuth1AccessToken oAuth1AccessToken; try { DbxRequestConfig requestConfig = new DbxRequestConfig("Your App", Locale.getDefault().toString()); DbxAppInfo appInfo = new DbxAppInfo(DROPBOX_APP_KEY, DROPBOX_APP_SECRET); upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo); oAuth1AccessToken = new DbxOAuth1AccessToken(oauth1AccessKey, oauth1AccessSecret); String newToken = upgrader.createOAuth2AccessToken(oAuth1AccessToken); upgrader.disableOAuth1AccessToken(oAuth1AccessToken); return newToken; } catch (Exception e) { //deal with it }
public static void init(String accessToken) { if (sDbxClient == null || DropboxClientFactory.accessToken != accessToken) { DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("upm") .withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())) .build(); sDbxClient = new DbxClientV2(requestConfig, accessToken); DropboxClientFactory.accessToken = accessToken; } }
/** * Convenience function for making HTTP POST requests. Like startPostNoAuth but takes byte[] instead of params. */ public static HttpRequestor.Response startPostRaw(DbxRequestConfig requestConfig, String sdkUserAgentIdentifier, String host, String path, byte[] body, /*@Nullable*/List<HttpRequestor.Header> headers) throws NetworkIOException { String uri = buildUri(host, path); headers = copyHeaders(headers); headers = addUserAgentHeader(headers, requestConfig, sdkUserAgentIdentifier); headers.add(new HttpRequestor.Header("Content-Length", Integer.toString(body.length))); try { HttpRequestor.Uploader uploader = requestConfig.getHttpRequestor().startPost(uri, headers); try { uploader.upload(body); return uploader.finish(); } finally { uploader.close(); } } catch (IOException ex) { throw new NetworkIOException(ex); } }
return DbxRequestUtil.runAndRetry(requestConfig.getMaxRetries(), new DbxRequestUtil.RequestMaker<Downloader, DbxException>() { @Override
private DbxAuthFinish finish(String code, String redirectUri, final String state) throws DbxException { if (code == null) throw new NullPointerException("code"); Map<String, String> params = new HashMap<String, String>(); params.put("grant_type", "authorization_code"); params.put("code", code); params.put("locale", requestConfig.getUserLocale()); if (redirectUri != null) { params.put("redirect_uri", redirectUri); } List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>(); DbxRequestUtil.addBasicAuthHeader(headers, appInfo.getKey(), appInfo.getSecret()); return DbxRequestUtil.doPostNoAuth( requestConfig, DbxRawClientV2.USER_AGENT_ID, appInfo.getHost().getApi(), "oauth2/token", DbxRequestUtil.toParamsArray(params), headers, new DbxRequestUtil.ResponseHandler<DbxAuthFinish>() { @Override public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException { if (response.getStatusCode() != 200) { throw DbxRequestUtil.unexpectedStatus(response); } return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response) .withUrlState(state); } } ); }
/** * 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 DbxClientV2(config, accessToken); }
private boolean authSession() { String accessToken = MyPreferences.getDropboxAuthToken(context); if (accessToken != null) { if (dropboxClient == null) { DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("financisto") .withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())) .build(); dropboxClient = new DbxClientV2(requestConfig, accessToken); } return true; } return false; }
return executeRetriable(requestConfig.getMaxRetries(), new RetriableExecution<ResT> () { private String userIdAnon;
/** * Convenience function for making HTTP POST requests. */ public static HttpRequestor.Response startPostNoAuth(DbxRequestConfig requestConfig, String sdkUserAgentIdentifier, String host, String path, /*@Nullable*/String/*@Nullable*/[] params, /*@Nullable*/List<HttpRequestor.Header> headers) throws NetworkIOException { byte[] encodedParams = StringUtil.stringToUtf8(encodeUrlParams(requestConfig.getUserLocale(), params)); headers = copyHeaders(headers); headers.add(new HttpRequestor.Header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")); return startPostRaw(requestConfig, sdkUserAgentIdentifier, host, path, encodedParams, headers); }