Search in sources :

Example 71 with Builder

use of okhttp3.Request.Builder in project docker-client by spotify.

the class DefaultDockerClientUnitTest method testBindBuilderSelinuxLabeling.

@Test
public void testBindBuilderSelinuxLabeling() throws Exception {
    final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
    final Bind bindNoSelinuxLabel = HostConfig.Bind.builder().from("noselinux").to("noselinux").build();
    final Bind bindSharedSelinuxContent = HostConfig.Bind.builder().from("shared").to("shared").selinuxLabeling(true).build();
    final Bind bindPrivateSelinuxContent = HostConfig.Bind.builder().from("private").to("private").selinuxLabeling(false).build();
    final HostConfig hostConfig = HostConfig.builder().binds(bindNoSelinuxLabel, bindSharedSelinuxContent, bindPrivateSelinuxContent).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).build();
    server.enqueue(new MockResponse());
    dockerClient.createContainer(containerConfig);
    final RecordedRequest recordedRequest = takeRequestImmediately();
    final JsonNode requestJson = toJson(recordedRequest.getBody());
    final JsonNode binds = requestJson.get("HostConfig").get("Binds");
    assertThat(binds.isArray(), is(true));
    Set<String> bindSet = childrenTextNodes((ArrayNode) binds);
    assertThat(bindSet, hasSize(3));
    assertThat(bindSet, hasItem(allOf(containsString("noselinux"), not(containsString("z")), not(containsString("Z")))));
    assertThat(bindSet, hasItem(allOf(containsString("shared"), containsString("z"))));
    assertThat(bindSet, hasItem(allOf(containsString("private"), containsString("Z"))));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Bind(com.spotify.docker.client.messages.HostConfig.Bind) ConfigBind(com.spotify.docker.client.messages.swarm.ConfigBind) HostConfig(com.spotify.docker.client.messages.HostConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 72 with Builder

use of okhttp3.Request.Builder in project fitpay-android-sdk by fitpay.

the class BaseClient method getOkHttpClient.

public static OkHttpClient.Builder getOkHttpClient(boolean enabledLogging) {
    OkHttpClient.Builder builder = getDefaultOkHttpClient();
    int connectTimeout = Integer.valueOf(ApiManager.getConfig().get(ApiManager.PROPERTY_HTTP_CONNECT_TIMEOUT));
    int readTimeout = Integer.valueOf(ApiManager.getConfig().get(ApiManager.PROPERTY_HTTP_READ_TIMEOUT));
    int writeTimeout = Integer.valueOf(ApiManager.getConfig().get(ApiManager.PROPERTY_HTTP_WRITE_TIMEOUT));
    builder = builder.connectTimeout(connectTimeout, TimeUnit.SECONDS).readTimeout(readTimeout, TimeUnit.SECONDS).writeTimeout(writeTimeout, TimeUnit.SECONDS).followRedirects(true).followSslRedirects(true).retryOnConnectionFailure(true);
    if (enabledLogging) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder = builder.addInterceptor(logging);
    }
    return enableTls12OnPreLollipop(builder);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 73 with Builder

use of okhttp3.Request.Builder in project AndoridLib by twp520.

the class HttpLogInterceptor method logForResponse.

private Response logForResponse(Response response) {
    try {
        // ===>response log
        if (showLog) {
            Log.e(tag, "********响应日志开始********");
            Response.Builder builder = response.newBuilder();
            Response clone = builder.build();
            Log.e(tag, "url : " + clone.request().url());
            Log.e(tag, "code : " + clone.code());
            if (!TextUtils.isEmpty(clone.message()))
                Log.e(tag, "message : " + clone.message());
            ResponseBody body = clone.body();
            if (body != null) {
                MediaType mediaType = body.contentType();
                if (mediaType != null) {
                    if (isText(mediaType)) {
                        String resp = body.string();
                        // String res = Des3.decode(resp);
                        Log.e(tag, "响应内容: " + resp);
                        Log.e(tag, "********响应日志结束********");
                        body = ResponseBody.create(mediaType, resp);
                        return response.newBuilder().body(body).build();
                    } else {
                        Log.e(tag, "响应内容 : " + " 发生错误");
                    }
                }
            }
            Log.e(tag, "********响应日志结束********");
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    return response;
}
Also used : Response(okhttp3.Response) MediaType(okhttp3.MediaType) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 74 with Builder

use of okhttp3.Request.Builder in project Collar by CodeZsx.

the class HttpUtils method getRetrofitWithAppKey.

private Retrofit getRetrofitWithAppKey(String baseUrl) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request r = addParam(chain.request());
            Log.i(TAG, r.method() + " " + r.url().toString());
            return chain.proceed(r);
        }

        private Request addParam(Request oldRequest) {
            if (oldRequest.method().equals("POST")) {
                return oldRequest;
            }
            HttpUrl.Builder builder = oldRequest.url().newBuilder().setEncodedQueryParameter("source", Config.APP_KEY);
            return oldRequest.newBuilder().method(oldRequest.method(), oldRequest.body()).url(builder.build()).build();
        }
    }).build();
    return new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(baseUrl).build();
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor) HttpUrl(okhttp3.HttpUrl)

Example 75 with Builder

use of okhttp3.Request.Builder in project Collar by CodeZsx.

the class HttpUtils method getRetrofit.

private Retrofit getRetrofit(String baseUrl) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request r = addParam(chain.request());
            Log.i(TAG, r.method() + " " + r.url().toString());
            return chain.proceed(r);
        }

        private Request addParam(Request oldRequest) {
            if (oldRequest.method().equals("POST")) {
                return oldRequest;
            }
            HttpUrl.Builder builder = oldRequest.url().newBuilder().setEncodedQueryParameter("access_token", AccessTokenKeeper.getInstance().getAccessToken());
            return oldRequest.newBuilder().method(oldRequest.method(), oldRequest.body()).url(builder.build()).build();
        }
    }).build();
    return new Retrofit.Builder().client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(baseUrl).build();
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor) HttpUrl(okhttp3.HttpUrl)

Aggregations

Request (okhttp3.Request)170 Response (okhttp3.Response)120 OkHttpClient (okhttp3.OkHttpClient)103 IOException (java.io.IOException)89 RequestBody (okhttp3.RequestBody)68 Test (org.junit.Test)68 File (java.io.File)36 MultipartBody (okhttp3.MultipartBody)36 HttpUrl (okhttp3.HttpUrl)34 Map (java.util.Map)31 MockResponse (okhttp3.mockwebserver.MockResponse)31 Call (okhttp3.Call)26 Interceptor (okhttp3.Interceptor)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)24 Builder (okhttp3.OkHttpClient.Builder)21 Retrofit (retrofit2.Retrofit)21 Builder (okhttp3.Request.Builder)19 FormBody (okhttp3.FormBody)18 ResponseBody (okhttp3.ResponseBody)18