Skip to content

Commit f8b637b

Browse files
committed
refactor and add missing endpoints
1 parent 4de25b2 commit f8b637b

17 files changed

Lines changed: 1469 additions & 386 deletions

veryfi/_documents/__init__.py

Whitespace-only changes.

veryfi/_documents/line_items.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import *
2+
3+
from veryfi.client_base import Client
4+
5+
6+
class LineItems:
7+
def __init__(self, client: Client):
8+
self.client = client
9+
10+
def get_line_items(self, document_id):
11+
"""
12+
Retrieve all line items for a document.
13+
https://docs.veryfi.com/api/receipts-invoices/get-document-line-items/
14+
15+
:param document_id: ID of the document you'd like to retrieve
16+
:return: List of line items extracted from the document
17+
"""
18+
return self.client._request("GET", f"/documents/{document_id}/line-items/")
19+
20+
def get_line_item(self, document_id, line_item_id):
21+
"""
22+
Retrieve a line item for existing document by ID.
23+
https://docs.veryfi.com/api/receipts-invoices/get-a-line-item/
24+
25+
:param document_id: ID of the document you'd like to retrieve
26+
:param line_item_id: ID of the line item you'd like to retrieve
27+
:return: Line item extracted from the document
28+
"""
29+
return self.client._request("GET", f"/documents/{document_id}/line-items/{line_item_id}")
30+
31+
def add_line_item(self, document_id: int, payload: Dict) -> Dict:
32+
"""
33+
Add a new line item on an existing document.
34+
https://docs.veryfi.com/api/receipts-invoices/create-a-line-item/
35+
36+
:param document_id: ID of the document you'd like to update
37+
:param payload: line item object to add
38+
:return: Added line item data
39+
"""
40+
return self.client._request("POST", f"/documents/{document_id}/line-items/", payload)
41+
42+
def update_line_item(self, document_id: int, line_item_id: int, payload: Dict) -> Dict:
43+
"""
44+
Update an existing line item on an existing document.
45+
https://docs.veryfi.com/api/receipts-invoices/update-a-line-item/
46+
47+
:param document_id: ID of the document you'd like to update
48+
:param line_item_id: ID of the line item you'd like to update
49+
:param payload: line item object to update
50+
:return: Line item data with updated fields, if fields are writable. Otherwise line item data with unchanged fields.
51+
"""
52+
return self.client._request(
53+
"PUT", f"/documents/{document_id}/line-items/{line_item_id}", payload
54+
)
55+
56+
def delete_line_items(self, document_id):
57+
"""
58+
Delete all line items on an existing document.
59+
https://docs.veryfi.com/api/receipts-invoices/delete-all-document-line-items/
60+
61+
:param document_id: ID of the document you'd like to delete
62+
"""
63+
self.client._request("DELETE", f"/documents/{document_id}/line-items/")
64+
65+
def delete_line_item(self, document_id, line_item_id):
66+
"""
67+
Delete an existing line item on an existing document.
68+
https://docs.veryfi.com/api/receipts-invoices/delete-a-line-item/
69+
70+
:param document_id: ID of the document you'd like to delete
71+
:param line_item_id: ID of the line item you'd like to delete
72+
"""
73+
self.client._request("DELETE", f"/documents/{document_id}/line-items/{line_item_id}")

veryfi/_documents/pdf_split.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import base64
3+
from typing import *
4+
5+
from veryfi.client_base import Client
6+
7+
8+
class PDFSplit:
9+
def __init__(self, client: Client):
10+
self.client = client
11+
12+
def get_pdf(self, **kwargs):
13+
"""
14+
Get a Submitted PDF endpoint allows you to retrieve a collection of previously processed.
15+
https://docs.veryfi.com/api/receipts-invoices/get-submitted-pdf/
16+
17+
:param kwargs: Additional query parameters.
18+
:return: The processed Document response.
19+
"""
20+
endpoint_name = "/documents-set/"
21+
return self.client._request("GET", endpoint_name, {}, kwargs)
22+
23+
def get_documents_from_pdf(self, document_id: int):
24+
"""
25+
Get Documents from PDF endpoint allows you to retrieve a collection of previously processed documents.
26+
https://docs.veryfi.com/api/receipts-invoices/get-documents-from-pdf/
27+
:param document_id: ID of the document you'd like to retrieve
28+
:return: The processed Document response.
29+
"""
30+
endpoint_name = f"/documents-set/{document_id}"
31+
return self.client._request("GET", endpoint_name, {})
32+
33+
def split_and_process_pdf(
34+
self,
35+
file_path: str,
36+
categories: Optional[List] = None,
37+
**kwargs,
38+
) -> Dict:
39+
"""
40+
Process a document and extract all the fields from it
41+
https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
42+
43+
:param file_path: Path on disk to a file to submit for data extraction
44+
:param categories: List of categories Veryfi can use to categorize the document
45+
:param kwargs: Additional body parameters
46+
:return: Data extracted from the document
47+
"""
48+
endpoint_name = "/documents-set/"
49+
categories = categories or []
50+
file_name = os.path.basename(file_path)
51+
with open(file_path, "rb") as image_file:
52+
base64_encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
53+
request_arguments = {
54+
"file_name": file_name,
55+
"file_data": base64_encoded_string,
56+
"categories": categories,
57+
}
58+
request_arguments.update(kwargs)
59+
return self.client._request("POST", endpoint_name, request_arguments)
60+
61+
def split_and_process_pdf_url(
62+
self,
63+
file_url: Optional[str] = None,
64+
categories: Optional[List[str]] = None,
65+
max_pages_to_process: Optional[int] = None,
66+
file_urls: Optional[List[str]] = None,
67+
**kwargs,
68+
) -> Dict:
69+
"""Process Document from url and extract all the fields from it.
70+
https://docs.veryfi.com/api/receipts-invoices/split-and-process-a-pdf/
71+
72+
:param file_url: Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
73+
:param file_urls: 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"]
74+
:param categories: List of categories to use when categorizing the document
75+
:param max_pages_to_process: When sending a long document to Veryfi for processing, this parameter controls how many pages of the document will be read and processed, starting from page 1.
76+
:param kwargs: Additional body parameters
77+
:return: Data extracted from the document.
78+
"""
79+
endpoint_name = "/documents-set/"
80+
categories = categories or []
81+
request_arguments = {
82+
"categories": categories,
83+
"file_url": file_url,
84+
"file_urls": file_urls,
85+
"max_pages_to_process": max_pages_to_process,
86+
}
87+
request_arguments.update(kwargs)
88+
return self.client._request("POST", endpoint_name, request_arguments)

veryfi/_documents/tags.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from typing import *
2+
from veryfi.client_base import Client
3+
4+
5+
class Tags:
6+
def __init__(self, client: Client):
7+
self.client = client
8+
9+
def get_tags(self, document_id):
10+
"""
11+
Return all Tag assigned to a specific Document.
12+
https://docs.veryfi.com/api/receipts-invoices/get-document-tags/
13+
14+
:param document_id: ID of the document you'd like to get
15+
:return: Added tags data
16+
"""
17+
endpoint_name = f"/documents/{document_id}/tags"
18+
return self.client._request("GET", endpoint_name, {})
19+
20+
def add_tag(self, document_id, tag_name):
21+
"""
22+
Add a new tag on an existing document.
23+
https://docs.veryfi.com/api/receipts-invoices/add-a-tag-to-a-document/
24+
25+
:param document_id: ID of the document you'd like to update
26+
:param tag_name: name of the new tag
27+
:return: Added tag data
28+
"""
29+
endpoint_name = f"/documents/{document_id}/tags/"
30+
request_arguments = {"name": tag_name}
31+
return self.client._request("PUT", endpoint_name, request_arguments)
32+
33+
def replace_tags(self, document_id, tags):
34+
"""
35+
Replace multiple tags on an existing document.
36+
https://docs.veryfi.com/api/receipts-invoices/update-a-document/
37+
38+
:param document_id: ID of the document you'd like to update
39+
:param tags: array of strings
40+
:return: Added tags data
41+
"""
42+
endpoint_name = f"/documents/{document_id}/"
43+
request_arguments = {"tags": tags}
44+
return self.client._request("PUT", endpoint_name, request_arguments)
45+
46+
def add_tags(self, document_id, tags):
47+
"""
48+
Add multiple tags on an existing document.
49+
https://docs.veryfi.com/api/receipts-invoices/add-tags-to-a-document/
50+
51+
:param document_id: ID of the document you'd like to update
52+
:param tags: array of strings
53+
:return: Added tags data
54+
"""
55+
endpoint_name = f"/documents/{document_id}/tags/"
56+
request_arguments = {"tags": tags}
57+
return self.client._request("POST", endpoint_name, request_arguments)
58+
59+
def delete_tag(self, document_id, tag_id):
60+
"""
61+
Unlink a tag from the list of tags assigned to a specific Document.
62+
https://docs.veryfi.com/api/receipts-invoices/unlink-a-tag-from-a-document/
63+
64+
:param document_id: ID of the document
65+
:param tag_id: ID of the tag you'd like to unlink
66+
"""
67+
endpoint_name = f"/documents/{document_id}/tags/{tag_id}"
68+
self.client._request("DELETE", endpoint_name, {})
69+
70+
def delete_tags(self, document_id):
71+
"""
72+
Unlink all tags assigned to a specific Document.
73+
https://docs.veryfi.com/api/receipts-invoices/unlink-all-tags-from-a-document/
74+
75+
:param document_id: ID of the document
76+
"""
77+
endpoint_name = f"/documents/{document_id}/tags"
78+
self.client._request("DELETE", endpoint_name, {})

veryfi/_w2s/__init__.py

Whitespace-only changes.

veryfi/_w2s/w2_split.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
import base64
3+
from typing import *
4+
5+
from veryfi.client_base import Client
6+
7+
8+
class W2Split:
9+
def __init__(self, client: Client):
10+
self.client = client
11+
12+
def get_documents_from_w2(self, document_id: int) -> Dict:
13+
"""
14+
Veryfi's Get Documents from W-2 endpoint allows you to retrieve a collection of previously processed W-2s.
15+
https://docs.veryfi.com/api/get-documents-from-w-2/
16+
17+
:param document_id: The unique identifier of the document.
18+
:return: Document Response
19+
"""
20+
endpoint_name = f"/w2s-set/{document_id}/"
21+
return self.client._request("GET", endpoint_name, {})
22+
23+
def get_list_of_w2s(self, **kwargs) -> Dict:
24+
"""
25+
Veryfi's Get List of W-2s endpoint allows you to retrieve a collection of previously processed W-2s.
26+
https://docs.veryfi.com/api/get-list-of-documents-from-w-2/
27+
28+
:param kwargs: Query parameters
29+
:return: List of W-2s
30+
"""
31+
endpoint_name = "/w2s-set/"
32+
return self.client._request("GET", endpoint_name, {}, kwargs)
33+
34+
def split_and_process_w2(self, file_path: str, **kwargs) -> Dict:
35+
"""
36+
Process a document and extract all the fields from it
37+
https://docs.veryfi.com/api/split-and-process-a-w-2/
38+
39+
:param file_path: Path on disk to a file to submit for data extraction
40+
:param kwargs: Additional body parameters
41+
:return: Data extracted from the document
42+
"""
43+
endpoint_name = "/w2s-set/"
44+
file_name = os.path.basename(file_path)
45+
with open(file_path, "rb") as image_file:
46+
base64_encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
47+
request_arguments = {
48+
"file_name": file_name,
49+
"file_data": base64_encoded_string,
50+
}
51+
request_arguments.update(kwargs)
52+
return self.client._request("POST", endpoint_name, request_arguments)
53+
54+
def split_and_process_w2_url(
55+
self,
56+
file_url: Optional[str] = None,
57+
file_urls: Optional[List[str]] = None,
58+
max_pages_to_process: Optional[int] = None,
59+
**kwargs,
60+
) -> Dict:
61+
"""Process Document from url and extract all the fields from it.
62+
https://docs.veryfi.com/api/split-and-process-a-w-2/
63+
64+
:param file_url: Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
65+
:param file_urls: 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"]
66+
:param max_pages_to_process: When sending a long document to Veryfi for processing, this parameter controls how many pages of the document will be read and processed, starting from page 1.
67+
:param kwargs: Additional body parameters
68+
:return: Data extracted from the document.
69+
"""
70+
endpoint_name = "/w2s-set/"
71+
request_arguments = {
72+
"file_url": file_url,
73+
"file_urls": file_urls,
74+
"max_pages_to_process": max_pages_to_process,
75+
}
76+
request_arguments.update(kwargs)
77+
return self.client._request("POST", endpoint_name, request_arguments)

0 commit comments

Comments
 (0)