use of okhttp3.Request.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);
}
}
use of okhttp3.Request.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;
}
use of okhttp3.Request.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();
}
use of okhttp3.Request.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);
}
use of okhttp3.Request.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);
}
Aggregations