8
8
9
9
import requests
10
10
11
- from veryfi .model import AddLineItem , UpdateLineItem
12
11
from veryfi .errors import VeryfiClientError
13
12
14
13
@@ -41,15 +40,14 @@ def __init__(
41
40
username ,
42
41
api_key ,
43
42
base_url = BASE_URL ,
44
- api_version = API_VERSION ,
45
43
timeout = API_TIMEOUT ,
46
44
):
47
45
self .client_id = client_id
48
46
self .client_secret = client_secret
49
47
self .username = username
50
48
self .api_key = api_key
51
49
self .base_url = base_url
52
- self .api_version = api_version
50
+ self .api_version = "v8"
53
51
self .versioned_url = self .base_url + self .api_version
54
52
self .timeout = timeout
55
53
self .headers = {}
@@ -66,12 +64,11 @@ def _get_headers(self) -> Dict:
66
64
"Content-Type" : "application/json" ,
67
65
"Client-Id" : self .client_id ,
68
66
}
69
-
70
67
final_headers .update ({"Authorization" : f"apikey { self .username } :{ self .api_key } " })
71
68
72
69
return final_headers
73
70
74
- def _request (self , http_verb , endpoint_name , request_arguments ):
71
+ def _request (self , http_verb , endpoint_name , request_arguments = None ):
75
72
"""
76
73
Submit the HTTP request.
77
74
:param http_verb: HTTP Method
@@ -80,8 +77,8 @@ def _request(self, http_verb, endpoint_name, request_arguments):
80
77
:return: A JSON of the response data.
81
78
"""
82
79
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 {}
85
82
if self .client_secret :
86
83
timestamp = int (time .time () * 1000 )
87
84
signature = self ._generate_signature (request_arguments , timestamp = timestamp )
@@ -112,10 +109,10 @@ def _generate_signature(self, payload_params, timestamp):
112
109
:param timestamp: Unix Long timestamp
113
110
:return: Unique signature generated using the client_secret and the payload
114
111
"""
115
- payload = "timestamp:{}" . format ( timestamp )
112
+ payload = f "timestamp:{ timestamp } "
116
113
for key in payload_params .keys ():
117
114
value = payload_params [key ]
118
- payload = "{0 },{1 }:{2}" . format ( payload , key , value )
115
+ payload = f" { payload } ,{ key } :{ value } "
119
116
120
117
secret_bytes = bytes (self .client_secret , "utf-8" )
121
118
payload_bytes = bytes (payload , "utf-8" )
@@ -135,15 +132,7 @@ def get_documents(
135
132
** kwargs : Dict ,
136
133
):
137
134
"""
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/
147
136
:return: List of previously processed documents
148
137
"""
149
138
endpoint_name = "/documents/"
@@ -173,16 +162,14 @@ def get_documents(
173
162
return documents ["documents" ]
174
163
return documents
175
164
176
- def get_document (self , document_id ):
165
+ def get_document (self , document_id , ** kwargs : Dict ):
177
166
"""
178
167
Retrieve document by ID
168
+ https://docs.veryfi.com/api/receipts-invoices/get-a-document/
179
169
:param document_id: ID of the document you'd like to retrieve
180
170
:return: Data extracted from the Document
181
171
"""
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 )
186
173
187
174
def process_document (
188
175
self ,
@@ -192,15 +179,15 @@ def process_document(
192
179
** kwargs : Dict ,
193
180
):
194
181
"""
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/
196
184
:param file_path: Path on disk to a file to submit for data extraction
197
185
:param categories: List of categories Veryfi can use to categorize the document
198
186
:param delete_after_processing: Delete this document from Veryfi after data has been extracted
199
187
:param kwargs: Additional request parameters
200
188
201
189
:return: Data extracted from the document
202
190
"""
203
- endpoint_name = "/documents/"
204
191
if not categories :
205
192
categories = self .CATEGORIES
206
193
file_name = os .path .basename (file_path )
@@ -213,8 +200,7 @@ def process_document(
213
200
"auto_delete" : delete_after_processing ,
214
201
}
215
202
request_arguments .update (kwargs )
216
- document = self ._request ("POST" , endpoint_name , request_arguments )
217
- return document
203
+ return self ._request ("POST" , "/documents/" , request_arguments )
218
204
219
205
def process_document_url (
220
206
self ,
@@ -228,7 +214,7 @@ def process_document_url(
228
214
** kwargs : Dict ,
229
215
) -> Dict :
230
216
"""Process Document from url and extract all the fields from it.
231
-
217
+ https://docs.veryfi.com/api/receipts-invoices/process-a-document/
232
218
:param file_url: Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
233
219
: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"]
234
220
:param categories: List of categories to use when categorizing the document
@@ -254,10 +240,11 @@ def process_document_url(
254
240
return self ._request ("POST" , endpoint_name , request_arguments )
255
241
256
242
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
258
244
) -> Dict :
259
245
"""
260
246
Process W9 Document from url and extract all the fields from it.
247
+ https://docs.veryfi.com/api/w9s/process-a-w-9/
261
248
262
249
:param file_url: Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
263
250
:param file_name: Optional name of file, eg. receipt.jpg
@@ -278,6 +265,7 @@ def process_w9_document_url(
278
265
def process_w9_document (self , file_path : str , file_name : Optional [str ] = None , ** kwargs ):
279
266
"""
280
267
Process W9 Document from url and extract all the fields from it.
268
+ https://docs.veryfi.com/api/w9s/process-a-w-9/
281
269
282
270
:param file_path: Path on disk to a file to submit for data extraction
283
271
: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, *
301
289
def delete_document (self , document_id ):
302
290
"""
303
291
Delete Document from Veryfi
292
+ https://docs.veryfi.com/api/receipts-invoices/delete-a-document/
304
293
:param document_id: ID of the document you'd like to delete
305
294
"""
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 })
309
296
310
297
def update_document (self , document_id : int , ** kwargs ) -> Dict :
311
298
"""
312
299
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/
313
301
314
302
```veryfi_client.update_document(id, date="2021-01-01", notes="look what I did")```
315
303
@@ -318,97 +306,86 @@ def update_document(self, document_id: int, **kwargs) -> Dict:
318
306
319
307
:return: A document json with updated fields, if fields are writable. Otherwise a document with unchanged fields.
320
308
"""
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 )
324
310
325
311
def get_line_items (self , document_id ):
326
312
"""
327
313
Retrieve all line items for a document.
314
+ https://docs.veryfi.com/api/receipts-invoices/get-document-line-items/
328
315
:param document_id: ID of the document you'd like to retrieve
329
316
:return: List of line items extracted from the document
330
317
"""
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/" )
335
319
336
320
def get_line_item (self , document_id , line_item_id ):
337
321
"""
338
322
Retrieve a line item for existing document by ID.
323
+ https://docs.veryfi.com/api/receipts-invoices/get-a-line-item/
339
324
:param document_id: ID of the document you'd like to retrieve
340
325
:param line_item_id: ID of the line item you'd like to retrieve
341
326
:return: Line item extracted from the document
342
327
"""
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 } " )
347
329
348
330
def add_line_item (self , document_id : int , payload : Dict ) -> Dict :
349
331
"""
350
332
Add a new line item on an existing document.
333
+ https://docs.veryfi.com/api/receipts-invoices/create-a-line-item/
351
334
:param document_id: ID of the document you'd like to update
352
335
:param payload: line item object to add
353
336
:return: Added line item data
354
337
"""
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 )
358
339
359
340
def update_line_item (self , document_id : int , line_item_id : int , payload : Dict ) -> Dict :
360
341
"""
361
342
Update an existing line item on an existing document.
343
+ https://docs.veryfi.com/api/receipts-invoices/update-a-line-item/
362
344
:param document_id: ID of the document you'd like to update
363
345
:param line_item_id: ID of the line item you'd like to update
364
346
:param payload: line item object to update
365
347
366
348
:return: Line item data with updated fields, if fields are writable. Otherwise line item data with unchanged fields.
367
349
"""
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 )
371
351
372
352
def delete_line_items (self , document_id ):
373
353
"""
374
354
Delete all line items on an existing document.
355
+ https://docs.veryfi.com/api/receipts-invoices/delete-all-document-line-items/
375
356
:param document_id: ID of the document you'd like to delete
376
357
"""
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/" )
380
359
381
360
def delete_line_item (self , document_id , line_item_id ):
382
361
"""
383
362
Delete an existing line item on an existing document.
363
+ https://docs.veryfi.com/api/receipts-invoices/delete-a-line-item/
364
+
384
365
:param document_id: ID of the document you'd like to delete
385
366
:param line_item_id: ID of the line item you'd like to delete
386
367
"""
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 } " )
390
369
391
370
def add_tag (self , document_id , tag_name ):
392
371
"""
393
372
Add a new tag on an existing document.
373
+ https://docs.veryfi.com/api/receipts-invoices/add-a-tag-to-a-document/
394
374
:param document_id: ID of the document you'd like to update
395
375
:param tag_name: name of the new tag
396
376
:return: Added tag data
397
377
"""
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 })
401
379
402
380
def replace_tags (self , document_id , tags ):
403
381
"""
404
382
Replace multiple tags on an existing document.
383
+ https://docs.veryfi.com/api/receipts-invoices/update-a-document/
405
384
:param document_id: ID of the document you'd like to update
406
385
:param tags: array of strings
407
386
:return: Added tags data
408
387
"""
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 })
412
389
413
390
def add_tags (self , document_id , tags ):
414
391
"""
@@ -417,6 +394,4 @@ def add_tags(self, document_id, tags):
417
394
:param tags: array of strings
418
395
:return: Added tags data
419
396
"""
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