Search in sources :

Example 66 with Call

use of retrofit2.Call in project retrofit by square.

the class RetrofitTest method argumentCapture.

/** Confirm that Retrofit encodes parameters when the call is executed, and not earlier. */
@Test
public void argumentCapture() throws Exception {
    AtomicInteger i = new AtomicInteger();
    server.enqueue(new MockResponse().setBody("a"));
    server.enqueue(new MockResponse().setBody("b"));
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    MutableParameters mutableParameters = retrofit.create(MutableParameters.class);
    i.set(100);
    Call<String> call1 = mutableParameters.method(i);
    i.set(101);
    Response<String> response1 = call1.execute();
    i.set(102);
    assertEquals("a", response1.body());
    assertEquals("/?i=101", server.takeRequest().getPath());
    i.set(200);
    Call<String> call2 = call1.clone();
    i.set(201);
    Response<String> response2 = call2.execute();
    i.set(202);
    assertEquals("b", response2.body());
    assertEquals("/?i=201", server.takeRequest().getPath());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 67 with Call

use of retrofit2.Call in project retrofit by square.

the class RetrofitTest method callAdapterFactoryNoMatchThrows.

@Test
public void callAdapterFactoryNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(nonMatchingFactory).build();
    try {
        retrofit.callAdapter(type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + "   * retrofit2.DefaultCallAdapterFactory");
    }
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 68 with Call

use of retrofit2.Call in project retrofit by square.

the class DeserializeErrorBody method main.

public static void main(String... args) throws IOException {
    // Create a local web server which response with a 404 and JSON body.
    MockWebServer server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse().setResponseCode(404).setBody("{\"message\":\"Unable to locate resource\"}"));
    // Create our Service instance with a Retrofit pointing at the local web server and Gson.
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(GsonConverterFactory.create()).build();
    Service service = retrofit.create(Service.class);
    Response<User> response = service.getUser().execute();
    // Normally you would check response.isSuccess() here before doing the following, but we know
    // this call will always fail. You could also use response.code() to determine whether to
    // convert the error body and/or which type to use for conversion.
    // Look up a converter for the Error type on the Retrofit instance.
    Converter<ResponseBody, Error> errorConverter = retrofit.responseBodyConverter(Error.class, new Annotation[0]);
    // Convert the error body into our Error type.
    Error error = errorConverter.convert(response.errorBody());
    System.out.println("ERROR: " + error.message);
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer) ResponseBody(okhttp3.ResponseBody)

Example 69 with Call

use of retrofit2.Call in project retrofit by square.

the class RetrofitTest method callAdapterFactoryDelegateNoMatchThrows.

@Test
public void callAdapterFactoryDelegateNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    DelegatingCallAdapterFactory delegatingFactory1 = new DelegatingCallAdapterFactory();
    DelegatingCallAdapterFactory delegatingFactory2 = new DelegatingCallAdapterFactory();
    NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(delegatingFactory1).addCallAdapterFactory(delegatingFactory2).addCallAdapterFactory(nonMatchingFactory).build();
    try {
        retrofit.callAdapter(type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + "  Skipped:\n" + "   * retrofit2.helpers.DelegatingCallAdapterFactory\n" + "   * retrofit2.helpers.DelegatingCallAdapterFactory\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + "   * retrofit2.DefaultCallAdapterFactory");
    }
    assertThat(delegatingFactory1.called).isTrue();
    assertThat(delegatingFactory2.called).isTrue();
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 70 with Call

use of retrofit2.Call in project retrofit by square.

the class RetrofitTest method callCallCustomAdapter.

@Test
public void callCallCustomAdapter() {
    final AtomicBoolean factoryCalled = new AtomicBoolean();
    final AtomicBoolean adapterCalled = new AtomicBoolean();
    class MyCallAdapterFactory extends CallAdapter.Factory {

        @Override
        public CallAdapter<?, ?> get(final Type returnType, Annotation[] annotations, Retrofit retrofit) {
            factoryCalled.set(true);
            if (getRawType(returnType) != Call.class) {
                return null;
            }
            return new CallAdapter<Object, Call<?>>() {

                @Override
                public Type responseType() {
                    return getParameterUpperBound(0, (ParameterizedType) returnType);
                }

                @Override
                public Call<Object> adapt(Call<Object> call) {
                    adapterCalled.set(true);
                    return call;
                }
            };
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addCallAdapterFactory(new MyCallAdapterFactory()).build();
    CallMethod example = retrofit.create(CallMethod.class);
    assertThat(example.getResponseBody()).isNotNull();
    assertThat(factoryCalled.get()).isTrue();
    assertThat(adapterCalled.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)189 Request (okhttp3.Request)176 Test (org.junit.Test)155 Call (okhttp3.Call)127 Response (retrofit2.Response)107 Response (okhttp3.Response)105 Observable (rx.Observable)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 IOException (java.io.IOException)72 MockResponse (okhttp3.mockwebserver.MockResponse)67 RequestBody (okhttp3.RequestBody)51 Callback (okhttp3.Callback)44 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 OkHttpClient (okhttp3.OkHttpClient)33 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)27 Buffer (okio.Buffer)19 Call (retrofit2.Call)19 MultipartBody (okhttp3.MultipartBody)17 HttpUrl (okhttp3.HttpUrl)15 Callback (retrofit2.Callback)15