Search in sources :

Example 31 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project edx-app-android by openedx.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody)

Example 32 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project java-sdk by dapr.

the class DaprHttp method doInvokeApi.

/**
 * Invokes an API that returns a text payload.
 *
 * @param method        HTTP method.
 * @param pathSegments  Array of path segments (/a/b/c -> ["a", "b", "c"]).
 * @param urlParameters Parameters in the URL
 * @param content       payload to be posted.
 * @param headers       HTTP headers.
 * @param context       OpenTelemetry's Context.
 * @return CompletableFuture for Response.
 */
private CompletableFuture<Response> doInvokeApi(String method, String[] pathSegments, Map<String, List<String>> urlParameters, byte[] content, Map<String, String> headers, Context context) {
    final String requestId = UUID.randomUUID().toString();
    RequestBody body;
    String contentType = headers != null ? headers.get(Metadata.CONTENT_TYPE) : null;
    MediaType mediaType = contentType == null ? MEDIA_TYPE_APPLICATION_JSON : MediaType.get(contentType);
    if (content == null) {
        body = mediaType.equals(MEDIA_TYPE_APPLICATION_JSON) ? REQUEST_BODY_EMPTY_JSON : RequestBody.Companion.create(new byte[0], mediaType);
    } else {
        body = RequestBody.Companion.create(content, mediaType);
    }
    HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
    urlBuilder.scheme(DEFAULT_HTTP_SCHEME).host(this.hostname).port(this.port);
    for (String pathSegment : pathSegments) {
        urlBuilder.addPathSegment(pathSegment);
    }
    Optional.ofNullable(urlParameters).orElse(Collections.emptyMap()).entrySet().stream().forEach(urlParameter -> Optional.ofNullable(urlParameter.getValue()).orElse(Collections.emptyList()).stream().forEach(urlParameterValue -> urlBuilder.addQueryParameter(urlParameter.getKey(), urlParameterValue)));
    Request.Builder requestBuilder = new Request.Builder().url(urlBuilder.build()).addHeader(HEADER_DAPR_REQUEST_ID, requestId);
    if (context != null) {
        context.stream().filter(entry -> ALLOWED_CONTEXT_IN_HEADERS.contains(entry.getKey().toString().toLowerCase())).forEach(entry -> requestBuilder.addHeader(entry.getKey().toString(), entry.getValue().toString()));
    }
    if (HttpMethods.GET.name().equals(method)) {
        requestBuilder.get();
    } else if (HttpMethods.DELETE.name().equals(method)) {
        requestBuilder.delete();
    } else {
        requestBuilder.method(method, body);
    }
    String daprApiToken = Properties.API_TOKEN.get();
    if (daprApiToken != null) {
        requestBuilder.addHeader(Headers.DAPR_API_TOKEN, daprApiToken);
    }
    if (headers != null) {
        Optional.ofNullable(headers.entrySet()).orElse(Collections.emptySet()).stream().forEach(header -> {
            requestBuilder.addHeader(header.getKey(), header.getValue());
        });
    }
    Request request = requestBuilder.build();
    CompletableFuture<Response> future = new CompletableFuture<>();
    this.httpClient.newCall(request).enqueue(new ResponseFutureCallback(future));
    return future;
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RequestBody(okhttp3.RequestBody) HashSet(java.util.HashSet) DaprError(io.dapr.exceptions.DaprError) Map(java.util.Map) Properties(io.dapr.config.Properties) Call(okhttp3.Call) Callback(okhttp3.Callback) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) Request(okhttp3.Request) Context(reactor.util.context.Context) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) DaprException(io.dapr.exceptions.DaprException) Metadata(io.dapr.client.domain.Metadata) Optional(java.util.Optional) HttpUrl(okhttp3.HttpUrl) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) CompletableFuture(java.util.concurrent.CompletableFuture) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 33 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project teamscale-jacoco-agent by cqse.

the class AzureFileStorageUploader method fillFile.

/**
 * Fills the file defined by the name with the given data. Should be used with {@link #createFile(File, String)},
 * because the request only writes exactly the length of the given data, so the file should be exactly as big as the
 * data, otherwise it will be partially filled or is not big enough.
 */
private Response<ResponseBody> fillFile(File zipFile, String fileName) throws IOException, UploaderException {
    String filePath = uploadUrl.url().getPath() + fileName;
    String range = "bytes=0-" + (zipFile.length() - 1);
    String contentType = "application/octet-stream";
    Map<String, String> headers = AzureFileStorageHttpUtils.getBaseHeaders();
    headers.put(X_MS_WRITE, "update");
    headers.put(X_MS_RANGE, range);
    headers.put(CONTENT_LENGTH, "" + zipFile.length());
    headers.put(CONTENT_TYPE, contentType);
    Map<String, String> queryParameters = new HashMap<>();
    queryParameters.put("comp", "range");
    String auth = AzureFileStorageHttpUtils.getAuthorizationString(PUT, account, accessKey, filePath, headers, queryParameters);
    headers.put(AUTHORIZATION, auth);
    RequestBody content = RequestBody.create(MediaType.parse(contentType), zipFile);
    return getApi().putData(filePath, headers, queryParameters, content).execute();
}
Also used : HashMap(java.util.HashMap) RequestBody(okhttp3.RequestBody)

Example 34 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project teamscale-jacoco-agent by cqse.

the class IHttpUploadApi method uploadCoverageZip.

/**
 * Convenience method to perform an {@link #upload(okhttp3.MultipartBody.Part)} call for a coverage zip.
 */
public default Response<ResponseBody> uploadCoverageZip(File data) throws IOException {
    RequestBody body = RequestBody.create(MediaType.parse("application/zip"), data);
    MultipartBody.Part part = MultipartBody.Part.createFormData("file", "coverage.zip", body);
    return upload(part).execute();
}
Also used : MultipartBody(okhttp3.MultipartBody) RequestBody(okhttp3.RequestBody)

Example 35 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project teamscale-jacoco-agent by cqse.

the class TeamscaleClient method uploadReport.

/**
 * Uploads one in-memory report to Teamscale.
 */
public void uploadReport(EReportFormat reportFormat, String report, CommitDescriptor commitDescriptor, String revision, String partition, String message) throws IOException {
    RequestBody requestBody = RequestBody.create(MultipartBody.FORM, report);
    service.uploadReport(projectId, commitDescriptor, revision, partition, reportFormat, message, requestBody);
}
Also used : RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)1358 Request (okhttp3.Request)785 Response (okhttp3.Response)598 IOException (java.io.IOException)420 Test (org.junit.Test)235 OkHttpClient (okhttp3.OkHttpClient)216 MultipartBody (okhttp3.MultipartBody)213 MediaType (okhttp3.MediaType)204 Call (okhttp3.Call)198 JSONObject (org.json.JSONObject)183 ResponseBody (okhttp3.ResponseBody)177 Callback (okhttp3.Callback)115 FormBody (okhttp3.FormBody)106 Buffer (okio.Buffer)98 File (java.io.File)92 Map (java.util.Map)90 JsonObject (io.vertx.core.json.JsonObject)89 Headers (okhttp3.Headers)88 HashMap (java.util.HashMap)83 HttpUrl (okhttp3.HttpUrl)80