List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient
public OkHttpClient()
From source file:com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java
License:Apache License
/** Builds the {@link OkHttpClient}. */ public OkHttpClient build() { final OkHttpClient rawClient = new OkHttpClient(); if (configuration.getConnectTimeout() > 0L) { rawClient.setConnectTimeout(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS); }/*from ww w .ja v a 2 s . com*/ if (configuration.getReadTimeout() > 0L) { rawClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS); } if (configuration.getWriteTimeout() > 0L) { rawClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.MILLISECONDS); } if (configuration.getCacheDir() != null && configuration.getCacheSize() > 0L) { rawClient.setCache(new Cache(configuration.getCacheDir(), configuration.getCacheSize())); } final OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, name); // If the environment is present, we tie the client with the server lifecycle if (environment != null) { environment.lifecycle().manage(new Managed() { @Override public void start() throws Exception { } @Override public void stop() throws Exception { client.getConnectionPool().evictAll(); } }); } return client; }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClients.java
License:Apache License
/** Create and instrument an {@link OkHttpClient}. */ public static OkHttpClient create(MetricRegistry registry) { return new InstrumentedOkHttpClient(registry, new OkHttpClient(), null); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClients.java
License:Apache License
/** * Create and instrument an {@link OkHttpClient} and give it the provided * {@code name}./*from w w w.j a va2s . c om*/ * * <p>{@code name} provides an identifier for the instrumented client. This * is useful in situations where you have more than one instrumented client * in your application. */ public static OkHttpClient create(MetricRegistry registry, String name) { return new InstrumentedOkHttpClient(registry, new OkHttpClient(), name); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientsTest.java
License:Apache License
@Test public void instrumentProvidedClient() { OkHttpClient rawClient = new OkHttpClient(); OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient); assertThatClientsAreEqual(client, rawClient); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientsTest.java
License:Apache License
@Test public void instrumentAndNameProvidedClient() { OkHttpClient rawClient = new OkHttpClient(); OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, "custom"); assertThatClientsAreEqual(client, rawClient); }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientTest.java
License:Apache License
@Before public void setUp() throws Exception { mockRegistry = mock(MetricRegistry.class); registry = new MetricRegistry(); rawClient = new OkHttpClient(); }
From source file:com.raskasa.oksolr.SolrClient.java
License:Apache License
private OkHttpClient createOkHttpClient() { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, SECONDS); client.setReadTimeout(10, SECONDS);/*w ww . j a v a 2s .c o m*/ client.setWriteTimeout(10, SECONDS); return client; }
From source file:com.raskasa.oksolr.SolrClientTest.java
License:Apache License
@Test public void testCustomTimeout() throws Exception { OkHttpClient okClient = new OkHttpClient(); okClient.setConnectTimeout(20, SECONDS); okClient.setReadTimeout(20, SECONDS); okClient.setWriteTimeout(20, SECONDS); SolrClient solrClient = new SolrClient(okClient); assertThat(solrClient.getConnectTimeout()).isEqualTo(20000); // 20 secs assertThat(solrClient.getReadTimeout()).isEqualTo(20000); // 20 secs assertThat(solrClient.getWriteTimeout()).isEqualTo(20000); // 20 secs }
From source file:com.raskasa.solrfit.SolrResponseTest.java
License:Apache License
@Test public void testSimpleResponse() throws Exception { // Create a MockWebServer. These are lean enough that you can create a new // instance for every unit test. MockWebServer server = new MockWebServer(); // Schedule some responses. server.enqueue(new MockResponse().setBody(loadJsonFile("simple.json"))); // Start the server. server.start();//from w w w . j ava 2 s .c o m // Ask the server for its URL. You'll need this to make HTTP requests. URL baseUrl = server.getUrl("/"); OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, SECONDS); client.setReadTimeout(10, SECONDS); client.setWriteTimeout(10, SECONDS); RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client)) .setEndpoint(baseUrl.toString()).build(); SimpleApi api = restAdapter.create(SimpleApi.class); User user = api.getUser(); assertThat(user.firstName).isEqualTo("Ras Kasa"); assertThat(user.lastName).isEqualTo("Williams"); // Shut down the server. Instances cannot be reused. server.shutdown(); }
From source file:com.raskasa.solrfit.SolrResponseTest.java
License:Apache License
@Test public void testSolrResponse() throws Exception { MockWebServer server = new MockWebServer(); server.enqueue(new MockResponse().setBody(loadJsonFile("solr.json"))); server.start();/*from ww w . j a va 2 s .c o m*/ URL baseUrl = server.getUrl("/"); OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(10, SECONDS); client.setReadTimeout(10, SECONDS); client.setWriteTimeout(10, SECONDS); RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client)) .setEndpoint(baseUrl.toString()).build(); ExampleSolrApi solr = restAdapter.create(ExampleSolrApi.class); SolrQuery query = new SolrQuery.Builder().basic("", "").build(); SolrResponse<State> response = solr.states(query); assertThat(response.internal.results.get(0).name).isEqualTo("New York"); assertThat(response.internal.results.get(0).code).isEqualTo("NY"); assertThat(response.internal.results.get(0).country).isEqualTo("United States"); assertThat(response.internal.results.get(0).countryCode).isEqualTo("US"); assertThat(response.internal.results.get(1).name).isEqualTo("California"); assertThat(response.internal.results.get(1).code).isEqualTo("CA"); assertThat(response.internal.results.get(1).country).isEqualTo("United States"); assertThat(response.internal.results.get(1).countryCode).isEqualTo("US"); }