Search in sources :

Example 26 with Response

use of okhttp3.Response in project sonarqube by SonarSource.

the class CeHttpServerTest method start_starts_http_server_and_publishes_URL_in_IPC.

@Test
public void start_starts_http_server_and_publishes_URL_in_IPC() throws Exception {
    Response response = call(underTest.getUrl() + "/pompom?toto=2");
    assertIsPomPomResponse(response);
}
Also used : Response(okhttp3.Response) Test(org.junit.Test)

Example 27 with Response

use of okhttp3.Response in project Reader by TheKeeperOfPie.

the class ActivityLogin method loadAccountInfo.

private void loadAccountInfo(final String tokenAuth, final String tokenRefresh, final long timeExpire) {
    Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/api/v1/me").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
    reddit.load(request).flatMap(UtilsRx.flatMapWrapError(response -> User.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new FinalizingSubscriber<User>() {

        @Override
        public void next(User user) {
            Account account = new Account(user.getName(), Reddit.ACCOUNT_TYPE);
            Intent result = new Intent();
            result.putExtra(AccountManager.KEY_ACCOUNT_NAME, user.getName());
            result.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Reddit.ACCOUNT_TYPE);
            result.putExtra(AccountManager.KEY_AUTHTOKEN, tokenAuth);
            if (getIntent().getBooleanExtra(KEY_IS_NEW_ACCOUNT, false)) {
                Bundle extras = new Bundle();
                extras.putString(KEY_TIME_EXPIRATION, String.valueOf(timeExpire));
                accountManager.addAccountExplicitly(account, tokenRefresh, extras);
                accountManager.setAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, tokenAuth);
            }
            destroyWebView();
            setAccountAuthenticatorResult(result.getExtras());
            setResult(RESULT_OK, result);
            ActivityLogin.this.finish();
        }
    });
}
Also used : Account(android.accounts.Account) User(com.winsonchiu.reader.data.reddit.User) Bundle(android.os.Bundle) WebResourceRequest(android.webkit.WebResourceRequest) Request(okhttp3.Request) Intent(android.content.Intent)

Example 28 with Response

use of okhttp3.Response in project Reader by TheKeeperOfPie.

the class ActivityLogin method fetchTokens.

private void fetchTokens(String code) {
    RequestBody requestBody = new FormBody.Builder().add(Reddit.QUERY_GRANT_TYPE, Reddit.CODE_GRANT).add(Reddit.QUERY_CODE, code).add(Reddit.QUERY_REDIRECT_URI, Reddit.REDIRECT_URI).build();
    Request request = Reddit.withRequestBasicAuth().url(Reddit.ACCESS_URL).post(requestBody).build();
    reddit.load(request).subscribe(new FinalizingSubscriber<String>() {

        @Override
        public void next(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String tokenAccess = jsonObject.getString(Reddit.QUERY_ACCESS_TOKEN);
                String tokenRefresh = jsonObject.getString(Reddit.QUERY_REFRESH_TOKEN);
                long timeExpire = System.currentTimeMillis() + jsonObject.getLong(Reddit.QUERY_EXPIRES_IN) * Reddit.SEC_TO_MS;
                Log.d(TAG, "timeExpire in: " + jsonObject.getLong(Reddit.QUERY_EXPIRES_IN));
                loadAccountInfo(tokenAccess, tokenRefresh, timeExpire);
            } catch (JSONException e) {
                onError(e);
            }
        }

        @Override
        public void error(Throwable e) {
            Toast.makeText(ActivityLogin.this, R.string.error_logging_in, Toast.LENGTH_LONG).show();
        }
    });
}
Also used : JSONObject(org.json.JSONObject) WebResourceRequest(android.webkit.WebResourceRequest) Request(okhttp3.Request) JSONException(org.json.JSONException) RequestBody(okhttp3.RequestBody)

Example 29 with Response

use of okhttp3.Response in project Reader by TheKeeperOfPie.

the class Reddit method getApplicationWideTokenBlocking.

private void getApplicationWideTokenBlocking() {
    if ("".equals(preferences.getString(AppSettings.DEVICE_ID, ""))) {
        preferences.edit().putString(AppSettings.DEVICE_ID, UUID.randomUUID().toString()).apply();
    }
    RequestBody requestBody = new FormBody.Builder().add(QUERY_GRANT_TYPE, INSTALLED_CLIENT_GRANT).add(QUERY_DEVICE_ID, preferences.getString(AppSettings.DEVICE_ID, UUID.randomUUID().toString())).build();
    Request request = new Request.Builder().url(ACCESS_URL).post(requestBody).build();
    try {
        String response = okHttpClientDefault.newCall(request).execute().body().string();
        // Check if an account was set while fetching a token
        if (account == null) {
            JSONObject jsonObject = new JSONObject(response);
            tokenAuth = jsonObject.getString(QUERY_ACCESS_TOKEN);
            timeExpire = System.currentTimeMillis() + jsonObject.getLong(QUERY_EXPIRES_IN) * SEC_TO_MS;
        }
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 30 with Response

use of okhttp3.Response in project SeriesGuide by UweTrottmann.

the class HttpClientModule method provideOkHttpClient.

/**
     * Returns this apps {@link OkHttpClient} with enabled response cache. Should be used with API
     * calls.
     */
@Provides
@Singleton
OkHttpClient provideOkHttpClient(SgApp app, Cache cache) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    builder.readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    builder.addInterceptor(new SgTmdbInterceptor());
    builder.addNetworkInterceptor(new SgTheTvdbInterceptor(app));
    builder.addNetworkInterceptor(new SgTraktInterceptor(app));
    builder.authenticator(new AllApisAuthenticator(app));
    builder.cache(cache);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SgTmdbInterceptor(com.battlelancer.seriesguide.tmdbapi.SgTmdbInterceptor) SgTraktInterceptor(com.battlelancer.seriesguide.traktapi.SgTraktInterceptor) SgTheTvdbInterceptor(com.battlelancer.seriesguide.thetvdbapi.SgTheTvdbInterceptor) AllApisAuthenticator(com.battlelancer.seriesguide.util.AllApisAuthenticator) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Test (org.junit.Test)471 Response (okhttp3.Response)444 MockResponse (okhttp3.mockwebserver.MockResponse)380 Request (okhttp3.Request)377 ResponseBody (okhttp3.ResponseBody)351 IOException (java.io.IOException)220 DateTime (org.joda.time.DateTime)194 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)192 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)178 Response (retrofit2.Response)150 ServiceCall (com.microsoft.rest.ServiceCall)140 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Observable (rx.Observable)104 Call (okhttp3.Call)103 List (java.util.List)95 RequestBody (okhttp3.RequestBody)85 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 OkHttpClient (okhttp3.OkHttpClient)78 HttpURLConnection (java.net.HttpURLConnection)47