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);
}
}
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();
}
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();
}
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");
}
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();
}
};
}
Aggregations