-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary upload + Json payload samples for Java
- Loading branch information
1 parent
19efb94
commit 3b98d0e
Showing
35 changed files
with
3,303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import okhttp3.*; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class Bmp { | ||
|
||
// Specify the path to your file here, or as the first argument when running the program. | ||
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; | ||
|
||
// Specify your API key here, or in the environment variable PDFREST_API_KEY. | ||
// You can also put the environment variable in a .env file. | ||
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | ||
|
||
public static void main(String[] args) { | ||
File inputFile; | ||
if (args.length > 0) { | ||
inputFile = new File(args[0]); | ||
} else { | ||
inputFile = new File(DEFAULT_FILE_PATH); | ||
} | ||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
String uploadString = uploadFile(inputFile); | ||
JSONObject uploadJSON = new JSONObject(uploadString); | ||
if (uploadJSON.has("error")) { | ||
System.out.println("Error during upload: " + uploadString); | ||
return; | ||
} | ||
JSONArray fileArray = uploadJSON.getJSONArray("files"); | ||
|
||
JSONObject fileObject = fileArray.getJSONObject(0); | ||
|
||
String uploadedID = fileObject.get("id").toString(); | ||
|
||
String JSONString = String.format("{\"id\":\"%s\"}", uploadedID); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(JSONString, MediaType.parse("application/json")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.url("https://api.pdfrest.com/bmp") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = | ||
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println("Processing Result code " + response.code()); | ||
if (response.body() != null) { | ||
System.out.println(prettyJson(response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static String prettyJson(String json) { | ||
// https://stackoverflow.com/a/9583835/11996393 | ||
return new JSONObject(json).toString(4); | ||
} | ||
|
||
// This function is just a copy of the 'Upload.java' file to upload a binary file | ||
private static String uploadFile(File inputFile) { | ||
|
||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(inputFile, MediaType.parse("application/pdf")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.header("Content-Filename", "File.pdf") | ||
.url("https://api.pdfrest.com/upload") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = new OkHttpClient().newBuilder().build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println("Upload Result code " + response.code()); | ||
if (response.body() != null) { | ||
return response.body().string(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import okhttp3.*; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class CompressedPDF { | ||
|
||
// Specify the path to your file here, or as the first argument when running the program. | ||
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; | ||
|
||
// Specify your API key here, or in the environment variable PDFREST_API_KEY. | ||
// You can also put the environment variable in a .env file. | ||
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | ||
|
||
public static void main(String[] args) { | ||
File inputFile; | ||
if (args.length > 0) { | ||
inputFile = new File(args[0]); | ||
} else { | ||
inputFile = new File(DEFAULT_FILE_PATH); | ||
} | ||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
String uploadString = uploadFile(inputFile); | ||
JSONObject uploadJSON = new JSONObject(uploadString); | ||
if (uploadJSON.has("error")) { | ||
System.out.println("Error during upload: " + uploadString); | ||
return; | ||
} | ||
JSONArray fileArray = uploadJSON.getJSONArray("files"); | ||
|
||
JSONObject fileObject = fileArray.getJSONObject(0); | ||
|
||
String uploadedID = fileObject.get("id").toString(); | ||
|
||
String JSONString = | ||
String.format("{\"id\":\"%s\",\"compression_level\":\"medium\" }", uploadedID); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(JSONString, MediaType.parse("application/json")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.url("https://api.pdfrest.com/compressed-pdf") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = | ||
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println("Processing Result code " + response.code()); | ||
if (response.body() != null) { | ||
System.out.println(prettyJson(response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static String prettyJson(String json) { | ||
// https://stackoverflow.com/a/9583835/11996393 | ||
return new JSONObject(json).toString(4); | ||
} | ||
|
||
// This function is just a copy of the 'Upload.java' file to upload a binary file | ||
private static String uploadFile(File inputFile) { | ||
|
||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(inputFile, MediaType.parse("application/pdf")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.header("Content-Filename", "File.pdf") | ||
.url("https://api.pdfrest.com/upload") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = new OkHttpClient().newBuilder().build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println("Upload Result code " + response.code()); | ||
if (response.body() != null) { | ||
return response.body().string(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import okhttp3.*; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class DecryptedPDF { | ||
|
||
// Specify the path to your file here, or as the first argument when running the program. | ||
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; | ||
|
||
// Specify your API key here, or in the environment variable PDFREST_API_KEY. | ||
// You can also put the environment variable in a .env file. | ||
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | ||
|
||
public static void main(String[] args) { | ||
File inputFile; | ||
if (args.length > 0) { | ||
inputFile = new File(args[0]); | ||
} else { | ||
inputFile = new File(DEFAULT_FILE_PATH); | ||
} | ||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
String uploadString = uploadFile(inputFile); | ||
JSONObject uploadJSON = new JSONObject(uploadString); | ||
if (uploadJSON.has("error")) { | ||
System.out.println("Error during upload: " + uploadString); | ||
return; | ||
} | ||
JSONArray fileArray = uploadJSON.getJSONArray("files"); | ||
|
||
JSONObject fileObject = fileArray.getJSONObject(0); | ||
|
||
String uploadedID = fileObject.get("id").toString(); | ||
|
||
String JSONString = | ||
String.format("{\"id\":\"%s\",\"current_open_password\":\"password\" }", uploadedID); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(JSONString, MediaType.parse("application/json")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.url("https://api.pdfrest.com/decrypted-pdf") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = | ||
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println("Processing Result code " + response.code()); | ||
if (response.body() != null) { | ||
System.out.println(prettyJson(response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static String prettyJson(String json) { | ||
// https://stackoverflow.com/a/9583835/11996393 | ||
return new JSONObject(json).toString(4); | ||
} | ||
|
||
// This function is just a copy of the 'Upload.java' file to upload a binary file | ||
private static String uploadFile(File inputFile) { | ||
|
||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(inputFile, MediaType.parse("application/pdf")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.header("Content-Filename", "File.pdf") | ||
.url("https://api.pdfrest.com/upload") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = new OkHttpClient().newBuilder().build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println("Upload Result code " + response.code()); | ||
if (response.body() != null) { | ||
return response.body().string(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import okhttp3.*; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class EncryptedPDF { | ||
|
||
// Specify the path to your file here, or as the first argument when running the program. | ||
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; | ||
|
||
// Specify your API key here, or in the environment variable PDFREST_API_KEY. | ||
// You can also put the environment variable in a .env file. | ||
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; | ||
|
||
public static void main(String[] args) { | ||
File inputFile; | ||
if (args.length > 0) { | ||
inputFile = new File(args[0]); | ||
} else { | ||
inputFile = new File(DEFAULT_FILE_PATH); | ||
} | ||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
String uploadString = uploadFile(inputFile); | ||
JSONObject uploadJSON = new JSONObject(uploadString); | ||
if (uploadJSON.has("error")) { | ||
System.out.println("Error during upload: " + uploadString); | ||
return; | ||
} | ||
JSONArray fileArray = uploadJSON.getJSONArray("files"); | ||
|
||
JSONObject fileObject = fileArray.getJSONObject(0); | ||
|
||
String uploadedID = fileObject.get("id").toString(); | ||
|
||
String JSONString = | ||
String.format("{\"id\":\"%s\",\"new_open_password\":\"password\" }", uploadedID); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(JSONString, MediaType.parse("application/json")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.url("https://api.pdfrest.com/encrypted-pdf") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = | ||
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
System.out.println("Processing Result code " + response.code()); | ||
if (response.body() != null) { | ||
System.out.println(prettyJson(response.body().string())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static String prettyJson(String json) { | ||
// https://stackoverflow.com/a/9583835/11996393 | ||
return new JSONObject(json).toString(4); | ||
} | ||
|
||
// This function is just a copy of the 'Upload.java' file to upload a binary file | ||
private static String uploadFile(File inputFile) { | ||
|
||
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); | ||
|
||
final RequestBody requestBody = | ||
RequestBody.create(inputFile, MediaType.parse("application/pdf")); | ||
|
||
Request request = | ||
new Request.Builder() | ||
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) | ||
.header("Content-Filename", "File.pdf") | ||
.url("https://api.pdfrest.com/upload") | ||
.post(requestBody) | ||
.build(); | ||
try { | ||
OkHttpClient client = new OkHttpClient().newBuilder().build(); | ||
Response response = client.newCall(request).execute(); | ||
System.out.println("Upload Result code " + response.code()); | ||
if (response.body() != null) { | ||
return response.body().string(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return ""; | ||
} | ||
} |
Oops, something went wrong.