Search in sources :

Example 6 with HttpUrl

use of okhttp3.HttpUrl in project zipkin by openzipkin.

the class ZipkinDispatcherTest method toQueryRequest_parseAnnotations.

@Test
public void toQueryRequest_parseAnnotations() {
    HttpUrl url = baseUrl.newBuilder().addQueryParameter("serviceName", "zipkin-server").addQueryParameter("annotationQuery", "error and finagle.timeout").build();
    QueryRequest request = ZipkinDispatcher.toQueryRequest(url);
    assertThat(request.annotations).containsExactly(Constants.ERROR, "finagle.timeout");
}
Also used : QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 7 with HttpUrl

use of okhttp3.HttpUrl in project Pokemap by omkarmoghe.

the class GoogleManager method refreshToken.

public void refreshToken(String refreshToken, final RefreshListener listener) {
    HttpUrl url = HttpUrl.parse(OAUTH_TOKEN_ENDPOINT).newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("client_secret", SECRET).addQueryParameter("refresh_token", refreshToken).addQueryParameter("grant_type", "refresh_token").build();
    Callback<GoogleService.TokenResponse> googleCallback = new Callback<GoogleService.TokenResponse>() {

        @Override
        public void onResponse(Call<GoogleService.TokenResponse> call, Response<GoogleService.TokenResponse> response) {
            if (response != null && response.body() != null) {
                listener.refreshSuccessful(response.body().getIdToken(), response.body().getRefreshToken());
            } else {
                listener.refreshFailed("Failed on requesting the id token");
            }
        }

        @Override
        public void onFailure(Call<GoogleService.TokenResponse> call, Throwable t) {
            t.printStackTrace();
            listener.refreshFailed("Failed on requesting the id token");
        }
    };
    if (mGoogleService != null) {
        Call<GoogleService.TokenResponse> call = mGoogleService.requestToken(url.toString());
        call.enqueue(googleCallback);
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) HttpUrl(okhttp3.HttpUrl)

Example 8 with HttpUrl

use of okhttp3.HttpUrl in project Pokemap by omkarmoghe.

the class NianticManager method loginPTC.

private void loginPTC(final String username, final String password, NianticService.LoginValues values, final LoginListener loginListener) {
    HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("lt", values.getLt()).addQueryParameter("execution", values.getExecution()).addQueryParameter("_eventId", "submit").addQueryParameter("username", username).addQueryParameter("password", password).build();
    OkHttpClient client = mClient.newBuilder().followRedirects(false).followSslRedirects(false).build();
    NianticService service = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).client(client).build().create(NianticService.class);
    Callback<NianticService.LoginResponse> loginCallback = new Callback<NianticService.LoginResponse>() {

        @Override
        public void onResponse(Call<NianticService.LoginResponse> call, Response<NianticService.LoginResponse> response) {
            String location = response.headers().get("location");
            if (location != null && location.split("ticket=").length > 0) {
                String ticket = location.split("ticket=")[1];
                requestToken(ticket, loginListener);
            } else {
                Log.e(TAG, "PTC login failed via loginPTC(). There was no location header in response.");
                loginListener.authFailed("Pokemon Trainer Club Login Failed");
            }
        }

        @Override
        public void onFailure(Call<NianticService.LoginResponse> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "PTC login failed via loginPTC(). loginCallback.onFailure() threw: " + t.getMessage());
            loginListener.authFailed("Pokemon Trainer Club Login Failed");
        }
    };
    Call<NianticService.LoginResponse> call = service.login(url.toString());
    call.enqueue(loginCallback);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback)

Example 9 with HttpUrl

use of okhttp3.HttpUrl in project muzei by romannurik.

the class FiveHundredPxExampleArtSource method onTryUpdate.

@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
    String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(final Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
    FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
    PhotosResponse response;
    try {
        response = service.getPopularPhotos().execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Error reading 500px response", e);
        throw new RetryException();
    }
    if (response == null || response.photos == null) {
        throw new RetryException();
    }
    if (response.photos.size() == 0) {
        Log.w(TAG, "No photos returned from API.");
        scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
        return;
    }
    Random random = new Random();
    Photo photo;
    String token;
    while (true) {
        photo = response.photos.get(random.nextInt(response.photos.size()));
        token = Integer.toString(photo.id);
        if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
            break;
        }
    }
    publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
    scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Artwork(com.google.android.apps.muzei.api.Artwork) Request(okhttp3.Request) Photo(com.example.muzei.examplesource500px.FiveHundredPxService.Photo) Intent(android.content.Intent) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Retrofit(retrofit2.Retrofit) Random(java.util.Random) PhotosResponse(com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse) Interceptor(okhttp3.Interceptor)

Example 10 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method assertConditionallyCached.

/** @return the request with the conditional get headers. */
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
    // scenario 1: condition succeeds
    server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    // scenario 2: condition fails
    server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
    HttpUrl valid = server.url("/valid");
    Response response1 = get(valid);
    assertEquals("A", response1.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response1.code());
    assertEquals("A-OK", response1.message());
    Response response2 = get(valid);
    assertEquals("A", response2.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response2.code());
    assertEquals("A-OK", response2.message());
    HttpUrl invalid = server.url("/invalid");
    Response response3 = get(invalid);
    assertEquals("B", response3.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response3.code());
    assertEquals("B-OK", response3.message());
    Response response4 = get(invalid);
    assertEquals("C", response4.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response4.code());
    assertEquals("C-OK", response4.message());
    // regular get
    server.takeRequest();
    // conditional get
    return server.takeRequest();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse)

Aggregations

HttpUrl (okhttp3.HttpUrl)64 Test (org.junit.Test)51 MockResponse (okhttp3.mockwebserver.MockResponse)49 Request (okhttp3.Request)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)19 Response (okhttp3.Response)15 RequestBody (okhttp3.RequestBody)12 MockWebServer (okhttp3.mockwebserver.MockWebServer)10 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 QueryRequest (zipkin.storage.QueryRequest)5 ResponseCache (java.net.ResponseCache)4 Map (java.util.Map)4 Call (okhttp3.Call)4