Search in sources :

Example 56 with retrofit2.http

use of retrofit2.http in project Pokemap by omkarmoghe.

the class GoogleManager method requestToken.

public void requestToken(String deviceCode, final LoginListener listener) {
    HttpUrl url = HttpUrl.parse(OAUTH_TOKEN_ENDPOINT).newBuilder().build();
    RequestBody body = new FormBody.Builder().add("code", deviceCode).add("client_id", CLIENT_ID).add("client_secret", SECRET).add("redirect_uri", "http://127.0.0.1:8080").add("grant_type", "authorization_code").build();
    Callback<GoogleService.TokenResponse> googleCallback = new Callback<GoogleService.TokenResponse>() {

        @Override
        public void onResponse(Call<GoogleService.TokenResponse> call, Response<GoogleService.TokenResponse> response) {
            if (response.body() != null) {
                listener.authSuccessful(response.body().getIdToken(), response.body().getRefreshToken());
            } else {
                Log.e(TAG, "Google login failed while fetching token. response.body() is null.");
                listener.authFailed("Google login failed while authenticating. Token missing.");
            }
        }

        @Override
        public void onFailure(Call<GoogleService.TokenResponse> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "Google authentication failed while fetching request token using requestToken(). googleCallback's onFailure() threw: " + t.getMessage());
            listener.authFailed("Failed on requesting the id token");
        }
    };
    if (mGoogleService != null) {
        Call<GoogleService.TokenResponse> call = mGoogleService.requestToken(url.toString(), body);
        call.enqueue(googleCallback);
    }
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) HttpUrl(okhttp3.HttpUrl) RequestBody(okhttp3.RequestBody)

Example 57 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderAndroidTest method getWithAndroidUriUrl.

@Test
public void getWithAndroidUriUrl() {
    class Example {

        @GET
        Call<ResponseBody> method(@Url Uri url) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, Uri.parse("foo/bar/"));
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Request(okhttp3.Request) RequestBuilderTest.buildRequest(retrofit2.RequestBuilderTest.buildRequest) Uri(android.net.Uri) Url(retrofit2.http.Url) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 58 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithStringUrl.

@Test
public void getWithStringUrl() {
    class Example {

        @GET
        Call<ResponseBody> method(@Url String url) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "foo/bar/");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Request(okhttp3.Request) Url(retrofit2.http.Url) HttpUrl(okhttp3.HttpUrl) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 59 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class CallTest method cancelOkHttpRequest.

@Test
public void cancelOkHttpRequest() throws InterruptedException {
    OkHttpClient client = new OkHttpClient();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    Call<String> call = service.getString();
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    // Cancel the underlying HTTP Call. Should be reflected accurately back in the Retrofit Call.
    client.dispatcher().cancelAll();
    assertThat(call.isCanceled()).isTrue();
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(IOException.class).hasMessage("Canceled");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 60 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class BehaviorDelegateTest method setUp.

@Before
public void setUp() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com").build();
    MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit).networkBehavior(behavior).build();
    final BehaviorDelegate<DoWorkService> delegate = mockRetrofit.create(DoWorkService.class);
    service = new DoWorkService() {

        @Override
        public Call<String> response() {
            Call<String> response = Calls.response("Response!");
            return delegate.returning(response).response();
        }

        @Override
        public Call<String> failure() {
            Call<String> failure = Calls.failure(mockFailure);
            return delegate.returning(failure).failure();
        }
    };
}
Also used : Retrofit(retrofit2.Retrofit) Call(retrofit2.Call) Before(org.junit.Before)

Aggregations

Test (org.junit.Test)82 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)39 OkHttpClient (okhttp3.OkHttpClient)28 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8