88
99import requests
1010
11- from veryfi .model import AddLineItem , UpdateLineItem
1211from veryfi .errors import VeryfiClientError
1312
1413
@@ -41,15 +40,14 @@ def __init__(
4140 username ,
4241 api_key ,
4342 base_url = BASE_URL ,
44- api_version = API_VERSION ,
4543 timeout = API_TIMEOUT ,
4644 ):
4745 self .client_id = client_id
4846 self .client_secret = client_secret
4947 self .username = username
5048 self .api_key = api_key
5149 self .base_url = base_url
52- self .api_version = api_version
50+ self .api_version = "v8"
5351 self .versioned_url = self .base_url + self .api_version
5452 self .timeout = timeout
5553 self .headers = {}
@@ -66,12 +64,11 @@ def _get_headers(self) -> Dict:
6664 "Content-Type" : "application/json" ,
6765 "Client-Id" : self .client_id ,
6866 }
69-
7067 final_headers .update ({"Authorization" : f"apikey { self .username } :{ self .api_key } " })
7168
7269 return final_headers
7370
74- def _request (self , http_verb , endpoint_name , request_arguments ):
71+ def _request (self , http_verb , endpoint_name , request_arguments = None ):
7572 """
7673 Submit the HTTP request.
7774 :param http_verb: HTTP Method
@@ -80,8 +77,8 @@ def _request(self, http_verb, endpoint_name, request_arguments):
8077 :return: A JSON of the response data.
8178 """
8279 headers = self ._get_headers ()
83- api_url = "{0 }/partner{1}" . format ( self . versioned_url , endpoint_name )
84-
80+ api_url = f" { self . versioned_url } /partner{ endpoint_name } "
81+ request_arguments = request_arguments or {}
8582 if self .client_secret :
8683 timestamp = int (time .time () * 1000 )
8784 signature = self ._generate_signature (request_arguments , timestamp = timestamp )
@@ -112,10 +109,10 @@ def _generate_signature(self, payload_params, timestamp):
112109 :param timestamp: Unix Long timestamp
113110 :return: Unique signature generated using the client_secret and the payload
114111 """
115- payload = "timestamp:{}" . format ( timestamp )
112+ payload = f "timestamp:{ timestamp } "
116113 for key in payload_params .keys ():
117114 value = payload_params [key ]
118- payload = "{0 },{1 }:{2}" . format ( payload , key , value )
115+ payload = f" { payload } ,{ key } :{ value } "
119116
120117 secret_bytes = bytes (self .client_secret , "utf-8" )
121118 payload_bytes = bytes (payload , "utf-8" )
@@ -135,15 +132,7 @@ def get_documents(
135132 ** kwargs : Dict ,
136133 ):
137134 """
138- Get list of documents
139- :param query: Search term to search for a specific document by its content. These fields will be searched: external_id, category, vendor.name, notes, invoice_number, total and ocr_text.
140- :param external_id: Search for documents that match your custom identifier
141- :param tag: Search for documents with the specified tag
142- :param created__gt: Search for documents with a created date greater than this one. Format YYYY-MM-DD+HH:MM:SS. Don't send both created__gt and created__gte in a single request.
143- :param created__gte: Search for documents with a created date greater than or equal to this one. Format YYYY-MM-DD+HH:MM:SS. Don't send both created__gt and created__gte in a single request.
144- :param created__lt: Search for documents with a created date greater than this one. Format YYYY-MM-DD+HH:MM:SS. Don't send both created__lt and created__lte in a single request.
145- :param created__lte: Search for documents with a created date less than or equal to this one. Format YYYY-MM-DD+HH:MM:SS. Don't send both created__lt and created__lte in a single request.
146- :param kwargs: Additional request parameters
135+ Get list of documents. Please refer to https://docs.veryfi.com/api/receipts-invoices/search-documents/
147136 :return: List of previously processed documents
148137 """
149138 endpoint_name = "/documents/"
@@ -173,16 +162,14 @@ def get_documents(
173162 return documents ["documents" ]
174163 return documents
175164
176- def get_document (self , document_id ):
165+ def get_document (self , document_id , ** kwargs : Dict ):
177166 """
178167 Retrieve document by ID
168+ https://docs.veryfi.com/api/receipts-invoices/get-a-document/
179169 :param document_id: ID of the document you'd like to retrieve
180170 :return: Data extracted from the Document
181171 """
182- endpoint_name = "/documents/{}/" .format (document_id )
183- request_arguments = {"id" : document_id }
184- document = self ._request ("GET" , endpoint_name , request_arguments )
185- return document
172+ return self ._request ("GET" , f"/documents/{ document_id } /" , kwargs )
186173
187174 def process_document (
188175 self ,
@@ -192,15 +179,15 @@ def process_document(
192179 ** kwargs : Dict ,
193180 ):
194181 """
195- Process a document and extract all the fields from it
182+ Process a document and extract all the fields from it.
183+ https://docs.veryfi.com/api/receipts-invoices/process-a-document/
196184 :param file_path: Path on disk to a file to submit for data extraction
197185 :param categories: List of categories Veryfi can use to categorize the document
198186 :param delete_after_processing: Delete this document from Veryfi after data has been extracted
199187 :param kwargs: Additional request parameters
200188
201189 :return: Data extracted from the document
202190 """
203- endpoint_name = "/documents/"
204191 if not categories :
205192 categories = self .CATEGORIES
206193 file_name = os .path .basename (file_path )
@@ -213,8 +200,7 @@ def process_document(
213200 "auto_delete" : delete_after_processing ,
214201 }
215202 request_arguments .update (kwargs )
216- document = self ._request ("POST" , endpoint_name , request_arguments )
217- return document
203+ return self ._request ("POST" , "/documents/" , request_arguments )
218204
219205 def process_document_url (
220206 self ,
@@ -228,7 +214,7 @@ def process_document_url(
228214 ** kwargs : Dict ,
229215 ) -> Dict :
230216 """Process Document from url and extract all the fields from it.
231-
217+ https://docs.veryfi.com/api/receipts-invoices/process-a-document/
232218 :param file_url: Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
233219 :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"]
234220 :param categories: List of categories to use when categorizing the document
@@ -254,10 +240,11 @@ def process_document_url(
254240 return self ._request ("POST" , endpoint_name , request_arguments )
255241
256242 def process_w9_document_url (
257- self , file_url : str , file_name : Optional [str ] = None , ** kwargs : Dict
243+ self , file_url : str , file_name : Optional [str ] = None , ** kwargs
258244 ) -> Dict :
259245 """
260246 Process W9 Document from url and extract all the fields from it.
247+ https://docs.veryfi.com/api/w9s/process-a-w-9/
261248
262249 :param file_url: Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
263250 :param file_name: Optional name of file, eg. receipt.jpg
@@ -278,6 +265,7 @@ def process_w9_document_url(
278265 def process_w9_document (self , file_path : str , file_name : Optional [str ] = None , ** kwargs ):
279266 """
280267 Process W9 Document from url and extract all the fields from it.
268+ https://docs.veryfi.com/api/w9s/process-a-w-9/
281269
282270 :param file_path: Path on disk to a file to submit for data extraction
283271 :param file_name: Optional name of file, eg. receipt.jpg
@@ -301,15 +289,15 @@ def process_w9_document(self, file_path: str, file_name: Optional[str] = None, *
301289 def delete_document (self , document_id ):
302290 """
303291 Delete Document from Veryfi
292+ https://docs.veryfi.com/api/receipts-invoices/delete-a-document/
304293 :param document_id: ID of the document you'd like to delete
305294 """
306- endpoint_name = f"/documents/{ document_id } /"
307- request_arguments = {"id" : document_id }
308- self ._request ("DELETE" , endpoint_name , request_arguments )
295+ self ._request ("DELETE" , f"/documents/{ document_id } /" , {"id" : document_id })
309296
310297 def update_document (self , document_id : int , ** kwargs ) -> Dict :
311298 """
312299 Update data for a previously processed document, including almost any field like `vendor`, `date`, `notes` and etc.
300+ https://docs.veryfi.com/api/receipts-invoices/update-a-document/
313301
314302 ```veryfi_client.update_document(id, date="2021-01-01", notes="look what I did")```
315303
@@ -318,97 +306,86 @@ def update_document(self, document_id: int, **kwargs) -> Dict:
318306
319307 :return: A document json with updated fields, if fields are writable. Otherwise a document with unchanged fields.
320308 """
321- endpoint_name = f"/documents/{ document_id } /"
322-
323- return self ._request ("PUT" , endpoint_name , kwargs )
309+ return self ._request ("PUT" , f"/documents/{ document_id } /" , kwargs )
324310
325311 def get_line_items (self , document_id ):
326312 """
327313 Retrieve all line items for a document.
314+ https://docs.veryfi.com/api/receipts-invoices/get-document-line-items/
328315 :param document_id: ID of the document you'd like to retrieve
329316 :return: List of line items extracted from the document
330317 """
331- endpoint_name = f"/documents/{ document_id } /line-items/"
332- request_arguments = {}
333- line_items = self ._request ("GET" , endpoint_name , request_arguments )
334- return line_items
318+ return self ._request ("GET" , f"/documents/{ document_id } /line-items/" )
335319
336320 def get_line_item (self , document_id , line_item_id ):
337321 """
338322 Retrieve a line item for existing document by ID.
323+ https://docs.veryfi.com/api/receipts-invoices/get-a-line-item/
339324 :param document_id: ID of the document you'd like to retrieve
340325 :param line_item_id: ID of the line item you'd like to retrieve
341326 :return: Line item extracted from the document
342327 """
343- endpoint_name = f"/documents/{ document_id } /line-items/{ line_item_id } "
344- request_arguments = {}
345- line_items = self ._request ("GET" , endpoint_name , request_arguments )
346- return line_items
328+ return self ._request ("GET" , f"/documents/{ document_id } /line-items/{ line_item_id } " )
347329
348330 def add_line_item (self , document_id : int , payload : Dict ) -> Dict :
349331 """
350332 Add a new line item on an existing document.
333+ https://docs.veryfi.com/api/receipts-invoices/create-a-line-item/
351334 :param document_id: ID of the document you'd like to update
352335 :param payload: line item object to add
353336 :return: Added line item data
354337 """
355- endpoint_name = f"/documents/{ document_id } /line-items/"
356- request_arguments = AddLineItem (** payload ).dict (exclude_none = True )
357- return self ._request ("POST" , endpoint_name , request_arguments )
338+ return self ._request ("POST" , f"/documents/{ document_id } /line-items/" , payload )
358339
359340 def update_line_item (self , document_id : int , line_item_id : int , payload : Dict ) -> Dict :
360341 """
361342 Update an existing line item on an existing document.
343+ https://docs.veryfi.com/api/receipts-invoices/update-a-line-item/
362344 :param document_id: ID of the document you'd like to update
363345 :param line_item_id: ID of the line item you'd like to update
364346 :param payload: line item object to update
365347
366348 :return: Line item data with updated fields, if fields are writable. Otherwise line item data with unchanged fields.
367349 """
368- endpoint_name = f"/documents/{ document_id } /line-items/{ line_item_id } "
369- request_arguments = UpdateLineItem (** payload ).dict (exclude_none = True )
370- return self ._request ("PUT" , endpoint_name , request_arguments )
350+ return self ._request ("PUT" , f"/documents/{ document_id } /line-items/{ line_item_id } " , payload )
371351
372352 def delete_line_items (self , document_id ):
373353 """
374354 Delete all line items on an existing document.
355+ https://docs.veryfi.com/api/receipts-invoices/delete-all-document-line-items/
375356 :param document_id: ID of the document you'd like to delete
376357 """
377- endpoint_name = f"/documents/{ document_id } /line-items/"
378- request_arguments = {}
379- self ._request ("DELETE" , endpoint_name , request_arguments )
358+ self ._request ("DELETE" , f"/documents/{ document_id } /line-items/" )
380359
381360 def delete_line_item (self , document_id , line_item_id ):
382361 """
383362 Delete an existing line item on an existing document.
363+ https://docs.veryfi.com/api/receipts-invoices/delete-a-line-item/
364+
384365 :param document_id: ID of the document you'd like to delete
385366 :param line_item_id: ID of the line item you'd like to delete
386367 """
387- endpoint_name = f"/documents/{ document_id } /line-items/{ line_item_id } "
388- request_arguments = {}
389- self ._request ("DELETE" , endpoint_name , request_arguments )
368+ self ._request ("DELETE" , f"/documents/{ document_id } /line-items/{ line_item_id } " )
390369
391370 def add_tag (self , document_id , tag_name ):
392371 """
393372 Add a new tag on an existing document.
373+ https://docs.veryfi.com/api/receipts-invoices/add-a-tag-to-a-document/
394374 :param document_id: ID of the document you'd like to update
395375 :param tag_name: name of the new tag
396376 :return: Added tag data
397377 """
398- endpoint_name = f"/documents/{ document_id } /tags/"
399- request_arguments = {"name" : tag_name }
400- return self ._request ("PUT" , endpoint_name , request_arguments )
378+ return self ._request ("PUT" , f"/documents/{ document_id } /tags/" , {"name" : tag_name })
401379
402380 def replace_tags (self , document_id , tags ):
403381 """
404382 Replace multiple tags on an existing document.
383+ https://docs.veryfi.com/api/receipts-invoices/update-a-document/
405384 :param document_id: ID of the document you'd like to update
406385 :param tags: array of strings
407386 :return: Added tags data
408387 """
409- endpoint_name = f"/documents/{ document_id } /"
410- request_arguments = {"tags" : tags }
411- return self ._request ("PUT" , endpoint_name , request_arguments )
388+ return self ._request ("PUT" , f"/documents/{ document_id } /" , {"tags" : tags })
412389
413390 def add_tags (self , document_id , tags ):
414391 """
@@ -417,6 +394,4 @@ def add_tags(self, document_id, tags):
417394 :param tags: array of strings
418395 :return: Added tags data
419396 """
420- endpoint_name = f"/documents/{ document_id } /tags/"
421- request_arguments = {"tags" : tags }
422- return self ._request ("POST" , endpoint_name , request_arguments )
397+ return self ._request ("POST" , f"/documents/{ document_id } /tags/" , {"tags" : tags })
0 commit comments