Skip to content

Commit aa69f99

Browse files
Merge pull request #39 from veryfi/veryfi/feature/LP-1158-Add-support-for-base64-encoded-file-data
LP-1158: Add support for base64 encoded file data, Classify and Split API methods
2 parents 4dfb6e2 + d9a5c57 commit aa69f99

36 files changed

Lines changed: 2370 additions & 66 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ Install the package using Maven:
1818
<dependency>
1919
<groupId>com.veryfi</groupId>
2020
<artifactId>veryfi-java</artifactId>
21-
<version>2.0.0</version>
21+
<version>2.1.0</version>
2222
</dependency>
2323
```
2424
Install the package using Gradle:
2525
```bash
26-
implementation group: 'com.veryfi', name: 'veryfi-java', version: '2.0.0'
26+
implementation group: 'com.veryfi', name: 'veryfi-java', version: '2.1.0'
2727
```
2828

2929
## Getting Started

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.veryfi</groupId>
77
<artifactId>veryfi-java</artifactId>
8-
<version>2.0.0</version>
8+
<version>2.1.0</version>
99
<packaging>jar</packaging>
1010
<name>veryfi-java</name>
1111
<description>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package veryfi;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.Base64;
7+
8+
public class Base64Helper {
9+
10+
public static String getBase64FileContent(String filePath) throws IOException {
11+
String fileName = filePath.replaceAll("^.*[/\\\\]", "");
12+
File file = new File(filePath);
13+
return getBase64FileContent(file);
14+
}
15+
16+
public static String getBase64FileContent(File file) throws IOException {
17+
String fileData = "";
18+
byte[] fileContent = Files.readAllBytes(file.toPath());
19+
fileData = Base64.getEncoder().encodeToString(fileContent);
20+
return getUriPrefix(file) + fileData;
21+
}
22+
23+
public static String getUriPrefix(File file) {
24+
String extension = getFileExtension(file);
25+
if (extension.isEmpty())
26+
extension = "png";
27+
return "data:image/" + extension + ";base64,";
28+
}
29+
30+
protected static String getFileExtension(File file) {
31+
String fileName = file.getName();
32+
int dotIndex = fileName.lastIndexOf('.');
33+
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
34+
return fileName.substring(dotIndex + 1);
35+
} else {
36+
return "";
37+
}
38+
}
39+
40+
}

src/main/java/veryfi/Client.java

Lines changed: 360 additions & 12 deletions
Large diffs are not rendered by default.

src/main/java/veryfi/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private Constants() {
2121
/**
2222
* header for HttpRequest
2323
*/
24-
public static final String USER_AGENT_JAVA = "Java Veryfi-Java/2.0.0";
24+
public static final String USER_AGENT_JAVA = "Java Veryfi-Java/2.1.0";
2525
/**
2626
* header for HttpRequest
2727
*/

src/main/java/veryfi/NetworkClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ public void setHttpClient(HttpClient httpClient) {
232232
this.httpClient = httpClient;
233233
}
234234

235+
/**
236+
* Creates the JSON Object for the parameters of the request
237+
*
238+
* @param fileName Name of the file to upload to the Veryfi API
239+
* @param fileData Base64 encoded file data
240+
* @param parameters Additional request parameters
241+
* @return the JSON object of the parameters of the request
242+
*/
243+
protected JSONObject addFileToParameters(String fileName, String fileData, JSONObject parameters) {
244+
if (parameters == null)
245+
parameters = new JSONObject();
246+
parameters.put(FILE_NAME, fileName);
247+
parameters.put(FILE_DATA, fileData);
248+
return parameters;
249+
}
250+
235251
/**
236252
* Creates the JSON Object for the parameters of the request
237253
*
@@ -242,18 +258,13 @@ public void setHttpClient(HttpClient httpClient) {
242258
protected JSONObject addFileToParameters(String filePath, JSONObject parameters) {
243259
String fileName = filePath.replaceAll("^.*[/\\\\]", "");
244260
File file = new File(filePath);
245-
String base64EncodedString = "";
261+
String fileData = "";
246262
try {
247-
byte[] fileContent = Files.readAllBytes(file.toPath());
248-
base64EncodedString = Base64.getEncoder().encodeToString(fileContent);
263+
fileData = Base64Helper.getBase64FileContent(file);
249264
} catch (Exception e) {
250265
logger.severe("addFileToParameters: " + e.getMessage());
251266
}
252-
if (parameters == null)
253-
parameters = new JSONObject();
254-
parameters.put(FILE_NAME, fileName);
255-
parameters.put(FILE_DATA, base64EncodedString);
256-
return parameters;
267+
return addFileToParameters(fileName, fileData, parameters);
257268
}
258269

259270
/**

src/main/java/veryfi/enums/Endpoint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public enum Endpoint {
99
w2s("/partner/w2s/"),
1010
w9s("/partner/w9s/"),
1111
w8BenE("/partner/w-8ben-e/"),
12-
contracts("/partner/contracts/");
12+
contracts("/partner/contracts/"),
13+
classify("/partner/classify/"),
14+
split("/partner/documents-set/");
1315

1416
public final String path;
1517

src/main/java/veryfi/services/AnyDocumentServices.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ protected CompletableFuture<String> processAnyDocumentAsync(String filePath, Str
130130
return requestAsync(HttpMethod.POST, Endpoint.anyDocuments.path, parameters);
131131
}
132132

133+
/**
134+
* Process a Any Document and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/
135+
*
136+
* @param fileName Name of the file to upload to the Veryfi API
137+
* @param fileData Base64 encoded file data
138+
* @param blueprintName The name of the extraction blueprints.
139+
* @param parameters Additional request parameters.
140+
* @return the data extracted from the Any Document {@link String}
141+
*/
142+
protected String processAnyDocument(String fileName, String fileData, String blueprintName, JSONObject parameters) {
143+
parameters = addFileToParameters(fileName, fileData, parameters);
144+
parameters.put("blueprint_name", blueprintName);
145+
return request(HttpMethod.POST, Endpoint.anyDocuments.path, parameters);
146+
}
147+
148+
/**
149+
* Process a Any Document and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/
150+
*
151+
* @param fileName Name of the file to upload to the Veryfi API
152+
* @param fileData Base64 encoded file data
153+
* @param blueprintName The name of the extraction blueprints.
154+
* @param parameters Additional request parameters.
155+
* @return the data extracted from the Any Document {@link CompletableFuture<String>}
156+
*/
157+
protected CompletableFuture<String> processAnyDocumentAsync(String fileName, String fileData, String blueprintName, JSONObject parameters) {
158+
parameters = addFileToParameters(fileName, fileData, parameters);
159+
parameters.put("blueprint_name", blueprintName);
160+
return requestAsync(HttpMethod.POST, Endpoint.anyDocuments.path, parameters);
161+
}
162+
133163
/**
134164
* Process Any Document from url and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/
135165
*

src/main/java/veryfi/services/BankStatementServices.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected CompletableFuture<String> getBankStatementAsync(String documentId) {
106106
* Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/
107107
*
108108
* @param filePath Path on disk to a file to submit for data extraction.
109-
* @param parameters Additional request parameters.
109+
* @param parameters Additional request parameters.
110110
* @return the data extracted from the Bank Statement {@link String}
111111
*/
112112
protected String processBankStatement(String filePath, JSONObject parameters) {
@@ -126,6 +126,32 @@ protected CompletableFuture<String> processBankStatementAsync(String filePath, J
126126
return requestAsync(HttpMethod.POST, Endpoint.bankStatements.path, parameters);
127127
}
128128

129+
/**
130+
* Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/
131+
*
132+
* @param fileName Name of the file to upload to the Veryfi API
133+
* @param fileData Base64 encoded file data
134+
* @param parameters Additional request parameters
135+
* @return the data extracted from the Bank Statement {@link String}
136+
*/
137+
protected String processBankStatement(String fileName, String fileData, JSONObject parameters) {
138+
parameters = addFileToParameters(fileName, fileData, parameters);
139+
return request(HttpMethod.POST, Endpoint.bankStatements.path, parameters);
140+
}
141+
142+
/**
143+
* Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/
144+
*
145+
* @param fileName Name of the file to upload to the Veryfi API
146+
* @param fileData Base64 encoded file data
147+
* @param parameters Additional request parameters
148+
* @return the data extracted from the Bank Statement {@link CompletableFuture<String>}
149+
*/
150+
protected CompletableFuture<String> processBankStatementAsync(String fileName, String fileData, JSONObject parameters) {
151+
parameters = addFileToParameters(fileName, fileData, parameters);
152+
return requestAsync(HttpMethod.POST, Endpoint.bankStatements.path, parameters);
153+
}
154+
129155
/**
130156
* Process Bank Statement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/
131157
*

src/main/java/veryfi/services/BusinessCardsServices.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected CompletableFuture<String> getBusinessCardAsync(String documentId) {
106106
* Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/
107107
*
108108
* @param filePath Path on disk to a file to submit for data extraction.
109-
* @param parameters Additional request parameters.
109+
* @param parameters Additional request parameters.
110110
* @return the data extracted from the Business Card {@link String}
111111
*/
112112
protected String processBusinessCard(String filePath, JSONObject parameters) {
@@ -126,6 +126,32 @@ protected CompletableFuture<String> processBusinessCardAsync(String filePath, JS
126126
return requestAsync(HttpMethod.POST, Endpoint.businessCards.path, parameters);
127127
}
128128

129+
/**
130+
* Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/
131+
*
132+
* @param fileName Name of the file to upload to the Veryfi API
133+
* @param fileData Base64 encoded file data
134+
* @param parameters Additional request parameters.
135+
* @return the data extracted from the Business Card {@link String}
136+
*/
137+
protected String processBusinessCard(String fileName, String fileData, JSONObject parameters) {
138+
parameters = addFileToParameters(fileName, fileData, parameters);
139+
return request(HttpMethod.POST, Endpoint.businessCards.path, parameters);
140+
}
141+
142+
/**
143+
* Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/
144+
*
145+
* @param fileName Name of the file to upload to the Veryfi API
146+
* @param fileData Base64 encoded file data
147+
* @param parameters Additional request parameters.
148+
* @return the data extracted from the Business Card {@link CompletableFuture<String>}
149+
*/
150+
protected CompletableFuture<String> processBusinessCardAsync(String fileName, String fileData, JSONObject parameters) {
151+
parameters = addFileToParameters(fileName, fileData, parameters);
152+
return requestAsync(HttpMethod.POST, Endpoint.businessCards.path, parameters);
153+
}
154+
129155
/**
130156
* Process Business Card from url and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/
131157
*

0 commit comments

Comments
 (0)