Search in sources :

Example 1 with HttpClientRetryer

use of com.microsoft.appcenter.http.HttpClientRetryer in project mobile-center-sdk-android by Microsoft.

the class Distribute method getLatestReleaseDetails.

/**
 * Get latest release details from server.
 *
 * @param distributionGroupId distribution group id.
 * @param updateToken         token to secure API call.
 */
@VisibleForTesting
synchronized void getLatestReleaseDetails(String distributionGroupId, String updateToken) {
    AppCenterLog.debug(LOG_TAG, "Get latest release details...");
    HttpClientRetryer retryer = new HttpClientRetryer(new DefaultHttpClient());
    NetworkStateHelper networkStateHelper = NetworkStateHelper.getSharedInstance(mContext);
    HttpClient httpClient = new HttpClientNetworkStateHandler(retryer, networkStateHelper);
    String releaseHash = computeReleaseHash(mPackageInfo);
    String url = mApiUrl;
    if (updateToken == null) {
        url += String.format(GET_LATEST_PUBLIC_RELEASE_PATH_FORMAT, mAppSecret, distributionGroupId, releaseHash, getReportingParametersForUpdatedRelease(true, ""));
    } else {
        url += String.format(GET_LATEST_PRIVATE_RELEASE_PATH_FORMAT, mAppSecret, releaseHash, getReportingParametersForUpdatedRelease(false, distributionGroupId));
    }
    Map<String, String> headers = new HashMap<>();
    if (updateToken != null) {
        headers.put(HEADER_API_TOKEN, updateToken);
    }
    final Object releaseCallId = mCheckReleaseCallId = new Object();
    mCheckReleaseApiCall = httpClient.callAsync(url, METHOD_GET, headers, new HttpClient.CallTemplate() {

        @Override
        public String buildRequestBody() throws JSONException {
            /* Only GET is used by Distribute service. This method is never getting called. */
            return null;
        }

        @Override
        public void onBeforeCalling(URL url, Map<String, String> headers) {
            if (AppCenterLog.getLogLevel() <= VERBOSE) {
                /* Log url. */
                String urlString = url.toString().replaceAll(mAppSecret, HttpUtils.hideSecret(mAppSecret));
                AppCenterLog.verbose(LOG_TAG, "Calling " + urlString + "...");
                /* Log headers. */
                Map<String, String> logHeaders = new HashMap<>(headers);
                String apiToken = logHeaders.get(HEADER_API_TOKEN);
                if (apiToken != null) {
                    logHeaders.put(HEADER_API_TOKEN, HttpUtils.hideSecret(apiToken));
                }
                AppCenterLog.verbose(LOG_TAG, "Headers: " + logHeaders);
            }
        }
    }, new ServiceCallback() {

        @Override
        public void onCallSucceeded(final String payload) {
            /* onPostExecute is not always called on UI thread due to an old Android bug. */
            HandlerUtils.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        handleApiCallSuccess(releaseCallId, payload, ReleaseDetails.parse(payload));
                    } catch (JSONException e) {
                        onCallFailed(e);
                    }
                }
            });
        }

        @Override
        public void onCallFailed(Exception e) {
            handleApiCallFailure(releaseCallId, e);
        }
    });
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) DEFAULT_INSTALL_URL(com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_INSTALL_URL) DEFAULT_API_URL(com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_API_URL) URL(java.net.URL) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) HttpException(com.microsoft.appcenter.http.HttpException) ActivityNotFoundException(android.content.ActivityNotFoundException) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpClientRetryer(com.microsoft.appcenter.http.HttpClientRetryer) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) HttpClient(com.microsoft.appcenter.http.HttpClient) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) Map(java.util.Map) HashMap(java.util.HashMap) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 2 with HttpClientRetryer

use of com.microsoft.appcenter.http.HttpClientRetryer in project AppCenter-SDK-Android by Microsoft.

the class Distribute method getLatestReleaseDetails.

/**
 * Get latest release details from server.
 *
 * @param distributionGroupId distribution group id.
 * @param updateToken         token to secure API call.
 */
@VisibleForTesting
synchronized void getLatestReleaseDetails(String distributionGroupId, String updateToken) {
    AppCenterLog.debug(LOG_TAG, "Get latest release details...");
    HttpClientRetryer retryer = new HttpClientRetryer(new DefaultHttpClient());
    NetworkStateHelper networkStateHelper = NetworkStateHelper.getSharedInstance(mContext);
    HttpClient httpClient = new HttpClientNetworkStateHandler(retryer, networkStateHelper);
    String releaseHash = computeReleaseHash(mPackageInfo);
    String url = mApiUrl;
    if (updateToken == null) {
        url += String.format(GET_LATEST_PUBLIC_RELEASE_PATH_FORMAT, mAppSecret, distributionGroupId, releaseHash, getReportingParametersForUpdatedRelease(true, ""));
    } else {
        url += String.format(GET_LATEST_PRIVATE_RELEASE_PATH_FORMAT, mAppSecret, releaseHash, getReportingParametersForUpdatedRelease(false, distributionGroupId));
    }
    Map<String, String> headers = new HashMap<>();
    if (updateToken != null) {
        headers.put(HEADER_API_TOKEN, updateToken);
    }
    final Object releaseCallId = mCheckReleaseCallId = new Object();
    mCheckReleaseApiCall = httpClient.callAsync(url, METHOD_GET, headers, new HttpClient.CallTemplate() {

        @Override
        public String buildRequestBody() throws JSONException {
            /* Only GET is used by Distribute service. This method is never getting called. */
            return null;
        }

        @Override
        public void onBeforeCalling(URL url, Map<String, String> headers) {
            if (AppCenterLog.getLogLevel() <= VERBOSE) {
                /* Log url. */
                String urlString = url.toString().replaceAll(mAppSecret, HttpUtils.hideSecret(mAppSecret));
                AppCenterLog.verbose(LOG_TAG, "Calling " + urlString + "...");
                /* Log headers. */
                Map<String, String> logHeaders = new HashMap<>(headers);
                String apiToken = logHeaders.get(HEADER_API_TOKEN);
                if (apiToken != null) {
                    logHeaders.put(HEADER_API_TOKEN, HttpUtils.hideSecret(apiToken));
                }
                AppCenterLog.verbose(LOG_TAG, "Headers: " + logHeaders);
            }
        }
    }, new ServiceCallback() {

        @Override
        public void onCallSucceeded(final String payload) {
            /* onPostExecute is not always called on UI thread due to an old Android bug. */
            HandlerUtils.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        handleApiCallSuccess(releaseCallId, payload, ReleaseDetails.parse(payload));
                    } catch (JSONException e) {
                        onCallFailed(e);
                    }
                }
            });
        }

        @Override
        public void onCallFailed(Exception e) {
            handleApiCallFailure(releaseCallId, e);
        }
    });
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) DEFAULT_INSTALL_URL(com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_INSTALL_URL) DEFAULT_API_URL(com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_API_URL) URL(java.net.URL) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) HttpException(com.microsoft.appcenter.http.HttpException) ActivityNotFoundException(android.content.ActivityNotFoundException) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpClientRetryer(com.microsoft.appcenter.http.HttpClientRetryer) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) HttpClient(com.microsoft.appcenter.http.HttpClient) NetworkStateHelper(com.microsoft.appcenter.utils.NetworkStateHelper) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) Map(java.util.Map) HashMap(java.util.HashMap) VisibleForTesting(android.support.annotation.VisibleForTesting)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)2 VisibleForTesting (android.support.annotation.VisibleForTesting)2 DEFAULT_API_URL (com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_API_URL)2 DEFAULT_INSTALL_URL (com.microsoft.appcenter.distribute.DistributeConstants.DEFAULT_INSTALL_URL)2 DefaultHttpClient (com.microsoft.appcenter.http.DefaultHttpClient)2 HttpClient (com.microsoft.appcenter.http.HttpClient)2 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)2 HttpClientRetryer (com.microsoft.appcenter.http.HttpClientRetryer)2 HttpException (com.microsoft.appcenter.http.HttpException)2 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)2 NetworkStateHelper (com.microsoft.appcenter.utils.NetworkStateHelper)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONException (org.json.JSONException)2