Search in sources :

Example 46 with Builder

use of okhttp3.HttpUrl.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 47 with Builder

use of okhttp3.HttpUrl.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 48 with Builder

use of okhttp3.HttpUrl.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 49 with Builder

use of okhttp3.HttpUrl.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 50 with Builder

use of okhttp3.HttpUrl.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)206 Response (okhttp3.Response)148 OkHttpClient (okhttp3.OkHttpClient)142 IOException (java.io.IOException)111 RequestBody (okhttp3.RequestBody)81 Test (org.junit.Test)75 HttpUrl (okhttp3.HttpUrl)47 File (java.io.File)42 MockResponse (okhttp3.mockwebserver.MockResponse)42 MultipartBody (okhttp3.MultipartBody)40 Map (java.util.Map)39 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)31 Call (okhttp3.Call)29 Interceptor (okhttp3.Interceptor)29 Retrofit (retrofit2.Retrofit)29 Builder (okhttp3.OkHttpClient.Builder)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 ResponseBody (okhttp3.ResponseBody)24 HashMap (java.util.HashMap)22 FormBody (okhttp3.FormBody)21