Skip to content

Commit 8150cdb

Browse files
authored
Merge pull request #58 from veryfi/feature/LP-1229-Add-classify-api
Add classify and split document API methods
2 parents e3d72a0 + 126a96f commit 8150cdb

17 files changed

Lines changed: 531 additions & 2 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* Classify a document and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/
4+
* @example
5+
* veryfi_client.classify_document_from_base64('base64_encoded_string',
6+
* 'receipt.png',
7+
* {'extra': 'parameters'})
8+
*
9+
* @memberof Client
10+
* @param {String} base64_encoded_string Buffer string of a file to submit for classify extraction
11+
* @param {String} file_name The file name including the extension
12+
* @param {Object} kwargs Additional request parameters
13+
* @returns {JSON} Data extracted from the document
14+
*/
15+
Client.prototype.classify_document_from_base64 = async function (
16+
base64_encoded_string,
17+
file_name,
18+
{...kwargs} = {}
19+
) {
20+
21+
let endpoint_name = "/classify/";
22+
let request_arguments = {
23+
"file_name": file_name,
24+
"file_data": base64_encoded_string,
25+
};
26+
request_arguments = Object.assign(request_arguments, kwargs);
27+
let document = await this._request("POST", endpoint_name, request_arguments, null, false);
28+
return document['data'];
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* Classify document from url and extract all the fields from it. https://docs.veryfi.com/api/classify/classify-a-document/
4+
* @example
5+
* veryfi_client.classify_document_from_url('https://cdn.example.com/receipt.jpg')
6+
*
7+
* @memberof Client
8+
* @param {string|null} file_url Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
9+
* @param {Array} file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"]
10+
* @param {Object} kwargs Additional request parameters
11+
* @return {JSON} Data extracted from the document.
12+
*/
13+
Client.prototype.classify_document_from_url = async function (
14+
file_url = null,
15+
file_urls = null,
16+
{...kwargs} = {},
17+
) {
18+
let endpoint_name = "/classify/";
19+
let request_arguments = {
20+
"file_url": file_url,
21+
"file_urls": file_urls,
22+
};
23+
request_arguments = Object.assign(request_arguments, kwargs);
24+
let response = await this._request("POST", endpoint_name, request_arguments, null, false);
25+
return response['data'];
26+
}

lib/client/client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ require('../checks/processCheck');
2727
require('../checks/processCheckBase64');
2828
require('../checks/processCheckStream');
2929
require('../checks/processCheckUrl');
30+
require('../classify/classifyDocumentBase64');
31+
require('../classify/classifyDocumentUrl');
3032
require('../documents/deleteDocument');
3133
require('../documents/getDocument');
3234
require('../documents/getDocuments');
@@ -39,6 +41,10 @@ require('../documents/tags/addTags');
3941
require('../documents/tags/addTag');
4042
require('../documents/tags/deleteTags');
4143
require('../documents/tags/replaceTags');
44+
require('../split/splitDocumentBase64');
45+
require('../split/splitDocumentUrl');
46+
require('../split/getSplitDocument');
47+
require('../split/getSplitDocuments');
4248
require('../w2s/deleteW2');
4349
require('../w2s/getW2');
4450
require('../w2s/getW2s');

lib/client/getHeaders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Client = require('../client/constructor');
88
*/
99
Client.prototype._get_headers = function (has_files = false) {
1010
let final_headers = {
11-
"User-Agent": "Node.js Veryfi-Nodejs/1.4.5",
11+
"User-Agent": "Node.js Veryfi-Nodejs/1.4.6",
1212
"Accept": "application/json",
1313
"Content-Type": "application/json",
1414
"Client-Id": this.client_id,

lib/split/getSplitDocument.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* 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/
4+
* @memberof Client
5+
* @param {string} document_id ID of the document you'd like to retrieve
6+
* @param {Object} kwargs Additional request parameters
7+
* @returns {JSON} Data extracted from the Document
8+
*/
9+
Client.prototype.get_split_document = async function (document_id, {...kwargs} = {}) {
10+
let endpoint_name = `/documents-set//${document_id}/`;
11+
let request_arguments = {"id": document_id};
12+
let document = await this._request("GET", endpoint_name, request_arguments, kwargs, false);
13+
return document['data'];
14+
}

lib/split/getSplitDocuments.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* 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/
4+
* @memberof Client
5+
* @param {number} page The page number. The response is capped to maximum of 50 results per page.
6+
* @param {number} page_size The number of Documents per page.
7+
* @param {Object} kwargs Additional request parameters
8+
* @returns {JSON} JSON object of previously processed documents
9+
*/
10+
Client.prototype.get_split_documents = async function (
11+
page = 1,
12+
page_size = 50,
13+
{...kwargs} = {}
14+
) {
15+
let endpoint_name = "/documents-set/";
16+
let request_arguments = {
17+
"page": page,
18+
"page_size": page_size
19+
};
20+
let documents = await this._request("GET", endpoint_name, request_arguments, kwargs, false);
21+
if ("data" in documents) {
22+
documents = documents["data"];
23+
}
24+
return documents;
25+
}

lib/split/splitDocumentBase64.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
4+
* @example
5+
* veryfi_client.split_document_from_base64('base64_encoded_string',
6+
* 'receipt.png',
7+
* {'extra': 'parameters'})
8+
*
9+
* @memberof Client
10+
* @param {String} base64_encoded_string Buffer string of a file to submit for classify extraction
11+
* @param {String} file_name The file name including the extension
12+
* @param {Object} kwargs Additional request parameters
13+
* @returns {JSON} Data extracted from the document
14+
*/
15+
Client.prototype.split_document_from_base64 = async function (
16+
base64_encoded_string,
17+
file_name,
18+
{...kwargs} = {}
19+
) {
20+
21+
let endpoint_name = "/documents-set/";
22+
let request_arguments = {
23+
"file_name": file_name,
24+
"file_data": base64_encoded_string,
25+
};
26+
request_arguments = Object.assign(request_arguments, kwargs);
27+
let document = await this._request("POST", endpoint_name, request_arguments, null, false);
28+
return document['data'];
29+
}

lib/split/splitDocumentUrl.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Client = require('../client/constructor');
2+
/**
3+
* Split document PDF from url and extract all the fields from it. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
4+
* @example
5+
* veryfi_client.split_document_from_url('https://cdn.example.com/receipts.pdf')
6+
*
7+
* @memberof Client
8+
* @param {string|null} file_url Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
9+
* @param {Array} file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files, e.g. ["https://cdn.example.com/receipt1.jpg", "https://cdn.example.com/receipt2.jpg"]
10+
* @param {Object} kwargs Additional request parameters
11+
* @return {JSON} Data extracted from the document.
12+
*/
13+
Client.prototype.split_document_from_url = async function (
14+
file_url = null,
15+
file_urls = null,
16+
{...kwargs} = {},
17+
) {
18+
let endpoint_name = "/documents-set/";
19+
let request_arguments = {
20+
"file_url": file_url,
21+
"file_urls": file_urls
22+
};
23+
request_arguments = Object.assign(request_arguments, kwargs);
24+
let response = await this._request("POST", endpoint_name, request_arguments, null, false);
25+
return response['data'];
26+
}

lib/types/Client.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,95 @@ export declare class Client {
4545
*/
4646
public _request(http_verb: String, endpoint_name: String, request_arguments: Object, params: Object, has_files: boolean): Promise<any>;
4747

48+
/**
49+
* Classify a document. https://docs.veryfi.com/api/classify/classify-a-document/
50+
* @example
51+
* veryfi_client.classify_document_from_base64('base64_encoded_string',
52+
* 'receipt.png',
53+
* {'extra': 'parameters'})
54+
*
55+
* @memberof Client
56+
* @param {String} base64_encoded_string Buffer string of a file to submit for classify and data extraction
57+
* @param {String} file_name The file name including the extension
58+
* @param {Object} kwargs Additional request parameters
59+
* @returns {JSON} JSON of document classification
60+
*/
61+
public classify_document_from_base64(
62+
base64_encoded_string: string,
63+
file_name: string,
64+
{...kwargs}?: VeryfiExtraArgs
65+
): Promise<JsonObject>;
66+
67+
/**
68+
* Classify document. https://docs.veryfi.com/api/receipts-invoices/process-a-document/
69+
* @memberof Client
70+
* @param {string} file_url Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. 'https://cdn.example.com/receipt.jpg'.
71+
* @param {string[]} file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files, e.g. ['https://cdn.example.com/receipt1.jpg', 'https://cdn.example.com/receipt2.jpg']
72+
* @param {VeryfiExtraArgs} kwargs Additional request parameters
73+
* @returns {JSON} JSON of document classification
74+
*/
75+
public classify_document_from_url(
76+
file_url?: string,
77+
file_urls?: string[],
78+
{...kwargs}?: VeryfiExtraArgs
79+
): Promise<JsonObject>;
80+
81+
/**
82+
* Veryfi's PDF Splitter allows you to split a multipage PDF with different receipts and invoices inside into multiple Documents. This API supports .pdf,.zip. Min file size is 250bytes. The max pdf file size is 50mb. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
83+
* @memberof Client
84+
* @param {string} file_url Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. 'https://cdn.example.com/receipt.jpg'.
85+
* @param {string[]} file_urls Required if file_url isn't specified. List of publicly accessible URLs to multiple files, e.g. ['https://cdn.example.com/receipt1.jpg', 'https://cdn.example.com/receipt2.jpg']
86+
* @param {VeryfiExtraArgs} kwargs Additional request parameters
87+
* @returns {JSON} JSON of document classification
88+
*/
89+
public split_document_from_url(
90+
file_url?: string,
91+
file_urls?: string[],
92+
{...kwargs}?: VeryfiExtraArgs
93+
): Promise<JsonObject>;
94+
95+
/**
96+
* Veryfi's PDF Splitter allows you to split a multipage PDF with different receipts and invoices inside into multiple Documents. This API supports .pdf,.zip. Min file size is 250bytes. The max pdf file size is 50mb. https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
97+
* @example
98+
* veryfi_client.split_document_from_base64('base64_encoded_string',
99+
* 'receipt.png',
100+
* {'extra': 'parameters'})
101+
*
102+
* @memberof Client
103+
* @param {String} base64_encoded_string Buffer string of a file to submit for classify and data extraction
104+
* @param {String} file_name The file name including the extension
105+
* @param {Object} kwargs Additional request parameters
106+
* @returns {JSON} JSON of document classification
107+
*/
108+
public split_document_from_base64(
109+
base64_encoded_string: string,
110+
file_name: string,
111+
{...kwargs}?: VeryfiExtraArgs
112+
): Promise<JsonObject>;
113+
114+
/**
115+
* 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/
116+
* @memberof Client
117+
* @param {string} document_id ID of the document you'd like to retrieve
118+
* @param {Object} kwargs Additional request parameters
119+
* @returns {Promise<JsonObject>} Object of data extracted from the document
120+
*/
121+
public get_split_document(document_id: string,
122+
{...kwargs}?: VeryfiExtraArgs): Promise<JsonObject>;
123+
124+
/**
125+
* 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/
126+
* @memberof Client
127+
* @param {number} page The page number. The response is capped to maximum of 50 results per page.
128+
* @param {number} page_size The number of Documents per page.
129+
* @param {Object} kwargs Additional request parameters
130+
* @returns {Promise<JsonObject>} Object of previously processed documents
131+
*/
132+
public get_split_documents(
133+
page?: number,
134+
page_size?: number,
135+
{...kwargs}?: VeryfiExtraArgs
136+
): Promise<JsonObject>;
48137

49138
/**
50139
* Delete document from Veryfi. https://docs.veryfi.com/api/receipts-invoices/delete-a-document/

mocks/classify.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"data": {
3+
"document_type": {
4+
"value": "receipt",
5+
"score": 0.97
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)