Example usage for com.squareup.okhttp Request method

List of usage examples for com.squareup.okhttp Request method

Introduction

In this page you can find the example usage for com.squareup.okhttp Request method.

Prototype

String method

To view the source code for com.squareup.okhttp Request method.

Click Source Link

Usage

From source file:com.groupon.mesos.util.HttpProtocolSender.java

License:Apache License

@Override
public void onFailure(final Request request, final IOException e) {
    final Object tag = request.tag();
    checkState(tag != null, "saw a request with null tag");

    final SettableFuture<Void> future = inFlight.remove(tag);
    checkState(future != null, "Saw tag %s but not in in flight map", tag);
    future.setException(e);/*www  .j av  a  2 s  .c o m*/

    LOG.warn("While running %s %s: %s", request.method(), request.urlString(), e.getMessage());
}

From source file:com.heroiclabs.sdk.android.util.http.GzipRequestInterceptor.java

License:Apache License

/** {@inheritDoc} */
@Override//from w w w  . j  a  v a  2 s  . c o  m
public Response intercept(final Chain chain) throws IOException {
    final Request originalRequest = chain.request();

    // Do not compress requests already encoded, or that are too small.
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null
            || originalRequest.body().contentLength() < 300) {
        return chain.proceed(originalRequest);
    }

    final Request compressedRequest = originalRequest.newBuilder().header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body()))).build();
    return chain.proceed(compressedRequest);
}

From source file:com.hexdo.hexexamples.network.GOERetrofit.java

License:Open Source License

public static <S> S createService(Class<S> serviceClass, final String authToken) {
    List<Interceptor> interceptors = httpClient.interceptors();
    interceptors.clear();//from www.  j  a v  a  2  s  . co  m
    if (authToken != null) {
        interceptors.add(new Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                Request original = chain.request();

                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder().header("Authorization", authToken)
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    interceptors.add(logging);

    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test build.//from   ww  w. j  av a  2 s .  c o  m
 */
@Test
public void testBuild() {
    final String xToken = "x-token";
    final Request request = RequestBuilder.post(urlWithQuery).withBodyContent("body1", HttpMediaType.TEXT_PLAIN)
            .withHeader("x-token", "token1").build();

    assertEquals("POST", request.method());
    assertEquals("token1", request.header(xToken));
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test delete.//from w w  w .jav  a2 s.c o m
 */
@Test
public void testDelete() {
    final Request request = RequestBuilder.delete(urlWithQuery).build();
    assertEquals("DELETE", request.method());
    assertEquals(urlWithQuery, request.urlString());
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test get./*from   w  w  w.j  av  a  2 s.c  o m*/
 */
@Test
public void testGet() {
    final Request request = RequestBuilder.get(urlWithQuery).build();
    assertEquals("GET", request.method());
    assertEquals(urlWithQuery, request.urlString());
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test post.//from ww w.j  a v a 2 s. co  m
 */
@Test
public void testPost() {
    final Request request = RequestBuilder.post(url).build();
    assertEquals("POST", request.method());
    assertEquals(url, request.urlString());
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test put.//from  w w w .j a va 2s .c o  m
 */
@Test
public void testPut() {
    final Request request = RequestBuilder.put(urlWithQuery).build();
    assertEquals("PUT", request.method());
    assertEquals(urlWithQuery, request.urlString());
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test using path url./* w  ww. j a  v  a2  s .  c  o m*/
 */
@Test
public void testUsingPathUrl() {
    final String url = "/v1/ping";
    final Request request = RequestBuilder.get(url).build();
    assertEquals("GET", request.method());
    assertTrue(RequestUtil.isRelative(request));
    assertEquals(url, HttpUrl.parse(request.urlString()).encodedPath());
}

From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java

License:Open Source License

private void sendRequest(BaseRequest request, @Nonnull Request.Builder builder, String requestBody,
        String method) {//from  www  .  java 2s  . c o  m
    String uri = null;
    long waitTime = 0L;
    try {
        setHeaders(request, builder, request.getHeaderInformation());

        List<String> cookies = new ArrayList<String>();
        if (((CookieManager) okHttpClient.getCookieHandler()).getCookieStore().getCookies() != null) {
            for (HttpCookie httpCookie : ((CookieManager) okHttpClient.getCookieHandler()).getCookieStore()
                    .getCookies()) {
                cookies.add("REQUEST COOKIE: " + httpCookie.toString());
            }
        }

        Request okRequest = builder.build();
        uri = okRequest.uri().toString();
        LOG.debug(request.getLogUtil().getLogMessage(
                "About to " + okRequest.method() + " request to " + uri + " with requestBody  " + requestBody,
                LogEventType.Informational));
        request.logRequest(uri, requestBody, okRequest.method(), request.getHeaderInformation(), cookies,
                false);

        Response response = okHttpClient.newCall(okRequest).execute();
        long startTime = Long.parseLong(response.header("OkHttp-Sent-Millis"));
        long endTime = Long.parseLong(response.header("OkHttp-Sent-Millis"));

        // read response body
        byte[] responseBody = new byte[0];
        // check for no content headers
        if (response.code() != 203 && response.code() != 202 && response.code() != 204) {
            try {
                responseBody = response.body().bytes();
            } catch (Exception e) {
                LOG.warn("could not get response body: " + e);
            }
        }

        processResponse(responseBody, startTime, endTime, request, response.message(), response.code(),
                response.headers());
        waitTime = endTime - startTime;
    } catch (Exception ex) {
        LOG.error(request.getLogUtil().getLogMessage(
                "Could not do " + method + " to url " + uri + " |  error: " + ex.toString(), LogEventType.IO),
                ex);
        throw new RuntimeException(ex);
    } finally {
        if (method.equalsIgnoreCase("post") && request.getLogUtil().getAgentConfig().getLogPostResponse()) {
            LOG.info(request.getLogUtil()
                    .getLogMessage("Response from POST to " + request.getRequestUrl() + " got status code "
                            + request.getResponse().getHttpCode() + " BODY { " + request.getResponse().getBody()
                            + " }", LogEventType.Informational));
        }
    }
    if (waitTime != 0) {
        doWaitDueToLongResponse(request, waitTime, uri);
    }
}