Search in sources :

Example 1 with Builder

use of okhttp3.HttpUrl.Builder in project buck by facebook.

the class HttpArtifactCache method storeImpl.

@Override
protected void storeImpl(ArtifactInfo info, final Path file, final Finished.Builder eventBuilder) throws IOException {
    // Build the request, hitting the multi-key endpoint.
    Request.Builder builder = new Request.Builder();
    final HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(info, new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return projectFilesystem.newFileInputStream(file);
        }
    });
    eventBuilder.getStoreBuilder().setRequestSizeBytes(storeRequest.getContentLength());
    // Wrap the file into a `RequestBody` which uses `ProjectFilesystem`.
    builder.put(new RequestBody() {

        @Override
        public MediaType contentType() {
            return OCTET_STREAM_CONTENT_TYPE;
        }

        @Override
        public long contentLength() throws IOException {
            return storeRequest.getContentLength();
        }

        @Override
        public void writeTo(BufferedSink bufferedSink) throws IOException {
            StoreWriteResult writeResult = storeRequest.write(bufferedSink.outputStream());
            eventBuilder.getStoreBuilder().setArtifactContentHash(writeResult.getArtifactContentHashCode().toString());
        }
    });
    // Dispatch the store operation and verify it succeeded.
    try (HttpResponse response = storeClient.makeRequest("/artifacts/key", builder)) {
        final boolean requestFailed = response.statusCode() != HttpURLConnection.HTTP_ACCEPTED;
        if (requestFailed) {
            reportFailure("store(%s, %s): unexpected response: [%d:%s].", response.requestUrl(), info.getRuleKeys(), response.statusCode(), response.statusMessage());
        }
        eventBuilder.getStoreBuilder().setWasStoreSuccessful(!requestFailed);
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 2 with Builder

use of okhttp3.HttpUrl.Builder in project okhttputils by hongyangAndroid.

the class LoggerInterceptor method logForResponse.

private Response logForResponse(Response response) {
    try {
        //===>response log
        Log.e(tag, "========response'log=======");
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        Log.e(tag, "url : " + clone.request().url());
        Log.e(tag, "code : " + clone.code());
        Log.e(tag, "protocol : " + clone.protocol());
        if (!TextUtils.isEmpty(clone.message()))
            Log.e(tag, "message : " + clone.message());
        if (showResponse) {
            ResponseBody body = clone.body();
            if (body != null) {
                MediaType mediaType = body.contentType();
                if (mediaType != null) {
                    Log.e(tag, "responseBody's contentType : " + mediaType.toString());
                    if (isText(mediaType)) {
                        String resp = body.string();
                        Log.e(tag, "responseBody's content : " + resp);
                        body = ResponseBody.create(mediaType, resp);
                        return response.newBuilder().body(body).build();
                    } else {
                        Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!");
                    }
                }
            }
        }
        Log.e(tag, "========response'log=======end");
    } catch (Exception e) {
    //            e.printStackTrace();
    }
    return response;
}
Also used : Response(okhttp3.Response) MediaType(okhttp3.MediaType) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 3 with Builder

use of okhttp3.HttpUrl.Builder in project SeriesGuide by UweTrottmann.

the class HttpClientModule method provideOkHttpClient.

/**
     * Returns this apps {@link OkHttpClient} with enabled response cache. Should be used with API
     * calls.
     */
@Provides
@Singleton
OkHttpClient provideOkHttpClient(SgApp app, Cache cache) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    builder.readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    builder.addInterceptor(new SgTmdbInterceptor());
    builder.addNetworkInterceptor(new SgTheTvdbInterceptor(app));
    builder.addNetworkInterceptor(new SgTraktInterceptor(app));
    builder.authenticator(new AllApisAuthenticator(app));
    builder.cache(cache);
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SgTmdbInterceptor(com.battlelancer.seriesguide.tmdbapi.SgTmdbInterceptor) SgTraktInterceptor(com.battlelancer.seriesguide.traktapi.SgTraktInterceptor) SgTheTvdbInterceptor(com.battlelancer.seriesguide.thetvdbapi.SgTheTvdbInterceptor) AllApisAuthenticator(com.battlelancer.seriesguide.util.AllApisAuthenticator) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 4 with Builder

use of okhttp3.HttpUrl.Builder in project MVCHelper by LuckyJayce.

the class PostFileMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    MultipartBody.Builder builder = new MultipartBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    if (httpbodys != null) {
        for (Entry<String, Data2<String, RequestBody>> entry : httpbodys.entrySet()) {
            String key = entry.getKey();
            Data2<String, RequestBody> body = entry.getValue();
            builder.addFormDataPart(key, body.getValue1(), body.getValue2());
        }
    }
    RequestBody formBody = builder.build();
    if (listener != null) {
        formBody = new CountingRequestBody(formBody, listener);
    }
    return new Request.Builder().url(url).post(formBody);
}
Also used : Data2(com.shizhefei.mvc.data.Data2) MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 5 with Builder

use of okhttp3.HttpUrl.Builder in project MVCHelper by LuckyJayce.

the class PostMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).post(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)204 Response (okhttp3.Response)146 OkHttpClient (okhttp3.OkHttpClient)141 IOException (java.io.IOException)111 RequestBody (okhttp3.RequestBody)81 Test (org.junit.Test)75 HttpUrl (okhttp3.HttpUrl)47 File (java.io.File)42 MultipartBody (okhttp3.MultipartBody)40 MockResponse (okhttp3.mockwebserver.MockResponse)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