Java Code Examples for org.asynchttpclient.request.body.multipart.StringPart

The following examples show how to use org.asynchttpclient.request.body.multipart.StringPart. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: pulsar   Source File: FunctionsImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> uploadFunctionAsync(String sourceFile, String path) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder = post(functions.path("upload").getUri().toASCIIString())
                .addBodyPart(new FilePart("data", new File(sourceFile), MediaType.APPLICATION_OCTET_STREAM))
                .addBodyPart(new StringPart("path", path, MediaType.TEXT_PLAIN));

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build()).toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 2
Source Project: pulsar   Source File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> createFunctionAsync(FunctionConfig functionConfig, String fileName) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(functions.path(functionConfig.getTenant()).path(functionConfig.getNamespace())
                        .path(functionConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("functionConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(functionConfig), MediaType.APPLICATION_JSON));

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
 
Example 3
Source Project: pulsar   Source File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> updateFunctionAsync(
        FunctionConfig functionConfig, String fileName, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(functions.path(functionConfig.getTenant())
                        .path(functionConfig.getNamespace())
                        .path(functionConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("functionConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(functionConfig), MediaType.APPLICATION_JSON));

        if (updateOptions != null) {
            builder.addBodyPart(new StringPart("updateOptions", ObjectMapperFactory.getThreadLocal()
                    .writeValueAsString(updateOptions), MediaType.APPLICATION_JSON));
        }

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 4
Source Project: pulsar   Source File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> putFunctionStateAsync(
        String tenant, String namespace, String function, FunctionState state) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(functions.path(tenant).path(namespace).path(function)
                        .path("state").path(state.getKey()).getUri().toASCIIString());
        builder.addBodyPart(new StringPart("state", ObjectMapperFactory.getThreadLocal()
                .writeValueAsString(state), MediaType.APPLICATION_JSON));
        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(getApiException(
                                Response.status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
 
Example 5
Source Project: pulsar   Source File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Void> updateOnWorkerLeaderAsync(String tenant, String namespace,
                                                         String function, byte[] functionMetaData,
                                                         boolean delete) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(functions.path("leader").path(tenant).path(namespace)
                        .path(function).getUri().toASCIIString())
                        .addBodyPart(new ByteArrayPart("functionMetaData", functionMetaData))
                .addBodyPart(new StringPart("delete", Boolean.toString(delete)));

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
 
Example 6
Source Project: pulsar   Source File: SourcesImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> createSourceAsync(SourceConfig sourceConfig, String fileName) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(source.path(sourceConfig.getTenant())
                        .path(sourceConfig.getNamespace()).path(sourceConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("sourceConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(sourceConfig), MediaType.APPLICATION_JSON));

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(source, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 7
Source Project: pulsar   Source File: SourcesImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> updateSourceAsync(
        SourceConfig sourceConfig, String fileName, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(source.path(sourceConfig.getTenant()).path(sourceConfig.getNamespace())
                        .path(sourceConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("sourceConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(sourceConfig), MediaType.APPLICATION_JSON));

        if (updateOptions != null) {
            builder.addBodyPart(new StringPart("updateOptions",
                    ObjectMapperFactory.getThreadLocal().writeValueAsString(updateOptions),
                    MediaType.APPLICATION_JSON));
        }

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(source, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 8
Source Project: pulsar   Source File: SinksImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> createSinkAsync(SinkConfig sinkConfig, String fileName) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(sink.path(sinkConfig.getTenant())
                        .path(sinkConfig.getNamespace()).path(sinkConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("sinkConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(sinkConfig), MediaType.APPLICATION_JSON));

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(sink, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 9
Source Project: pulsar   Source File: SinksImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> updateSinkAsync(
        SinkConfig sinkConfig, String fileName, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(sink.path(sinkConfig.getTenant()).path(sinkConfig.getNamespace())
                        .path(sinkConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("sinkConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(sinkConfig), MediaType.APPLICATION_JSON));

        if (updateOptions != null) {
            builder.addBodyPart(new StringPart("updateOptions",
                    ObjectMapperFactory.getThreadLocal()
                            .writeValueAsString(updateOptions), MediaType.APPLICATION_JSON));
        }

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(sink, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example 10
Source Project: zendesk-java-client   Source File: Zendesk.java    License: Apache License 2.0 5 votes vote down vote up
public ArticleAttachments createUploadArticle(long articleId, File file, boolean inline) throws IOException {
RequestBuilder builder = reqBuilder("POST", tmpl("/help_center/articles/{id}/attachments.json").set("id", articleId).toString());
    builder.setHeader("Content-Type", "multipart/form-data");

    if (inline)
        builder.addBodyPart(new StringPart("inline", "true"));

  builder.addBodyPart(
        new FilePart("file", file, "application/octet-stream", StandardCharsets.UTF_8, file.getName()));
    final Request req = builder.build();
    return complete(submit(req, handle(ArticleAttachments.class, "article_attachment")));
}