Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

cloudconvert/cloudconvert-java

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cloudconvert-java

This is the official Java SDK v2 for the CloudConvert API v2. Build Status

Installation

$ mvn clean install -U

Creating API Client

Configuration

By default, API Key, Sandbox and Webhook Signing Secret are being read from application.properties file

CLOUDCONVERT_API_KEY=<api-key>
CLOUDCONVERT_SANDBOX=<true|false>
CLOUDCONVERT_WEBHOOK_SIGNING_SECRET=<api-url>

It is also possible to provide configuration above using environment variables, custom properties file, system properties and string variables. For all options, CLOUDCONVERT_API_KEY, CLOUDCONVERT_SANDBOX and CLOUDCONVERT_WEBHOOK_SIGNING_SECRET variable names should be used.

Default (synchronous) client
// Using configuration from `application.properties` file
new CloudConvertClient();

// Using configuration from environment variables
new CloudConvertClient(new EnvironmentVariableSettingsProvider());

// Using configuration from custom properties file
new CloudConvertClient(new PropertyFileSettingsProvider("custom.properties"));

// Using configuration from string variables
new CloudConvertClient(new StringSettingsProvider("api-url", "webhook-signing-secret"));

// Using configuration from system properties
new CloudConvertClient(new SystemPropertySettingsProvider()); 
Asynchronous client
// Using configuration from `application.properties` file
new AsyncCloudConvertClient();

// Using configuration from environment variables
new AsyncCloudConvertClient(new EnvironmentVariableSettingsProvider());

// Using configuration from custom properties file
new AsyncCloudConvertClient(new PropertyFileSettingsProvider("custom.properties"));

// Using configuration from string variables
new AsyncCloudConvertClient(new StringSettingsProvider("api-url", "webhook-signing-secret"));

// Using configuration from system properties
new AsyncCloudConvertClient(new SystemPropertySettingsProvider());

Creating Jobs

Default (synchronous) client
// Create a client
final CloudConvertClient cloudConvertClient = new CloudConvertClient();

// Create a job
final JobResponse createJobResponse = cloudConvertClient.jobs().create(
    ImmutableMap.of(
        "import-my-file", new UrlImportRequest().setUrl("import-url"),
        "convert-my-file", new ConvertFilesTaskRequest().setInput("import-my-file").set("width", 100).set("height", 100),
        "export-my-file", new UrlExportRequest().setInput("convert-my-file")
    )
).getBody();

// Get a job id
final String jobId = createJobResponse.getId();

// Wait for a job completion
final JobResponse waitJobResponse = cloudConvertClient.jobs().wait(jobId).getBody();

// Get an export/url task id
final String exportUrlTaskId = waitJobResponse.getTasks().stream().filter(taskResponse -> taskResponse.getName().equals("export-my-file")).findFirst().get().getId();
Asynchronous client
// Create a client
final AsyncCloudConvertClient asyncCloudConvertClient = new AsyncCloudConvertClient();

// Create a job
final JobResponse createJobResponse = asyncCloudConvertClient.jobs().create(
    ImmutableMap.of(
        "import-my-file", new UrlImportRequest().setUrl("import-url"),
        "convert-my-file", new ConvertFilesTaskRequest().setInput("import-my-file").set("width", 100).set("height", 100),
        "export-my-file", new UrlExportRequest().setInput("convert-my-file")
    )
).get().getBody();

// Get a job id
final String jobId = createJobResponse.getId();

// Wait for a job completion
final JobResponse waitJobResponse = asyncCloudConvertClient.jobs().wait(jobId).get().getBody();

// Get an export/url task id
final String exportUrlTaskId = waitJobResponse.getTasks().stream().filter(taskResponse -> taskResponse.getName().equals("export-my-file")).findFirst().get().getId();

Downloading Files

CloudConvert can generate public URLs using export/url tasks. You can use these URLs to download output files.

Default (synchronous) client
// Create a client
final CloudConvertClient cloudConvertClient = new CloudConvertClient();

// Create an export/url task
final TaskResponse exportUrlTaskResponse = cloudConvertClient.exportUsing().url(new UrlExportRequest()).getBody();

// Get an export/url task id
final String exportUrlTaskId = exportUrlTaskResponse.getId();

// Wait for an export/url task to be finished
final TaskResponse waitUrlExportTaskResponse = cloudConvertClient.tasks().wait(exportUrlTaskId).getBody();

// Get a url of export/url task
final String exportUrl = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("url");

// Get file as input stream using url of export/url task
final InputStream inputStream = cloudConvertClient.files().download(exportUrl).getBody();
Asynchronous client
// Create a client
final AsyncCloudConvertClient asyncCloudConvertClient = new AsyncCloudConvertClient();

// Create an export/url task
final TaskResponse exportUrlTaskResponse = asyncCloudConvertClient.exportUsing().url(new UrlExportRequest()).get().getBody();

// Get an export/url task id
final String exportUrlTaskId = exportUrlTaskResponse.getId();

// Wait for an export/url task to be finished
final TaskResponse waitUrlExportTaskResponse = asyncCloudConvertClient.tasks().wait(exportUrlTaskId).get().getBody();

// Get a url of export/url task
final String exportUrl = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("url");

// Get file as input stream using url of export/url task
final InputStream inputStream = asyncCloudConvertClient.files().download(exportUrl).get().getBody();

Uploading Files

Uploads to CloudConvert are done via import/upload tasks (see the docs). This SDK offers a convenient upload method:

Default (synchronous) client
// Create a client
final CloudConvertClient cloudConvertClient = new CloudConvertClient();

// File as input stream
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.jpg");

// Upload file using import/upload task
final TaskResponse uploadImportTaskResponse = cloudConvertClient.importUsing().upload(new UploadImportRequest(), inputStream).getBody();

// Wait for import/upload task to be finished
final TaskResponse waitUploadImportTaskResponse = cloudConvertClient.tasks().wait(uploadImportTaskResponse.getId()).getBody();
Asynchronous client
// Create a client
final AsyncCloudConvertClient asyncCloudConvertClient = new CloudConvertClient();

// File as input stream
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("file.jpg");

// Upload file using import/upload task
final TaskResponse uploadImportTaskResponse = asyncCloudConvertClient.importUsing().upload(new UploadImportRequest(), inputStream).get().getBody();

// Wait for import/upload task to be finished
final TaskResponse waitUploadImportTaskResponse = asyncCloudConvertClient.tasks().wait(uploadImportTaskResponse.getId()).get().getBody();

Signing Webhook

The node SDK allows to verify webhook requests received from CloudConvert.

Default (synchronous) client
// Create a client
final CloudConvertClient cloudConvertClient = new CloudConvertClient();

// The JSON payload from the raw request body.
final String payload = "payload";

// The value of the "CloudConvert-Signature" header.
final String signature = "signature";

// You can find it in your webhook settings.
final String secret = "secret";

// Returns true if signature is valid, and false if signature is invalid
final boolean isValid = cloudConvertClient.webhooks().verify(payload, signature);
Asynchronous client
// Create a client
final AsyncCloudConvertClient asyncCloudConvertClient = new AsyncCloudConvertClient();

// The JSON payload from the raw request body.
final String payload = "payload";

// The value of the "CloudConvert-Signature" header.
final String signature = "signature";

// You can find it in your webhook settings.
final String secret = "secret";

// Returns true if signature is valid, and false if signature is invalid
final boolean isValid = asyncCloudConvertClient.webhooks().verify(payload, signature);

Unit Tests

$ mvn clean install -U -Punit-tests

Integration Tests

$ mvn clean install -U -Pintegration-tests

Resources

About

Official CloudConvert Java SDK

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 7

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.