Example usage for com.squareup.okhttp Response newBuilder

List of usage examples for com.squareup.okhttp Response newBuilder

Introduction

In this page you can find the example usage for com.squareup.okhttp Response newBuilder.

Prototype

public Builder newBuilder() 

Source Link

Usage

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void rewriteResponseFromServer(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse().addHeader("Original-Header: foo").setBody("abc"));

    interceptors.add(new Interceptor() {
        @Override//from   ww w  .j ava2 s  .  c o  m
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().body(uppercase(originalResponse.body()))
                    .addHeader("OkHttp-Intercepted", "yep").build();
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    Response response = client.newCall(request).execute();
    assertEquals("ABC", response.body().string());
    assertEquals("yep", response.header("OkHttp-Intercepted"));
    assertEquals("foo", response.header("Original-Header"));
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void multipleInterceptors(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override/*  www.  j a  va  2 s. c om*/
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Response originalResponse = chain
                    .proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", "Android") // 1. Added first.
                            .build());
            return originalResponse.newBuilder().addHeader("Response-Interceptor", "Donut") // 4. Added last.
                    .build();
        }
    });
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Response originalResponse = chain
                    .proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", "Bob") // 2. Added second.
                            .build());
            return originalResponse.newBuilder().addHeader("Response-Interceptor", "Cupcake") // 3. Added third.
                    .build();
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    Response response = client.newCall(request).execute();
    assertEquals(Arrays.asList("Cupcake", "Donut"), response.headers("Response-Interceptor"));

    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(Arrays.asList("Android", "Bob"), recordedRequest.getHeaders().values("Request-Interceptor"));
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void asyncInterceptors(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override//from   w w w  .j a va2  s. c  o  m
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);

    callback.await(request.httpUrl()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}

From source file:co.uk.crowdemotion.FaceVideoApi.java

License:Apache License

private com.squareup.okhttp.Call facevideoFacevideoIdDeleteCall(Integer facevideoId,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // verify the required parameter 'facevideoId' is set
    if (facevideoId == null) {
        throw new ApiException(
                "Missing the required parameter 'facevideoId' when calling facevideoFacevideoIdDelete(Async)");
    }// ww w  . j  ava  2  s.  c o m

    // create path and map variables
    String localVarPath = "/facevideo/{facevideo_id}".replaceAll("\\{format\\}", "json")
            .replaceAll("\\{" + "facevideo_id" + "\\}", apiClient.escapeString(facevideoId.toString()));

    List<Pair> localVarQueryParams = new ArrayList<Pair>();

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "DELETE", localVarQueryParams, localVarPostBody,
            localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.FaceVideoApi.java

License:Apache License

private com.squareup.okhttp.Call facevideoGetCall(Integer facevideoId, Integer responseId,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // create path and map variables
    String localVarPath = "/facevideo".replaceAll("\\{format\\}", "json");

    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    if (facevideoId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "facevideo_id", facevideoId));
    if (responseId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "response_id", responseId));

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override//w  ww  .  jav a 2s .  c  o m
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams,
            localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.FaceVideoApi.java

License:Apache License

private com.squareup.okhttp.Call facevideoPostCall(File filename, Boolean sandbox, Integer responseId,
        Integer researchId, Integer mediaId, Integer respondentId, Boolean processVideo,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // create path and map variables
    String localVarPath = "/facevideo".replaceAll("\\{format\\}", "json");

    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    if (sandbox != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "sandbox", sandbox));
    if (responseId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "response_id", responseId));
    if (researchId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "research_id", researchId));
    if (mediaId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "media_id", mediaId));
    if (respondentId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "respondent_id", respondentId));
    if (processVideo != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "processVideo", processVideo));

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();
    if (filename != null)
        localVarFormParams.put("filename", filename);

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "multipart/form-data" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override//w w w.j ava 2  s.  c  o  m
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "POST", localVarQueryParams, localVarPostBody,
            localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.FaceVideoApi.java

License:Apache License

private com.squareup.okhttp.Call facevideoPutCall(Boolean sandbox, Integer responseId, Integer researchId,
        Integer mediaId, Integer respondentId, Boolean processVideo, FaceVideoUpload body,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = body;

    // create path and map variables
    String localVarPath = "/facevideo".replaceAll("\\{format\\}", "json");

    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    if (sandbox != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "sandbox", sandbox));
    if (responseId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "response_id", responseId));
    if (researchId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "research_id", researchId));
    if (mediaId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "media_id", mediaId));
    if (respondentId != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "respondent_id", respondentId));
    if (processVideo != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "processVideo", processVideo));

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override//from   w  ww.  j  av a  2s.co m
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams,
            localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.MediaApi.java

License:Apache License

private com.squareup.okhttp.Call mediaGetCall(Integer skip, Integer limit, String where, String sort,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // create path and map variables
    String localVarPath = "/media".replaceAll("\\{format\\}", "json");

    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    if (skip != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "skip", skip));
    if (limit != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "limit", limit));
    if (where != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "where", where));
    if (sort != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "sort", sort));

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override/*from  w w  w  .  jav a2 s .  c  om*/
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams,
            localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.MediaApi.java

License:Apache License

private com.squareup.okhttp.Call mediaMediaIdDeleteCall(Integer mediaId,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // verify the required parameter 'mediaId' is set
    if (mediaId == null) {
        throw new ApiException(
                "Missing the required parameter 'mediaId' when calling mediaMediaIdDelete(Async)");
    }//from   ww w.j  a va  2 s.c  om

    // create path and map variables
    String localVarPath = "/media/{media_id}".replaceAll("\\{format\\}", "json")
            .replaceAll("\\{" + "media_id" + "\\}", apiClient.escapeString(mediaId.toString()));

    List<Pair> localVarQueryParams = new ArrayList<Pair>();

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "DELETE", localVarQueryParams, localVarPostBody,
            localVarHeaderParams, localVarFormParams, localVarAuthNames, progressRequestListener);
}

From source file:co.uk.crowdemotion.MediaApi.java

License:Apache License

private com.squareup.okhttp.Call mediaMediaIdGetCall(Integer mediaId, Boolean presignedUrl,
        final ProgressResponseBody.ProgressListener progressListener,
        final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
    Object localVarPostBody = null;

    // verify the required parameter 'mediaId' is set
    if (mediaId == null) {
        throw new ApiException("Missing the required parameter 'mediaId' when calling mediaMediaIdGet(Async)");
    }//from w w w  .  j  a v a 2s.c  om

    // create path and map variables
    String localVarPath = "/media/{media_id}".replaceAll("\\{format\\}", "json")
            .replaceAll("\\{" + "media_id" + "\\}", apiClient.escapeString(mediaId.toString()));

    List<Pair> localVarQueryParams = new ArrayList<Pair>();
    if (presignedUrl != null)
        localVarQueryParams.addAll(apiClient.parameterToPairs("", "presignedUrl", presignedUrl));

    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    Map<String, Object> localVarFormParams = new HashMap<String, Object>();

    final String[] localVarAccepts = { "application/json" };
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null)
        localVarHeaderParams.put("Accept", localVarAccept);

    final String[] localVarContentTypes = { "application/json" };
    final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
    localVarHeaderParams.put("Content-Type", localVarContentType);

    if (progressListener != null) {
        apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
            @Override
            public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain)
                    throws IOException {
                com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
    }

    String[] localVarAuthNames = new String[] { "api_key" };
    return apiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams,
            localVarFormParams, localVarAuthNames, progressRequestListener);
}