diff --git a/README.md b/README.md index a9c5bf1..1512a88 100755 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ Install the package using Maven: com.veryfi veryfi-java - 2.0.0 + 2.1.0 ``` Install the package using Gradle: ```bash -implementation group: 'com.veryfi', name: 'veryfi-java', version: '2.0.0' +implementation group: 'com.veryfi', name: 'veryfi-java', version: '2.1.0' ``` ## Getting Started diff --git a/pom.xml b/pom.xml index d7e9d85..0e09a25 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.veryfi veryfi-java - 2.0.0 + 2.1.0 jar veryfi-java diff --git a/src/main/java/veryfi/Base64Helper.java b/src/main/java/veryfi/Base64Helper.java new file mode 100644 index 0000000..1eb506b --- /dev/null +++ b/src/main/java/veryfi/Base64Helper.java @@ -0,0 +1,40 @@ +package veryfi; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Base64; + +public class Base64Helper { + + public static String getBase64FileContent(String filePath) throws IOException { + String fileName = filePath.replaceAll("^.*[/\\\\]", ""); + File file = new File(filePath); + return getBase64FileContent(file); + } + + public static String getBase64FileContent(File file) throws IOException { + String fileData = ""; + byte[] fileContent = Files.readAllBytes(file.toPath()); + fileData = Base64.getEncoder().encodeToString(fileContent); + return getUriPrefix(file) + fileData; + } + + public static String getUriPrefix(File file) { + String extension = getFileExtension(file); + if (extension.isEmpty()) + extension = "png"; + return "data:image/" + extension + ";base64,"; + } + + protected static String getFileExtension(File file) { + String fileName = file.getName(); + int dotIndex = fileName.lastIndexOf('.'); + if (dotIndex > 0 && dotIndex < fileName.length() - 1) { + return fileName.substring(dotIndex + 1); + } else { + return ""; + } + } + +} diff --git a/src/main/java/veryfi/Client.java b/src/main/java/veryfi/Client.java index f3b967d..dd82e50 100644 --- a/src/main/java/veryfi/Client.java +++ b/src/main/java/veryfi/Client.java @@ -1,9 +1,13 @@ package veryfi; import org.json.JSONObject; +import veryfi.enums.Endpoint; +import veryfi.enums.HttpMethod; import veryfi.models.AddLineItem; import veryfi.models.NotValidModelException; import veryfi.models.UpdateLineItem; +import veryfi.services.SplitServices; + import java.net.http.HttpClient; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -72,6 +76,32 @@ String processDocument(String filePath, List categories, boolean deleteA CompletableFuture processDocumentAsync(String filePath, List categories, boolean deleteAfterProcessing, JSONObject parameters); + /** + * Process a document and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param categories List of categories Veryfi can use to categorize the document + * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted + * @param parameters Additional request parameters + * @return the data extracted from the Document {@link String} + */ + String processDocument(String fileName, String fileData, List categories, + boolean deleteAfterProcessing, JSONObject parameters); + + /** + * Process a document and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param categories List of categories Veryfi can use to categorize the document + * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted + * @param parameters Additional request parameters + * @return the data extracted from the Document {@link CompletableFuture} + */ + CompletableFuture processDocumentAsync(String fileName, String fileData, List categories, + boolean deleteAfterProcessing, JSONObject parameters); + /** * Process Document from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". @@ -325,6 +355,28 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processAnyDocumentAsync(String filePath, String blueprintName, JSONObject parameters); + /** + * Process a AnyDocument and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the AnyDocument {@link String} + */ + String processAnyDocument(String fileName, String fileData, String blueprintName, JSONObject parameters); + + /** + * Process a AnyDocument and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the AnyDocument {@link CompletableFuture} + */ + CompletableFuture processAnyDocumentAsync(String fileName, String fileData, String blueprintName, JSONObject parameters); + /** * Process AnyDocument from url and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ * @@ -404,55 +456,75 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f CompletableFuture getBankStatementAsync(String documentId); /** - * Process a BankStatement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param filePath Path on disk to a file to submit for data extraction. * @param parameters Additional request parameters. - * @return the data extracted from the BankStatement {@link String} + * @return the data extracted from the Bank Statement {@link String} */ String processBankStatement(String filePath, JSONObject parameters); /** - * Process a BankStatement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param filePath Path on disk to a file to submit for data extraction. * @param parameters Additional request parameters. - * @return the data extracted from the BankStatement {@link CompletableFuture} + * @return the data extracted from the Bank Statement {@link CompletableFuture} */ CompletableFuture processBankStatementAsync(String filePath, JSONObject parameters); /** - * Process BankStatement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link String} + */ + String processBankStatement(String fileName, String fileData, JSONObject parameters); + + /** + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link CompletableFuture} + */ + CompletableFuture processBankStatementAsync(String fileName, String fileData, JSONObject parameters); + + /** + * Process Bank Statement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] * @param parameters Additional request parameters - * @return the data extracted from the BankStatement {@link String} + * @return the data extracted from the Bank Statement {@link String} */ String processBankStatementUrl(String fileUrl, List fileUrls, JSONObject parameters); /** - * Process BankStatement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process Bank Statement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] * @param parameters Additional request parameters - * @return the data extracted from the BankStatement {@link CompletableFuture} + * @return the data extracted from the Bank Statement {@link CompletableFuture} */ CompletableFuture processBankStatementUrlAsync(String fileUrl, List fileUrls, JSONObject parameters); /** - * Delete BankStatement from Veryfi. https://docs.veryfi.com/api/bank-statements/delete-a-bank-statement/ + * Delete Bank Statement from Veryfi. https://docs.veryfi.com/api/bank-statements/delete-a-bank-statement/ * - * @param documentId ID of the BankStatement you'd like to delete. + * @param documentId ID of the Bank Statement you'd like to delete. * @return the response data. {@link String} */ String deleteBankStatement(String documentId); /** - * Delete BankStatement from Veryfi. https://docs.veryfi.com/api/bank-statements/delete-a-bank-statement/ + * Delete Bank Statement from Veryfi. https://docs.veryfi.com/api/bank-statements/delete-a-bank-statement/ * - * @param documentId ID of the BankStatement you'd like to delete. + * @param documentId ID of the Bank Statement you'd like to delete. * @return the response data. {@link CompletableFuture} */ CompletableFuture deleteBankStatementAsync(String documentId); @@ -515,6 +587,26 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processBusinessCardAsync(String filePath, JSONObject parameters); + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link String} + */ + String processBusinessCard(String fileName, String fileData, JSONObject parameters); + + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link CompletableFuture} + */ + CompletableFuture processBusinessCardAsync(String fileName, String fileData, JSONObject parameters); + /** * Process Business Card from url and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ * @@ -609,6 +701,26 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processCheckAsync(String filePath, JSONObject parameters); + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link String} + */ + String processCheck(String fileName, String fileData, JSONObject parameters); + + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link CompletableFuture} + */ + CompletableFuture processCheckAsync(String fileName, String fileData, JSONObject parameters); + /** * Process Check from url and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ * @@ -703,6 +815,26 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processW2Async(String filePath, JSONObject parameters); + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link String} + */ + String processW2(String fileName, String fileData, JSONObject parameters); + + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link CompletableFuture} + */ + CompletableFuture processW2Async(String fileName, String fileData, JSONObject parameters); + /** * Process W2 from url and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ * @@ -891,6 +1023,26 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processW9Async(String filePath, JSONObject parameters); + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link String} + */ + String processW9(String fileName, String fileData, JSONObject parameters); + + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link CompletableFuture} + */ + CompletableFuture processW9Async(String fileName, String fileData, JSONObject parameters); + /** * Process W9 from url and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ * @@ -981,6 +1133,46 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture processContractAsync(String filePath, JSONObject parameters); + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link String} + */ + String processContract(String fileName, String fileData, JSONObject parameters); + + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link CompletableFuture} + */ + CompletableFuture processContractAsync(String fileName, String fileData, JSONObject parameters); + + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link String} + */ + String processW8BenE(String fileName, String fileData, JSONObject parameters); + + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link CompletableFuture} + */ + CompletableFuture processW8BenEAsync(String fileName, String fileData, JSONObject parameters); + /** * Process Contract from url and extract all the fields from it. * @@ -1015,4 +1207,160 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture deleteContractAsync(String documentId); + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String classifyDocument(String filePath, JSONObject parameters); + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture classifyDocumentAsync(String filePath, JSONObject parameters); + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String classifyDocument(String fileName, String fileData, JSONObject parameters); + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture classifyDocumentAsync(String fileName, String fileData, JSONObject parameters); + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String classifyDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters); + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture classifyDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String splitDocument(String filePath, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture } + */ + CompletableFuture splitDocumentAsync(String filePath, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String splitDocument(String fileName, String fileData, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture splitDocumentAsync(String fileName, String fileData, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + String splitDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters); + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture splitDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters); + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + String getSplitDocuments(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters); + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + CompletableFuture getSplitDocumentsAsync(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters); + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link String} + */ + String getSplitDocument(String documentId); + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link CompletableFuture} + */ + CompletableFuture getSplitDocumentAsync(String documentId); + } diff --git a/src/main/java/veryfi/Constants.java b/src/main/java/veryfi/Constants.java index 902a3f7..b143493 100644 --- a/src/main/java/veryfi/Constants.java +++ b/src/main/java/veryfi/Constants.java @@ -21,7 +21,7 @@ private Constants() { /** * header for HttpRequest */ - public static final String USER_AGENT_JAVA = "Java Veryfi-Java/2.0.0"; + public static final String USER_AGENT_JAVA = "Java Veryfi-Java/2.1.0"; /** * header for HttpRequest */ diff --git a/src/main/java/veryfi/NetworkClient.java b/src/main/java/veryfi/NetworkClient.java index 963e00c..c517efb 100644 --- a/src/main/java/veryfi/NetworkClient.java +++ b/src/main/java/veryfi/NetworkClient.java @@ -232,6 +232,22 @@ public void setHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } + /** + * Creates the JSON Object for the parameters of the request + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the JSON object of the parameters of the request + */ + protected JSONObject addFileToParameters(String fileName, String fileData, JSONObject parameters) { + if (parameters == null) + parameters = new JSONObject(); + parameters.put(FILE_NAME, fileName); + parameters.put(FILE_DATA, fileData); + return parameters; + } + /** * Creates the JSON Object for the parameters of the request * @@ -242,18 +258,13 @@ public void setHttpClient(HttpClient httpClient) { protected JSONObject addFileToParameters(String filePath, JSONObject parameters) { String fileName = filePath.replaceAll("^.*[/\\\\]", ""); File file = new File(filePath); - String base64EncodedString = ""; + String fileData = ""; try { - byte[] fileContent = Files.readAllBytes(file.toPath()); - base64EncodedString = Base64.getEncoder().encodeToString(fileContent); + fileData = Base64Helper.getBase64FileContent(file); } catch (Exception e) { logger.severe("addFileToParameters: " + e.getMessage()); } - if (parameters == null) - parameters = new JSONObject(); - parameters.put(FILE_NAME, fileName); - parameters.put(FILE_DATA, base64EncodedString); - return parameters; + return addFileToParameters(fileName, fileData, parameters); } /** diff --git a/src/main/java/veryfi/enums/Endpoint.java b/src/main/java/veryfi/enums/Endpoint.java index 2fc3264..87f391b 100644 --- a/src/main/java/veryfi/enums/Endpoint.java +++ b/src/main/java/veryfi/enums/Endpoint.java @@ -9,7 +9,9 @@ public enum Endpoint { w2s("/partner/w2s/"), w9s("/partner/w9s/"), w8BenE("/partner/w-8ben-e/"), - contracts("/partner/contracts/"); + contracts("/partner/contracts/"), + classify("/partner/classify/"), + split("/partner/documents-set/"); public final String path; diff --git a/src/main/java/veryfi/services/AnyDocumentServices.java b/src/main/java/veryfi/services/AnyDocumentServices.java index f315899..7e9f2c0 100644 --- a/src/main/java/veryfi/services/AnyDocumentServices.java +++ b/src/main/java/veryfi/services/AnyDocumentServices.java @@ -130,6 +130,36 @@ protected CompletableFuture processAnyDocumentAsync(String filePath, Str return requestAsync(HttpMethod.POST, Endpoint.anyDocuments.path, parameters); } + /** + * Process a Any Document and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the Any Document {@link String} + */ + protected String processAnyDocument(String fileName, String fileData, String blueprintName, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + parameters.put("blueprint_name", blueprintName); + return request(HttpMethod.POST, Endpoint.anyDocuments.path, parameters); + } + + /** + * Process a Any Document and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the Any Document {@link CompletableFuture} + */ + protected CompletableFuture processAnyDocumentAsync(String fileName, String fileData, String blueprintName, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + parameters.put("blueprint_name", blueprintName); + return requestAsync(HttpMethod.POST, Endpoint.anyDocuments.path, parameters); + } + /** * Process Any Document from url and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ * diff --git a/src/main/java/veryfi/services/BankStatementServices.java b/src/main/java/veryfi/services/BankStatementServices.java index 8085ccd..148cd2b 100644 --- a/src/main/java/veryfi/services/BankStatementServices.java +++ b/src/main/java/veryfi/services/BankStatementServices.java @@ -106,7 +106,7 @@ protected CompletableFuture getBankStatementAsync(String documentId) { * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the Bank Statement {@link String} */ protected String processBankStatement(String filePath, JSONObject parameters) { @@ -126,6 +126,32 @@ protected CompletableFuture processBankStatementAsync(String filePath, J return requestAsync(HttpMethod.POST, Endpoint.bankStatements.path, parameters); } + /** + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link String} + */ + protected String processBankStatement(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.bankStatements.path, parameters); + } + + /** + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link CompletableFuture} + */ + protected CompletableFuture processBankStatementAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.bankStatements.path, parameters); + } + /** * Process Bank Statement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * diff --git a/src/main/java/veryfi/services/BusinessCardsServices.java b/src/main/java/veryfi/services/BusinessCardsServices.java index 741e164..d827dcd 100644 --- a/src/main/java/veryfi/services/BusinessCardsServices.java +++ b/src/main/java/veryfi/services/BusinessCardsServices.java @@ -106,7 +106,7 @@ protected CompletableFuture getBusinessCardAsync(String documentId) { * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the Business Card {@link String} */ protected String processBusinessCard(String filePath, JSONObject parameters) { @@ -126,6 +126,32 @@ protected CompletableFuture processBusinessCardAsync(String filePath, JS return requestAsync(HttpMethod.POST, Endpoint.businessCards.path, parameters); } + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link String} + */ + protected String processBusinessCard(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.businessCards.path, parameters); + } + + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link CompletableFuture} + */ + protected CompletableFuture processBusinessCardAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.businessCards.path, parameters); + } + /** * Process Business Card from url and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ * diff --git a/src/main/java/veryfi/services/CheckServices.java b/src/main/java/veryfi/services/CheckServices.java index 3b50fe9..1490113 100644 --- a/src/main/java/veryfi/services/CheckServices.java +++ b/src/main/java/veryfi/services/CheckServices.java @@ -106,7 +106,7 @@ protected CompletableFuture getCheckAsync(String documentId) { * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the Check {@link String} */ protected String processCheck(String filePath, JSONObject parameters) { @@ -126,6 +126,32 @@ protected CompletableFuture processCheckAsync(String filePath, JSONObjec return requestAsync(HttpMethod.POST, Endpoint.checks.path, parameters); } + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link String} + */ + protected String processCheck(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.checks.path, parameters); + } + + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link CompletableFuture} + */ + protected CompletableFuture processCheckAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.checks.path, parameters); + } + /** * Process Check from url and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ * diff --git a/src/main/java/veryfi/services/ClassifyServices.java b/src/main/java/veryfi/services/ClassifyServices.java new file mode 100644 index 0000000..533f64f --- /dev/null +++ b/src/main/java/veryfi/services/ClassifyServices.java @@ -0,0 +1,115 @@ +package veryfi.services; + +import org.json.JSONObject; +import veryfi.Credentials; +import veryfi.NetworkClient; +import veryfi.enums.Endpoint; +import veryfi.enums.HttpMethod; + +import java.net.http.HttpClient; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * API services for Document Classification + */ +class ClassifyServices extends NetworkClient { + + /** + * Creates an instance of {@link ClassifyServices}. + * + * @param credentials the {@link Credentials} provided by Veryfi. + * @param apiVersion the {@link int} api version to use Veryfi. + */ + protected ClassifyServices(Credentials credentials, int apiVersion) { + super(credentials, apiVersion); + } + + /** + * Creates an instance of {@link ClassifyServices}. + * + * @param credentials the {@link Credentials} provided by Veryfi. + * @param apiVersion the {@link int} api version to use Veryfi. + * @param httpClient {@link HttpClient} for the Veryfi API + */ + protected ClassifyServices(Credentials credentials, int apiVersion, HttpClient httpClient) { + super(credentials, apiVersion, httpClient); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String classifyDocument(String filePath, JSONObject parameters) { + parameters = addFileToParameters(filePath, parameters); + return request(HttpMethod.POST, Endpoint.classify.path, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture classifyDocumentAsync(String filePath, JSONObject parameters) { + parameters = addFileToParameters(filePath, parameters); + return requestAsync(HttpMethod.POST, Endpoint.classify.path, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String classifyDocument(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.classify.path, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture classifyDocumentAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.classify.path, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String classifyDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters) { + parameters = addUrlToParameters(fileUrl, fileUrls, parameters); + return request(HttpMethod.POST, Endpoint.classify.path, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture classifyDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters) { + parameters = addUrlToParameters(fileUrl, fileUrls, parameters); + return requestAsync(HttpMethod.POST, Endpoint.classify.path, parameters); + } + +} diff --git a/src/main/java/veryfi/services/ClientImpl.java b/src/main/java/veryfi/services/ClientImpl.java index 263f00d..6ad95f0 100644 --- a/src/main/java/veryfi/services/ClientImpl.java +++ b/src/main/java/veryfi/services/ClientImpl.java @@ -27,6 +27,8 @@ public class ClientImpl implements Client { private final W9Services w9Services; private final W8BenEServices w8BenEServices; private final ContractServices contractServices; + private final ClassifyServices classifyServices; + private final SplitServices splitServices; /** * Creates an instance of {@link ClientImpl}. @@ -50,6 +52,8 @@ public ClientImpl(String clientId, String clientSecret, String username, String w9Services = new W9Services(credentials, apiVersion); w8BenEServices = new W8BenEServices(credentials, apiVersion); contractServices = new ContractServices(credentials, apiVersion); + classifyServices = new ClassifyServices(credentials, apiVersion); + splitServices = new SplitServices(credentials, apiVersion); } /** @@ -75,6 +79,8 @@ public ClientImpl(String clientId, String clientSecret, String username, String w9Services = new W9Services(credentials, apiVersion, httpClient); w8BenEServices = new W8BenEServices(credentials, apiVersion, httpClient); contractServices = new ContractServices(credentials, apiVersion, httpClient); + classifyServices = new ClassifyServices(credentials, apiVersion, httpClient); + splitServices = new SplitServices(credentials, apiVersion, httpClient); } /** @@ -159,6 +165,38 @@ public CompletableFuture processDocumentAsync(String filePath, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { + return documentServices.processDocument(fileName, fileData, categories, deleteAfterProcessing, parameters); + } + + /** + * Process a document and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param categories List of categories Veryfi can use to categorize the document + * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted + * @param parameters Additional request parameters + * @return the data extracted from the Document {@link CompletableFuture} + */ + @Override + public CompletableFuture processDocumentAsync(String fileName, String fileData, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { + return documentServices.processDocumentAsync(fileName, fileData, categories, deleteAfterProcessing, parameters); + } + /** * Process Document from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ * @@ -178,7 +216,7 @@ public String processDocumentUrl(String fileUrl, List fileUrls, List processDocumentUrlAsync(String fileUrl, List processAnyDocumentAsync(String filePath, String return anyDocumentServices.processAnyDocumentAsync(filePath, blueprintName, parameters); } + /** + * Process a AnyDocument and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the AnyDocument {@link String} + */ + public String processAnyDocument(String fileName, String fileData, String blueprintName, JSONObject parameters) { + return anyDocumentServices.processAnyDocument(fileName, fileData, blueprintName, parameters); + } + + /** + * Process a AnyDocument and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param blueprintName The name of the extraction blueprints. + * @param parameters Additional request parameters. + * @return the data extracted from the AnyDocument {@link CompletableFuture} + */ + public CompletableFuture processAnyDocumentAsync(String fileName, String fileData, String blueprintName, JSONObject parameters) { + return anyDocumentServices.processAnyDocumentAsync(fileName, fileData, blueprintName, parameters); + } + /** * Process AnyDocument from url and extract all the fields from it. https://docs.veryfi.com/api/anydocs/process-A-doc/ * @@ -606,27 +670,51 @@ public CompletableFuture getBankStatementAsync(String documentId) { } /** - * Process a BankStatement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param filePath Path on disk to a file to submit for data extraction. * @param parameters Additional request parameters. - * @return the data extracted from the BankStatement {@link String} + * @return the data extracted from the Bank Statement {@link String} */ public String processBankStatement(String filePath, JSONObject parameters) { return bankStatementServices.processBankStatement(filePath, parameters); } /** - * Process a BankStatement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * * @param filePath Path on disk to a file to submit for data extraction. * @param parameters Additional request parameters. - * @return the data extracted from the BankStatement {@link CompletableFuture} + * @return the data extracted from the Bank Statement {@link CompletableFuture} */ public CompletableFuture processBankStatementAsync(String filePath, JSONObject parameters) { return bankStatementServices.processBankStatementAsync(filePath, parameters); } + /** + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link String} + */ + public String processBankStatement(String fileName, String fileData, JSONObject parameters) { + return bankStatementServices.processBankStatement(fileName, fileData, parameters); + } + + /** + * Process a Bank Statement and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Bank Statement {@link CompletableFuture} + */ + public CompletableFuture processBankStatementAsync(String fileName, String fileData, JSONObject parameters) { + return bankStatementServices.processBankStatementAsync(fileName, fileData, parameters); + } + /** * Process BankStatement from url and extract all the fields from it. https://docs.veryfi.com/api/bank-statements/process-a-bank-statement/ * @@ -741,6 +829,30 @@ public CompletableFuture processBusinessCardAsync(String filePath, JSONO return businessCardsServices.processBusinessCardAsync(filePath, parameters); } + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link String} + */ + public String processBusinessCard(String fileName, String fileData, JSONObject parameters) { + return businessCardsServices.processBusinessCard(fileName, fileData, parameters); + } + + /** + * Process a Business Card and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the Business Card {@link CompletableFuture} + */ + public CompletableFuture processBusinessCardAsync(String fileName, String fileData, JSONObject parameters) { + return businessCardsServices.processBusinessCardAsync(fileName, fileData, parameters); + } + /** * Process Business Card from url and extract all the fields from it. https://docs.veryfi.com/api/business-cards/process-a-business-card/ * @@ -855,6 +967,32 @@ public CompletableFuture processCheckAsync(String filePath, JSONObject p return checkServices.processCheckAsync(filePath, parameters); } + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link String} + */ + @Override + public String processCheck(String fileName, String fileData, JSONObject parameters) { + return checkServices.processCheck(fileName, fileData, parameters); + } + + /** + * Process a Check and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Check {@link CompletableFuture} + */ + @Override + public CompletableFuture processCheckAsync(String fileName, String fileData, JSONObject parameters) { + return checkServices.processCheckAsync(fileName, fileData, parameters); + } + /** * Process Check from url and extract all the fields from it. https://docs.veryfi.com/api/checks/process-a-check/ * @@ -1083,6 +1221,30 @@ public CompletableFuture processW8BenEAsync(String filePath, JSONObject return w8BenEServices.processW8BenEAsync(filePath, parameters); } + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link String} + */ + public String processW8BenE(String fileName, String fileData, JSONObject parameters) { + return w8BenEServices.processW8BenE(fileName, fileData, parameters); + } + + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link CompletableFuture} + */ + public CompletableFuture processW8BenEAsync(String fileName, String fileData, JSONObject parameters) { + return w8BenEServices.processW8BenEAsync(fileName, fileData, parameters); + } + /** * Process W-8BEN-E from url and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ * @@ -1197,6 +1359,30 @@ public CompletableFuture processW9Async(String filePath, JSONObject para return w9Services.processW9Async(filePath, parameters); } + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link String} + */ + public String processW9(String fileName, String fileData, JSONObject parameters) { + return w9Services.processW9(fileName, fileData, parameters); + } + + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link CompletableFuture} + */ + public CompletableFuture processW9Async(String fileName, String fileData, JSONObject parameters) { + return w9Services.processW9Async(fileName, fileData, parameters); + } + /** * Process W9 from url and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ * @@ -1307,6 +1493,54 @@ public CompletableFuture processContractAsync(String filePath, JSONObjec return contractServices.processContractAsync(filePath, parameters); } + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link String} + */ + public String processContract(String fileName, String fileData, JSONObject parameters) { + return contractServices.processContract(fileName, fileData, parameters); + } + + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link CompletableFuture} + */ + public CompletableFuture processContractAsync(String fileName, String fileData, JSONObject parameters) { + return contractServices.processContractAsync(fileName, fileData, parameters); + } + + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link String} + */ + public String processW2(String fileName, String fileData, JSONObject parameters) { + return w2Services.processW2(fileName, fileData, parameters); + } + + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link CompletableFuture} + */ + public CompletableFuture processW2Async(String fileName, String fileData, JSONObject parameters) { + return w2Services.processW2Async(fileName, fileData, parameters); + } + /** * Process Contract from url and extract all the fields from it. * @@ -1349,4 +1583,210 @@ public CompletableFuture deleteContractAsync(String documentId) { return contractServices.deleteContractAsync(documentId); } + // ClassifyServices methods + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String classifyDocument(String filePath, JSONObject parameters) { + return classifyServices.classifyDocument(filePath, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture classifyDocumentAsync(String filePath, JSONObject parameters) { + return classifyServices.classifyDocumentAsync(filePath, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String classifyDocument(String fileName, String fileData, JSONObject parameters) { + return classifyServices.classifyDocument(fileName, fileData, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture classifyDocumentAsync(String fileName, String fileData, JSONObject parameters) { + return classifyServices.classifyDocumentAsync(fileName, fileData, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String classifyDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters) { + return classifyServices.classifyDocumentUrl(fileUrl, fileUrls, parameters); + } + + /** + * Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture classifyDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters) { + return classifyServices.classifyDocumentUrlAsync(fileUrl, fileUrls, parameters); + } + + // SplitServices methods + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String splitDocument(String filePath, JSONObject parameters) { + return splitServices.splitDocument(filePath, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture } + */ + @Override + public CompletableFuture splitDocumentAsync(String filePath, JSONObject parameters) { + return splitServices.splitDocumentAsync(filePath, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String splitDocument(String fileName, String fileData, JSONObject parameters) { + return splitServices.splitDocument(fileName, fileData, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture splitDocumentAsync(String fileName, String fileData, JSONObject parameters) { + return splitServices.splitDocumentAsync(fileName, fileData, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + @Override + public String splitDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters) { + return splitServices.splitDocumentUrl(fileUrl, fileUrls, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture splitDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters) { + return splitServices.splitDocumentUrlAsync(fileUrl, fileUrls, parameters); + } + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + @Override + public String getSplitDocuments(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters) { + return splitServices.getSplitDocuments(page, pageSize, boundingBoxes, confidenceDetails, parameters); + } + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + @Override + public CompletableFuture getSplitDocumentsAsync(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters) { + return splitServices.getSplitDocumentsAsync(page, pageSize, boundingBoxes, confidenceDetails, parameters); + } + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link String} + */ + @Override + public String getSplitDocument(String documentId) { + return splitServices.getSplitDocument(documentId); + } + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link CompletableFuture} + */ + @Override + public CompletableFuture getSplitDocumentAsync(String documentId) { + return splitServices.getSplitDocumentAsync(documentId); + } + } diff --git a/src/main/java/veryfi/services/ContractServices.java b/src/main/java/veryfi/services/ContractServices.java index d757804..e98e594 100644 --- a/src/main/java/veryfi/services/ContractServices.java +++ b/src/main/java/veryfi/services/ContractServices.java @@ -98,7 +98,7 @@ protected CompletableFuture getContractAsync(String documentId) { * Process a Contract and extract all the fields from it. * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the Contract {@link String} */ protected String processContract(String filePath, JSONObject parameters) { @@ -118,6 +118,32 @@ protected CompletableFuture processContractAsync(String filePath, JSONOb return requestAsync(HttpMethod.POST, Endpoint.contracts.path, parameters); } + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link String} + */ + protected String processContract(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.contracts.path, parameters); + } + + /** + * Process a Contract and extract all the fields from it. + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters + * @return the data extracted from the Contract {@link CompletableFuture} + */ + protected CompletableFuture processContractAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.contracts.path, parameters); + } + /** * Process Contract from url and extract all the fields from it. * diff --git a/src/main/java/veryfi/services/DocumentServices.java b/src/main/java/veryfi/services/DocumentServices.java index 7c35c77..1226dc6 100644 --- a/src/main/java/veryfi/services/DocumentServices.java +++ b/src/main/java/veryfi/services/DocumentServices.java @@ -1,6 +1,7 @@ package veryfi.services; import org.json.JSONObject; +import veryfi.Base64Helper; import veryfi.Credentials; import veryfi.NetworkClient; import veryfi.enums.Endpoint; @@ -8,8 +9,6 @@ import java.io.File; import java.net.http.HttpClient; -import java.nio.file.Files; -import java.util.Base64; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -137,6 +136,38 @@ protected CompletableFuture processDocumentAsync(String filePath, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { + JSONObject requestArguments = getProcessDocumentArguments(fileName, fileData, categories, deleteAfterProcessing, parameters); + return request(HttpMethod.POST, Endpoint.documents.path, requestArguments); + } + + /** + * Process a document and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param categories List of categories Veryfi can use to categorize the document + * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted + * @param parameters Additional request parameters + * @return the data extracted from the Document {@link CompletableFuture} + */ + protected CompletableFuture processDocumentAsync(String fileName, String fileData, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { + JSONObject requestArguments = getProcessDocumentArguments(fileName, fileData, categories, deleteAfterProcessing, parameters); + return requestAsync(HttpMethod.POST, Endpoint.documents.path, requestArguments); + } + /** * Process Document from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/process-a-document/ * @@ -231,29 +262,21 @@ protected CompletableFuture updateDocumentAsync(String documentId, JSONO /** * Creates the JSON Object for the parameters of the request * - * @param filePath Path on disk to a file to submit for data extraction + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data * @param categories List of categories Veryfi can use to categorize the document * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted * @param parameters Additional request parameters * @return the JSON object of the parameters of the request */ - private JSONObject getProcessDocumentArguments(String filePath, List categories, - boolean deleteAfterProcessing, JSONObject parameters) { + private JSONObject getProcessDocumentArguments(String fileName, String fileData, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { if (categories == null || categories.isEmpty()) { categories = LIST_CATEGORIES; } - String fileName = filePath.replaceAll("^.*[/\\\\]", ""); - File file = new File(filePath); - String base64EncodedString = ""; - try { - byte[] fileContent = Files.readAllBytes(file.toPath()); - base64EncodedString = Base64.getEncoder().encodeToString(fileContent); - } catch (Exception e) { - logger.severe(e.getMessage()); - } JSONObject requestArguments = new JSONObject(); requestArguments.put(FILE_NAME, fileName); - requestArguments.put(FILE_DATA, base64EncodedString); + requestArguments.put(FILE_DATA, fileData); requestArguments.put(CATEGORIES, categories); requestArguments.put(AUTO_DELETE, deleteAfterProcessing); @@ -265,6 +288,28 @@ private JSONObject getProcessDocumentArguments(String filePath, List cat return requestArguments; } + /** + * Creates the JSON Object for the parameters of the request + * + * @param filePath Path on disk to a file to submit for data extraction + * @param categories List of categories Veryfi can use to categorize the document + * @param deleteAfterProcessing Delete this document from Veryfi after data has been extracted + * @param parameters Additional request parameters + * @return the JSON object of the parameters of the request + */ + private JSONObject getProcessDocumentArguments(String filePath, List categories, + boolean deleteAfterProcessing, JSONObject parameters) { + String fileName = filePath.replaceAll("^.*[/\\\\]", ""); + File file = new File(filePath); + String fileData = ""; + try { + fileData = Base64Helper.getBase64FileContent(file); + } catch (Exception e) { + logger.severe(e.getMessage()); + } + return getProcessDocumentArguments(fileName, fileData, categories, deleteAfterProcessing, parameters); + } + /** * Creates the JSON object of the parameters of the request * diff --git a/src/main/java/veryfi/services/SplitServices.java b/src/main/java/veryfi/services/SplitServices.java new file mode 100644 index 0000000..48d450d --- /dev/null +++ b/src/main/java/veryfi/services/SplitServices.java @@ -0,0 +1,181 @@ +package veryfi.services; + +import org.json.JSONObject; +import veryfi.Credentials; +import veryfi.NetworkClient; +import veryfi.enums.Endpoint; +import veryfi.enums.HttpMethod; + +import java.net.http.HttpClient; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * API services for PDF Split Processing + */ +public class SplitServices extends NetworkClient { + + /** + * Creates an instance of {@link SplitServices}. + * + * @param credentials the {@link Credentials} provided by Veryfi. + * @param apiVersion the {@link int} api version to use Veryfi. + */ + protected SplitServices(Credentials credentials, int apiVersion) { + super(credentials, apiVersion); + } + + /** + * Creates an instance of {@link SplitServices}. + * + * @param credentials the {@link Credentials} provided by Veryfi. + * @param apiVersion the {@link int} api version to use Veryfi. + * @param httpClient {@link HttpClient} for the Veryfi API + */ + protected SplitServices(Credentials credentials, int apiVersion, HttpClient httpClient) { + super(credentials, apiVersion, httpClient); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String splitDocument(String filePath, JSONObject parameters) { + parameters = addFileToParameters(filePath, parameters); + return request(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param filePath Path on disk to a file to submit for data extraction. + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture } + */ + protected CompletableFuture splitDocumentAsync(String filePath, JSONObject parameters) { + parameters = addFileToParameters(filePath, parameters); + return requestAsync(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String splitDocument(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileName Name of the file to upload to the Veryfi API + * @param fileData Base64 encoded file data + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture splitDocumentAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link String} + */ + protected String splitDocumentUrl(String fileUrl, List fileUrls, JSONObject parameters) { + parameters = addUrlToParameters(fileUrl, fileUrls, parameters); + return request(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/ + * + * @param fileUrl Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg". + * @param fileUrls Required if file_url isn't specifies. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"] + * @param parameters Additional request parameters. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture splitDocumentUrlAsync(String fileUrl, List fileUrls, JSONObject parameters) { + parameters = addUrlToParameters(fileUrl, fileUrls, parameters); + return requestAsync(HttpMethod.POST, Endpoint.split.path, parameters); + } + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + protected String getSplitDocuments(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters) { + if (parameters == null) + parameters = new JSONObject(); + parameters.put("page", page); + parameters.put("page_size", pageSize); + parameters.put("bounding_boxes", boundingBoxes); + parameters.put("confidence_details", confidenceDetails); + return request(HttpMethod.GET, Endpoint.split.path, parameters); + } + + /** + * Veryfi's Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/ + * + * @param page The page number. The response is capped to maximum of 50 results per page. + * @param pageSize The number of Documents per page. + * @param boundingBoxes A field used to determine whether or not to return bounding_box and bounding_region for extracted fields in the Document response. + * @param confidenceDetails A field used to determine whether or not to return the score and ocr_score fields in the Document response. + * @param parameters Additional request parameters. + * @return JSON object of previously processed documents {@link String} + */ + protected CompletableFuture getSplitDocumentsAsync(int page, int pageSize, boolean boundingBoxes, boolean confidenceDetails, JSONObject parameters) { + if (parameters == null) + parameters = new JSONObject(); + parameters.put("page", page); + parameters.put("page_size", pageSize); + parameters.put("bounding_boxes", boundingBoxes); + parameters.put("confidence_details", confidenceDetails); + return requestAsync(HttpMethod.GET, Endpoint.split.path, parameters); + } + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link String} + */ + protected String getSplitDocument(String documentId) { + String endpointName = Endpoint.split.path + documentId + "/"; + JSONObject parameters = new JSONObject(); + parameters.put("id", documentId); + return request(HttpMethod.GET, endpointName, parameters); + } + + /** + * Veryfi's Get a Documents from PDF endpoint allows you to retrieve a collection of previously processed documents. https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/ + * + * @param documentId ID of the document you'd like to retrieve. + * @return the data extracted from the document {@link CompletableFuture} + */ + protected CompletableFuture getSplitDocumentAsync(String documentId) { + String endpointName = Endpoint.split.path + documentId + "/"; + JSONObject parameters = new JSONObject(); + parameters.put("id", documentId); + return requestAsync(HttpMethod.GET, endpointName, parameters); + } + +} diff --git a/src/main/java/veryfi/services/W2Services.java b/src/main/java/veryfi/services/W2Services.java index 6e4ddcd..c491972 100644 --- a/src/main/java/veryfi/services/W2Services.java +++ b/src/main/java/veryfi/services/W2Services.java @@ -106,7 +106,7 @@ protected CompletableFuture getW2Async(String documentId) { * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the W2 {@link String} */ protected String processW2(String filePath, JSONObject parameters) { @@ -126,6 +126,32 @@ protected CompletableFuture processW2Async(String filePath, JSONObject p return requestAsync(HttpMethod.POST, Endpoint.w2s.path, parameters); } + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link String} + */ + protected String processW2(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.w2s.path, parameters); + } + + /** + * Process a W2 and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W2 {@link CompletableFuture} + */ + protected CompletableFuture processW2Async(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.w2s.path, parameters); + } + /** * Process W2 from url and extract all the fields from it. https://docs.veryfi.com/api/w2s/process-a-w-2/ * diff --git a/src/main/java/veryfi/services/W8BenEServices.java b/src/main/java/veryfi/services/W8BenEServices.java index 8c4edc3..97319e8 100644 --- a/src/main/java/veryfi/services/W8BenEServices.java +++ b/src/main/java/veryfi/services/W8BenEServices.java @@ -106,7 +106,7 @@ protected CompletableFuture getW8BenEAsync(String documentId) { * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ * * @param filePath Path on disk to a file to submit for data extraction. - * @param parameters Additional request parameters. + * @param parameters Additional request parameters. * @return the data extracted from the W-8BEN-E {@link String} */ protected String processW8BenE(String filePath, JSONObject parameters) { @@ -126,6 +126,32 @@ protected CompletableFuture processW8BenEAsync(String filePath, JSONObje return requestAsync(HttpMethod.POST, Endpoint.w8BenE.path, parameters); } + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link String} + */ + protected String processW8BenE(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.w8BenE.path, parameters); + } + + /** + * Process a W-8BEN-E and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W-8BEN-E {@link CompletableFuture} + */ + protected CompletableFuture processW8BenEAsync(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.w8BenE.path, parameters); + } + /** * Process W-8BEN-E from url and extract all the fields from it. https://docs.veryfi.com/api/w-8ben-e/process-a-w-8-ben-e/ * diff --git a/src/main/java/veryfi/services/W9Services.java b/src/main/java/veryfi/services/W9Services.java index 1f9a2d8..97eea2a 100644 --- a/src/main/java/veryfi/services/W9Services.java +++ b/src/main/java/veryfi/services/W9Services.java @@ -126,6 +126,32 @@ protected CompletableFuture processW9Async(String filePath, JSONObject p return requestAsync(HttpMethod.POST, Endpoint.w9s.path, parameters); } + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link String} + */ + protected String processW9(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return request(HttpMethod.POST, Endpoint.w9s.path, parameters); + } + + /** + * Process a W9 and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ + * + * @param fileName Name of the file to submit for data extraction. + * @param fileData Base64 encoded file data. + * @param parameters Additional request parameters. + * @return the data extracted from the W9 {@link CompletableFuture} + */ + protected CompletableFuture processW9Async(String fileName, String fileData, JSONObject parameters) { + parameters = addFileToParameters(fileName, fileData, parameters); + return requestAsync(HttpMethod.POST, Endpoint.w9s.path, parameters); + } + /** * Process W9 from url and extract all the fields from it. https://docs.veryfi.com/api/w9s/process-a-w-9/ * diff --git a/src/test/java/AnyDocumentTests.java b/src/test/java/AnyDocumentTests.java index 9e87f81..3013142 100644 --- a/src/test/java/AnyDocumentTests.java +++ b/src/test/java/AnyDocumentTests.java @@ -88,6 +88,22 @@ void processAnyDocumentTest() throws IOException, InterruptedException { Assertions.assertEquals(4559535, anyDocument.getJSONObject("data").getInt("id")); } + @Test + void processAnyDocumentBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("anyDocuments/processAnyDocument.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processAnyDocument(getFileName(), getFileData(), "us_driver_license", null); + JSONObject anyDocument = new JSONObject(jsonResponse); + Assertions.assertEquals(4559535, anyDocument.getJSONObject("data").getInt("id")); + } + @Test void processAnyDocumentUrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -176,6 +192,24 @@ void processAnyDocumentAsyncTest() throws ExecutionException, InterruptedExcepti Assertions.assertEquals(4559535, anyDocument.getJSONObject("data").getInt("id")); } + @Test + void processAnyDocumentBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("anyDocuments/processAnyDocument.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processAnyDocumentAsync(getFileName(), getFileData(), "us_driver_license", null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject anyDocument = new JSONObject(jsonResponse); + Assertions.assertEquals(4559535, anyDocument.getJSONObject("data").getInt("id")); + } + @Test void processAnyDocumentUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -213,8 +247,16 @@ void processAnyDocumentUrlAsyncTestWithFileUrlsAndParameters() throws ExecutionE Assertions.assertEquals(4559535, anyDocument.getJSONObject("data").getInt("id")); } - private String getFilePath() { - return ClassLoader.getSystemResource("anyDocuments/driver_license.png").getPath(); + return FileHelper.getFilePath("anyDocuments/driver_license.png"); } + + private String getFileName() { + return FileHelper.getFileName("anyDocuments/driver_license.png"); + } + + private String getFileData() { + return FileHelper.getFileData("anyDocuments/driver_license.png"); + } + } diff --git a/src/test/java/BankStatementTests.java b/src/test/java/BankStatementTests.java index 1664b39..52d0e23 100644 --- a/src/test/java/BankStatementTests.java +++ b/src/test/java/BankStatementTests.java @@ -88,6 +88,22 @@ void processBankStatementTest() throws IOException, InterruptedException { Assertions.assertEquals(4559568, bankStatement.getJSONObject("data").getInt("id")); } + @Test + void processBankStatementBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("bankStatements/processBankStatement.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processBankStatement(getFileName(), getFileData(), null); + JSONObject bankStatement = new JSONObject(jsonResponse); + Assertions.assertEquals(4559568, bankStatement.getJSONObject("data").getInt("id")); + } + @Test void processBankStatementUrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -176,6 +192,24 @@ void processBankStatementAsyncTest() throws ExecutionException, InterruptedExcep Assertions.assertEquals(4559568, bankStatement.getJSONObject("data").getInt("id")); } + @Test + void processBankStatementBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("bankStatements/processBankStatement.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processBankStatementAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject bankStatement = new JSONObject(jsonResponse); + Assertions.assertEquals(4559568, bankStatement.getJSONObject("data").getInt("id")); + } + @Test void processBankStatementUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -213,8 +247,16 @@ void processBankStatementUrlAsyncTestWithFileUrlsAndParameters() throws Executio Assertions.assertEquals(4559568, bankStatement.getJSONObject("data").getInt("id")); } - private String getFilePath() { - return ClassLoader.getSystemResource("bankStatements/bankstatement.pdf").getPath(); + return FileHelper.getFilePath("bankStatements/bankstatement.pdf"); } + + private String getFileName() { + return FileHelper.getFileName("bankStatements/bankstatement.pdf"); + } + + private String getFileData() { + return FileHelper.getFileData("bankStatements/bankstatement.pdf"); + } + } diff --git a/src/test/java/BusinessCardTests.java b/src/test/java/BusinessCardTests.java index f475bad..af20c1e 100644 --- a/src/test/java/BusinessCardTests.java +++ b/src/test/java/BusinessCardTests.java @@ -213,8 +213,49 @@ void processBusinessCardUrlAsyncTestWithFileUrlsAndParameters() throws Execution Assertions.assertEquals(4662609, businessCard.getJSONObject("data").getInt("id")); } + @Test + void processBusinessCardBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("businessCards/processBusinessCard.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processBusinessCard(getFileName(), getFileData(), null); + JSONObject businessCard = new JSONObject(jsonResponse); + Assertions.assertEquals(4662609, businessCard.getJSONObject("data").getInt("id")); + } + + @Test + void processBusinessCardBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("businessCards/processBusinessCard.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processBusinessCardAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject businessCard = new JSONObject(jsonResponse); + Assertions.assertEquals(4662609, businessCard.getJSONObject("data").getInt("id")); + } private String getFilePath() { - return ClassLoader.getSystemResource("businessCards/business_card.jpg").getPath(); + return FileHelper.getFilePath("businessCards/business_card.jpg"); + } + + private String getFileName() { + return FileHelper.getFileName("businessCards/business_card.jpg"); + } + + private String getFileData() { + return FileHelper.getFileData("businessCards/business_card.jpg"); } } diff --git a/src/test/java/CheckTests.java b/src/test/java/CheckTests.java index 08a2e61..c182e04 100644 --- a/src/test/java/CheckTests.java +++ b/src/test/java/CheckTests.java @@ -86,6 +86,22 @@ void processCheckTest() throws IOException, InterruptedException { Assertions.assertEquals(4662680, check.getJSONObject("data").getInt("id")); } + @Test + void processCheckBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("checks/processCheck.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processCheck(getFileName(), getFileData(), null); + JSONObject check = new JSONObject(jsonResponse); + Assertions.assertEquals(4662680, check.getJSONObject("data").getInt("id")); + } + @Test void processCheckUrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -174,6 +190,24 @@ void processCheckAsyncTest() throws ExecutionException, InterruptedException, IO Assertions.assertEquals(4662680, check.getJSONObject("data").getInt("id")); } + @Test + void processCheckBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("checks/processCheck.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processCheckAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject check = new JSONObject(jsonResponse); + Assertions.assertEquals(4662680, check.getJSONObject("data").getInt("id")); + } + @Test void processCheckUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -211,8 +245,17 @@ void processCheckUrlAsyncTestWithFileUrlsAndParameters() throws ExecutionExcepti Assertions.assertEquals(4662680, check.getJSONObject("data").getInt("id")); } - private String getFilePath() { - return ClassLoader.getSystemResource("checks/check.pdf").getPath(); + return FileHelper.getFilePath("checks/check.pdf"); + } + + private String getFileName() { + return FileHelper.getFileName("checks/check.pdf"); } + + private String getFileData() { + return FileHelper.getFileData("checks/check.pdf"); + } + + } diff --git a/src/test/java/ClassifyTests.java b/src/test/java/ClassifyTests.java new file mode 100644 index 0000000..603dc40 --- /dev/null +++ b/src/test/java/ClassifyTests.java @@ -0,0 +1,190 @@ +import org.json.JSONObject; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import veryfi.VeryfiClientFactory; +import veryfi.services.ClientImpl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ClassifyTests { + String clientId = "your_client_id"; + String clientSecret = "your_client_secret"; + String username = "your_username"; + String apiKey = "your_password"; + int apiVersion = 8; + String receiptUrl = "https://cdn.veryfi.com/receipts/92233902-c94a-491d-a4f9-0d61f9407cd2.pdf"; + HttpClient httpClient; + ClientImpl client; + boolean mockResponses = true; // Change to “false” if you want to test your personal credential + + @BeforeEach + void setup() { + httpClient = mockResponses ? mock(HttpClient.class) : HttpClient.newHttpClient(); + client = (ClientImpl) VeryfiClientFactory.createClient(clientId, clientSecret, username, apiKey, apiVersion, httpClient); + } + + @Test + void classifyDocumentTest() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.classifyDocument(getFilePath(), null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentUrlTest() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.classifyDocumentUrl(receiptUrl, null, null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentUrlTestWithFileUrlsAndParameters() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + JSONObject parameters = new JSONObject(); + String jsonResponse = client.classifyDocumentUrl("", Collections.singletonList(receiptUrl), parameters); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentAsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.classifyDocumentAsync(getFilePath(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.classifyDocumentUrlAsync(receiptUrl, null, null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentUrlAsyncTestWithFileUrlsAndParameters() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + JSONObject parameters = new JSONObject(); + CompletableFuture jsonResponseFuture = client.classifyDocumentUrlAsync("", Collections.singletonList(receiptUrl), parameters); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.classifyDocument(getFileName(), getFileData(), null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + @Test + void classifyDocumentBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("classify/classify.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.classifyDocumentAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document = new JSONObject(jsonResponse); + Assertions.assertEquals("receipt", document.getJSONObject("data").getJSONObject("document_type").getString("value")); + } + + private String getFilePath() { + return FileHelper.getFilePath("classify/receipt.png"); + } + + private String getFileName() { + return FileHelper.getFileName("classify/receipt.png"); + } + + private String getFileData() { + return FileHelper.getFileData("classify/receipt.png"); + } + + +} diff --git a/src/test/java/DocumentTests.java b/src/test/java/DocumentTests.java index d14a493..9d068b4 100644 --- a/src/test/java/DocumentTests.java +++ b/src/test/java/DocumentTests.java @@ -92,7 +92,7 @@ void processDocumentTest() throws IOException, InterruptedException { when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn(result); } - String jsonResponse = client.processDocument(getFilePath(), categories, false, null); + String jsonResponse = client.processDocument(getFileName(), getFileData(), categories, false, null); JSONObject document = new JSONObject(jsonResponse); Assertions.assertEquals("Walgreens", document.getJSONObject("vendor").getString("name")); } @@ -115,6 +115,24 @@ void processDocumentTestWithParameters() throws IOException, InterruptedExceptio Assertions.assertEquals("Walgreens", document.getJSONObject("vendor").getString("name")); } + @Test + void processDocumentBase64Test() throws IOException, InterruptedException { + List categories = Arrays.asList("Advertising & Marketing", "Automotive"); + JSONObject parameters = new JSONObject(); + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("documents/processDocument.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processDocument(getFilePath(), categories, false, parameters); + JSONObject document = new JSONObject(jsonResponse); + Assertions.assertEquals("Walgreens", document.getJSONObject("vendor").getString("name")); + } + @Test void processDocumentTestWithoutCategories() throws IOException, InterruptedException { if (mockResponses) { @@ -257,6 +275,25 @@ void processDocumentAsyncTest() throws ExecutionException, InterruptedException, Assertions.assertEquals("Walgreens", document.getJSONObject("vendor").getString("name")); } + @Test + void processDocumentBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + List categories = Arrays.asList("Advertising & Marketing", "Automotive"); + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("documents/processDocument.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processDocumentAsync(getFileName(), getFileData(), categories, false, null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document = new JSONObject(jsonResponse); + Assertions.assertEquals("Walgreens", document.getJSONObject("vendor").getString("name")); + } + @Test void updateDocumentAsyncTest() throws ExecutionException, InterruptedException, IOException { String documentId = "125344108"; // Change to your document Id @@ -330,6 +367,15 @@ void testBadCredentials() throws IOException { } private String getFilePath() { - return ClassLoader.getSystemResource("documents/receipt.jpeg").getPath(); + return FileHelper.getFilePath("documents/receipt.jpeg"); } + + private String getFileName() { + return FileHelper.getFileName("documents/receipt.jpeg"); + } + + private String getFileData() { + return FileHelper.getFileData("documents/receipt.jpeg"); + } + } diff --git a/src/test/java/FileHelper.java b/src/test/java/FileHelper.java new file mode 100644 index 0000000..0462fff --- /dev/null +++ b/src/test/java/FileHelper.java @@ -0,0 +1,26 @@ +import org.junit.jupiter.api.Assertions; +import veryfi.Base64Helper; +import java.io.File; + +public class FileHelper { + + protected static String getFilePath(String filename) { + return ClassLoader.getSystemResource(filename).getPath(); + } + + protected static String getFileName(String filename) { + return getFilePath(filename).replaceAll("^.*[/\\\\]", ""); + } + + protected static String getFileData(String filename) { + File file = new File(getFilePath(filename)); + String fileData = ""; + try { + fileData = Base64Helper.getBase64FileContent(file); + } catch (Exception e) { + Assertions.fail(e); + } + return fileData; + } + +} diff --git a/src/test/java/SplitTests.java b/src/test/java/SplitTests.java new file mode 100644 index 0000000..c44c2aa --- /dev/null +++ b/src/test/java/SplitTests.java @@ -0,0 +1,260 @@ +import org.json.JSONObject; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import veryfi.VeryfiClientFactory; +import veryfi.services.ClientImpl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SplitTests { + String clientId = "your_client_id"; + String clientSecret = "your_client_secret"; + String username = "your_username"; + String apiKey = "your_password"; + int apiVersion = 8; + String receiptUrl = "https://cdn.veryfi.com/receipts/92233902-c94a-491d-a4f9-0d61f9407cd2.pdf"; + HttpClient httpClient; + ClientImpl client; + boolean mockResponses = true; // Change to “false” if you want to test your personal credential + + @BeforeEach + void setup() { + httpClient = mockResponses ? mock(HttpClient.class) : HttpClient.newHttpClient(); + client = (ClientImpl) VeryfiClientFactory.createClient(clientId, clientSecret, username, apiKey, apiVersion, httpClient); + } + + @Test + void splitDocumentTest() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.splitDocument(getFilePath(), null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentUrlTest() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.splitDocumentUrl(receiptUrl, null, null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentUrlTestWithFileUrlsAndParameters() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + JSONObject parameters = new JSONObject(); + String jsonResponse = client.splitDocumentUrl("", Collections.singletonList(receiptUrl), parameters); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentAsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.splitDocumentAsync(getFilePath(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.splitDocumentUrlAsync(receiptUrl, null, null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentUrlAsyncTestWithFileUrlsAndParameters() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + JSONObject parameters = new JSONObject(); + CompletableFuture jsonResponseFuture = client.splitDocumentUrlAsync("", Collections.singletonList(receiptUrl), parameters); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.splitDocument(getFileName(), getFileData(), null); + JSONObject document =new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void splitDocumentBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/split.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.splitDocumentAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject document = new JSONObject(jsonResponse); + Assertions.assertTrue(document.getJSONObject("data").getInt("id") > 0); + } + + @Test + void getSplitDocumentTest() throws IOException, InterruptedException { + String documentId = "351609"; // Change to your document Id + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/getSplit.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.getSplitDocument(documentId); + JSONObject documents = new JSONObject(jsonResponse); + Assertions.assertTrue(documents.getJSONObject("data").getJSONArray("documents_id").length() > 0); + } + + @Test + void getSplitDocumentAsyncTest() throws ExecutionException, InterruptedException, IOException { + String documentId = "351609"; // Change to your document Id + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/getSplit.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.getSplitDocumentAsync(documentId); + String jsonResponse = jsonResponseFuture.get(); + JSONObject documents = new JSONObject(jsonResponse); + Assertions.assertTrue(documents.getJSONObject("data").getJSONArray("documents_id").length() > 0); + } + + @Test + void getSplitDocumentsTest() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/getSplits.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.getSplitDocuments(1, 50, false, false, null); + JSONObject documents = new JSONObject(jsonResponse); + Assertions.assertTrue(documents.getJSONObject("data").getJSONArray("results").length() > 0); + } + + @Test + void getSplitDocumentsAsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("split/getSplits.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.getSplitDocumentsAsync(1, 50, false, false, null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject documents = new JSONObject(jsonResponse); + Assertions.assertTrue(documents.getJSONObject("data").getJSONArray("results").length() > 0); + } + + private String getFilePath() { + return FileHelper.getFilePath("split/split.pdf"); + } + + private String getFileName() { + return FileHelper.getFileName("split/split.pdf"); + } + + private String getFileData() { + return FileHelper.getFileData("split/split.pdf"); + } + + +} diff --git a/src/test/java/W2Tests.java b/src/test/java/W2Tests.java index 88f9fdb..f5a8226 100644 --- a/src/test/java/W2Tests.java +++ b/src/test/java/W2Tests.java @@ -88,6 +88,22 @@ void processW2Test() throws IOException, InterruptedException { Assertions.assertEquals(4559395, w2s.getJSONObject("data").getInt("id")); } + @Test + void processW2Base64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w2s/processW2.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processW2(getFileName(), getFileData(), null); + JSONObject w2s = new JSONObject(jsonResponse); + Assertions.assertEquals(4559395, w2s.getJSONObject("data").getInt("id")); + } + @Test void processW2UrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -176,6 +192,24 @@ void processW2AsyncTest() throws ExecutionException, InterruptedException, IOExc Assertions.assertEquals(4559395, w2s.getJSONObject("data").getInt("id")); } + @Test + void processW2Base64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w2s/processW2.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processW2Async(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject w2s = new JSONObject(jsonResponse); + Assertions.assertEquals(4559395, w2s.getJSONObject("data").getInt("id")); + } + @Test void processW2UrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -213,8 +247,16 @@ void processW2UrlAsyncTestWithFileUrlsAndParameters() throws ExecutionException, Assertions.assertEquals(4559395, w2s.getJSONObject("data").getInt("id")); } - private String getFilePath() { - return ClassLoader.getSystemResource("w2s/w2.png").getPath(); + return FileHelper.getFilePath("w2s/w2.png"); } + + private String getFileName() { + return FileHelper.getFileName("w2s/w2.png"); + } + + private String getFileData() { + return FileHelper.getFileData("w2s/w2.png"); + } + } diff --git a/src/test/java/W8BenETests.java b/src/test/java/W8BenETests.java index ac206c6..d32c318 100644 --- a/src/test/java/W8BenETests.java +++ b/src/test/java/W8BenETests.java @@ -86,6 +86,22 @@ void processW8BenETest() throws IOException, InterruptedException { Assertions.assertEquals(4662698, w8bene.getJSONObject("data").getInt("id")); } + @Test + void processW8BenEBase64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w8bene/processW8bene.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processW8BenE(getFileName(), getFileData(), null); + JSONObject w8bene = new JSONObject(jsonResponse); + Assertions.assertEquals(4662698, w8bene.getJSONObject("data").getInt("id")); + } + @Test void processW8BenEUrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -174,6 +190,24 @@ void processW8BenEAsyncTest() throws ExecutionException, InterruptedException, I Assertions.assertEquals(4662698, w8bene.getJSONObject("data").getInt("id")); } + @Test + void processW8BenEBase64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w8bene/processW8bene.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processW8BenEAsync(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject w8bene = new JSONObject(jsonResponse); + Assertions.assertEquals(4662698, w8bene.getJSONObject("data").getInt("id")); + } + @Test void processW8BenEUrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -213,6 +247,14 @@ void processW8BenEUrlAsyncTestWithFileUrlsAndParameters() throws ExecutionExcept private String getFilePath() { - return ClassLoader.getSystemResource("w8bene/w8bene.pdf").getPath(); + return FileHelper.getFilePath("w8bene/w8bene.pdf"); + } + + private String getFileName() { + return FileHelper.getFileName("w8bene/w8bene.pdf"); + } + + private String getFileData() { + return FileHelper.getFileData("w8bene/w8bene.pdf"); } } diff --git a/src/test/java/W9Tests.java b/src/test/java/W9Tests.java index 954626c..17c3144 100644 --- a/src/test/java/W9Tests.java +++ b/src/test/java/W9Tests.java @@ -86,6 +86,22 @@ void processW9Test() throws IOException, InterruptedException { Assertions.assertEquals(4662722, w9s.getJSONObject("data").getInt("id")); } + @Test + void processW9Base64Test() throws IOException, InterruptedException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w9s/processW9.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + String jsonResponse = client.processW9(getFileName(), getFileData(), null); + JSONObject w9s = new JSONObject(jsonResponse); + Assertions.assertEquals(4662722, w9s.getJSONObject("data").getInt("id")); + } + @Test void processW9UrlTest() throws IOException, InterruptedException { if (mockResponses) { @@ -174,6 +190,24 @@ void processW9AsyncTest() throws ExecutionException, InterruptedException, IOExc Assertions.assertEquals(4662722, w9s.getJSONObject("data").getInt("id")); } + @Test + void processW9Base64AsyncTest() throws ExecutionException, InterruptedException, IOException { + if (mockResponses) { + InputStream fileStream = ClassLoader.getSystemResourceAsStream("w9s/processW9.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFuture = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFuture); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + } + CompletableFuture jsonResponseFuture = client.processW9Async(getFileName(), getFileData(), null); + String jsonResponse = jsonResponseFuture.get(); + JSONObject w9s = new JSONObject(jsonResponse); + Assertions.assertEquals(4662722, w9s.getJSONObject("data").getInt("id")); + } + @Test void processW9UrlAsyncTest() throws ExecutionException, InterruptedException, IOException { if (mockResponses) { @@ -211,8 +245,15 @@ void processW9UrlAsyncTestWithFileUrlsAndParameters() throws ExecutionException, Assertions.assertEquals(4662722, w9s.getJSONObject("data").getInt("id")); } - private String getFilePath() { - return ClassLoader.getSystemResource("w9s/w9.pdf").getPath(); + return FileHelper.getFilePath("w9s/w9.pdf"); + } + + private String getFileName() { + return FileHelper.getFileName("w9s/w9.pdf"); + } + + private String getFileData() { + return FileHelper.getFileData("w9s/w9.pdf"); } } diff --git a/src/test/resources/classify/classify.json b/src/test/resources/classify/classify.json new file mode 100644 index 0000000..4f8b64e --- /dev/null +++ b/src/test/resources/classify/classify.json @@ -0,0 +1,8 @@ +{ + "data": { + "document_type": { + "value": "receipt", + "score": 0.97 + } + } +} diff --git a/src/test/resources/classify/receipt.png b/src/test/resources/classify/receipt.png new file mode 100644 index 0000000..98e0208 Binary files /dev/null and b/src/test/resources/classify/receipt.png differ diff --git a/src/test/resources/split/getSplit.json b/src/test/resources/split/getSplit.json new file mode 100644 index 0000000..0469346 --- /dev/null +++ b/src/test/resources/split/getSplit.json @@ -0,0 +1,10 @@ +{ + "data": { + "id": 351609, + "documents_id": [ + 337479740, + 337479769 + ], + "status": "processed" + } +} \ No newline at end of file diff --git a/src/test/resources/split/getSplits.json b/src/test/resources/split/getSplits.json new file mode 100644 index 0000000..81fbb9b --- /dev/null +++ b/src/test/resources/split/getSplits.json @@ -0,0 +1,72 @@ +{ + "data": { + "count": 8, + "next": "", + "previous": "", + "results": [ + { + "id": 351628, + "documents_id": [ + 337482927 + ], + "status": "processed" + }, + { + "id": 351623, + "documents_id": [ + 337482669, + 337482689 + ], + "status": "processed" + }, + { + "id": 351622, + "documents_id": [ + 337482309, + 337482338 + ], + "status": "processed" + }, + { + "id": 351620, + "documents_id": [ + 337481974, + 337481997 + ], + "status": "processed" + }, + { + "id": 351619, + "documents_id": [ + 337481287, + 337481307 + ], + "status": "processed" + }, + { + "id": 351609, + "documents_id": [ + 337479740, + 337479769 + ], + "status": "processed" + }, + { + "id": 2098, + "documents_id": [ + 126516809, + 126516810 + ], + "status": "processed" + }, + { + "id": 2085, + "documents_id": [ + 126254495, + 126254497 + ], + "status": "processed" + } + ] + } +} \ No newline at end of file diff --git a/src/test/resources/split/split.json b/src/test/resources/split/split.json new file mode 100644 index 0000000..e628656 --- /dev/null +++ b/src/test/resources/split/split.json @@ -0,0 +1,5 @@ +{ + "data": { + "id": 351609 + } +} \ No newline at end of file diff --git a/src/test/resources/split/split.pdf b/src/test/resources/split/split.pdf new file mode 100644 index 0000000..2ebe3d8 Binary files /dev/null and b/src/test/resources/split/split.pdf differ