Search in sources :

Example 71 with Dispatcher

use of okhttp3.Dispatcher in project CubedPay-Java by MelonDevelopment.

the class CubedPayAPI method shutdown.

default void shutdown() throws InterruptedException {
    Dispatcher dispatcher = DispatcherMap.dispatcherMap.remove(this);
    dispatcher.cancelAll();
    dispatcher.executorService().shutdown();
    dispatcher.executorService().awaitTermination(10, TimeUnit.SECONDS);
    for (ExecutorService executor : EventMap.eventMap.values()) {
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    }
}
Also used : Dispatcher(okhttp3.Dispatcher)

Example 72 with Dispatcher

use of okhttp3.Dispatcher in project CubedPay-Java by MelonDevelopment.

the class CubedPayAPI method create.

static CubedPayAPI create(String appID, String accessToken, String apiUrl) {
    Dispatcher dispatcher = new Dispatcher();
    CubedPayAPI api = new Retrofit.Builder().baseUrl(apiUrl).addConverterFactory(new Converter.Factory() {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return super.responseBodyConverter(type, annotations, retrofit);
        }
    }).addConverterFactory(new APIEnvelopeTransformerConverterFactory(GsonConverterFactory.create())).addCallAdapterFactory(Java8CallAdapterFactory.create()).client(new OkHttpClient.Builder().addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("app-id", appID).url(chain.request().url().newBuilder().addQueryParameter("access_token", accessToken).build()).build())).dispatcher(dispatcher).build()).build().create(CubedPayAPI.class);
    DispatcherMap.dispatcherMap.put(api, dispatcher);
    return api;
}
Also used : APIEnvelopeTransformerConverterFactory(co.melondev.cubedpay.envelope.APIEnvelopeTransformerConverterFactory) Converter(retrofit2.Converter) co.melondev.cubedpay.data(co.melondev.cubedpay.data) java.util.concurrent(java.util.concurrent) HashMap(java.util.HashMap) CubedEventRunnable(co.melondev.cubedpay.event.CubedEventRunnable) Retrofit(retrofit2.Retrofit) Dispatcher(okhttp3.Dispatcher) CubedAnnotationProcessor(co.melondev.cubedpay.event.CubedAnnotationProcessor) Java8CallAdapterFactory(retrofit2.adapter.java8.Java8CallAdapterFactory) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) APIEnvelopeTransformerConverterFactory(co.melondev.cubedpay.envelope.APIEnvelopeTransformerConverterFactory) Type(java.lang.reflect.Type) retrofit2.http(retrofit2.http) Map(java.util.Map) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Annotation(java.lang.annotation.Annotation) CubedEvent(co.melondev.cubedpay.event.CubedEvent) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) Type(java.lang.reflect.Type) Java8CallAdapterFactory(retrofit2.adapter.java8.Java8CallAdapterFactory) APIEnvelopeTransformerConverterFactory(co.melondev.cubedpay.envelope.APIEnvelopeTransformerConverterFactory) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Dispatcher(okhttp3.Dispatcher) ResponseBody(okhttp3.ResponseBody)

Example 73 with Dispatcher

use of okhttp3.Dispatcher in project okhttp by square.

the class InterceptorTest method networkInterceptorReturnsNull.

@Test
public void networkInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = chain -> {
        chain.proceed(chain.request());
        return null;
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertThat(expected.getMessage()).isEqualTo(("interceptor " + interceptor + " returned null"));
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 74 with Dispatcher

use of okhttp3.Dispatcher in project edx-app-android by edx.

the class HttpBaseTestCase method setUp.

@Override
public void setUp() throws Exception {
    server = new MockWebServer();
    server.setDispatcher(new MockResponseDispatcher());
    server.start();
    okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(new RoboExecutorService())).addInterceptor(new JsonMergePatchInterceptor()).addInterceptor(new OnlyIfCachedStrippingInterceptor()).build();
    super.setUp();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) JsonMergePatchInterceptor(org.edx.mobile.http.interceptor.JsonMergePatchInterceptor) OnlyIfCachedStrippingInterceptor(org.edx.mobile.http.interceptor.OnlyIfCachedStrippingInterceptor) MockWebServer(okhttp3.mockwebserver.MockWebServer) Dispatcher(okhttp3.Dispatcher) RoboExecutorService(org.robolectric.util.concurrent.RoboExecutorService)

Example 75 with Dispatcher

use of okhttp3.Dispatcher in project spring-security by spring-projects.

the class ClientRegistrationsTests method registrationOidcFallback.

/**
 * Simulates a situation when the ClientRegistration is used with a legacy application
 * where the OIDC Discovery Endpoint is "/issuer1/.well-known/openid-configuration"
 * instead of "/.well-known/openid-configuration/issuer1" in which case the first
 * attempt results in HTTP 404 and the subsequent call results in 200 OK.
 *
 * @see <a href="https://tools.ietf.org/html/rfc8414#section-5">Section 5</a> for more
 * details.
 */
private ClientRegistration.Builder registrationOidcFallback(String path, String body) throws Exception {
    this.issuer = createIssuerFromServer(path);
    this.response.put("issuer", this.issuer);
    String responseBody = (body != null) ? body : this.mapper.writeValueAsString(this.response);
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            switch(request.getPath()) {
                case "/issuer1/.well-known/openid-configuration":
                case "/.well-known/openid-configuration/":
                    return buildSuccessMockResponse(responseBody);
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    this.server.setDispatcher(dispatcher);
    return ClientRegistrations.fromIssuerLocation(this.issuer).clientId("client-id").clientSecret("client-secret");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Dispatcher(okhttp3.mockwebserver.Dispatcher)

Aggregations

Dispatcher (okhttp3.Dispatcher)40 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Dispatcher (okhttp3.mockwebserver.Dispatcher)24 IOException (java.io.IOException)17 Before (org.junit.Before)14 Test (org.junit.Test)13 Call (okhttp3.Call)11 MockWebServer (okhttp3.mockwebserver.MockWebServer)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 Interceptor (okhttp3.Interceptor)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4