11import json
2+ from typing import Dict
23
34from mindee .inputs import (
45 InputDocument ,
78 FileDocument ,
89 PathDocument ,
910)
11+ from mindee .logger import logger
1012from mindee .response import format_response , DocumentResponse
11- from mindee .http import HTTPException
13+ from mindee .http import (
14+ HTTPException ,
15+ Endpoint ,
16+ InvoiceEndpoint ,
17+ ReceiptEndpoint ,
18+ PassportEndpoint ,
19+ )
1220from mindee .document_config import DocumentConfig , DocumentConfigDict
1321from mindee .documents .receipt import Receipt
1422from mindee .documents .financial_document import FinancialDocument
1523from mindee .documents .invoice import Invoice
1624from mindee .documents .passport import Passport
25+ from mindee .documents .custom_document import CustomDocument
1726
1827
1928class DocumentClient :
@@ -39,6 +48,8 @@ def parse(
3948 :param username:
4049 :param include_words: Bool, extract all words into http_response
4150 """
51+ logger .debug ("Parsing document as '%s'" , document_type )
52+
4253 found = []
4354 for k in self .doc_configs .keys ():
4455 if k [1 ] == document_type :
@@ -107,12 +118,7 @@ def __init__(self, raise_on_error: bool = True):
107118 """
108119 :param raise_on_error: Raise an Exception on HTTP errors
109120 """
110- self ._doc_configs = {
111- ("mindee" , "receipt" ): Receipt .get_document_config (),
112- ("mindee" , "invoice" ): Invoice .get_document_config (),
113- ("mindee" , "financial_doc" ): FinancialDocument .get_document_config (),
114- ("mindee" , "passport" ): Passport .get_document_config (),
115- }
121+ self ._doc_configs : Dict [tuple , DocumentConfig ] = {}
116122 self .raise_on_error = raise_on_error
117123
118124 def config_custom_doc (
@@ -138,14 +144,18 @@ def config_custom_doc(
138144 If not set, use the latest version of the model.
139145 """
140146 self ._doc_configs [(account_name , document_type )] = DocumentConfig (
141- {
142- "document_type" : document_type ,
143- "singular_name" : singular_name ,
144- "plural_name" : plural_name ,
145- "account_name" : account_name ,
146- "api_key" : api_key ,
147- "interface_version" : version ,
148- }
147+ document_type = document_type ,
148+ singular_name = singular_name ,
149+ plural_name = plural_name ,
150+ constructor = CustomDocument ,
151+ endpoints = [
152+ Endpoint (
153+ owner = account_name ,
154+ url_name = document_type ,
155+ version = version ,
156+ api_key = api_key ,
157+ ),
158+ ],
149159 )
150160 return self
151161
@@ -157,8 +167,14 @@ def config_invoice(self, api_key: str = None):
157167
158168 :param api_key: Invoice API key
159169 """
160- if api_key :
161- self ._doc_configs [("mindee" , "invoice" )].endpoints [0 ].api_key = api_key
170+ config = DocumentConfig (
171+ document_type = "invoice" ,
172+ singular_name = "invoice" ,
173+ plural_name = "invoices" ,
174+ constructor = Invoice ,
175+ endpoints = [InvoiceEndpoint (api_key = api_key )],
176+ )
177+ self ._doc_configs [("mindee" , "invoice" )] = config
162178 return self
163179
164180 def config_receipt (self , api_key : str = None ):
@@ -169,8 +185,14 @@ def config_receipt(self, api_key: str = None):
169185
170186 :param api_key: Expense Receipt API key
171187 """
172- if api_key :
173- self ._doc_configs [("mindee" , "receipt" )].endpoints [0 ].api_key = api_key
188+ config = DocumentConfig (
189+ document_type = "receipt" ,
190+ singular_name = "receipt" ,
191+ plural_name = "receipts" ,
192+ constructor = Receipt ,
193+ endpoints = [ReceiptEndpoint (api_key = api_key )],
194+ )
195+ self ._doc_configs [("mindee" , "receipt" )] = config
174196 return self
175197
176198 def config_financial_doc (
@@ -184,14 +206,17 @@ def config_financial_doc(
184206 :param receipt_api_key: Expense Receipt API key
185207 :param invoice_api_key: Invoice API key
186208 """
187- if invoice_api_key :
188- self ._doc_configs [("mindee" , "financial_doc" )].endpoints [
189- 0
190- ].api_key = invoice_api_key
191- if receipt_api_key :
192- self ._doc_configs [("mindee" , "financial_doc" )].endpoints [
193- 1
194- ].api_key = receipt_api_key
209+ config = DocumentConfig (
210+ document_type = "financial_doc" ,
211+ singular_name = "financial_doc" ,
212+ plural_name = "financial_docs" ,
213+ constructor = FinancialDocument ,
214+ endpoints = [
215+ InvoiceEndpoint (api_key = invoice_api_key ),
216+ ReceiptEndpoint (api_key = receipt_api_key ),
217+ ],
218+ )
219+ self ._doc_configs [("mindee" , "financial_doc" )] = config
195220 return self
196221
197222 def config_passport (self , api_key : str = None ):
@@ -202,8 +227,14 @@ def config_passport(self, api_key: str = None):
202227
203228 :param api_key: Passport API key
204229 """
205- if api_key :
206- self ._doc_configs [("mindee" , "passport" )].endpoints [0 ].api_key = api_key
230+ config = DocumentConfig (
231+ document_type = "passport" ,
232+ singular_name = "passport" ,
233+ plural_name = "passports" ,
234+ constructor = Passport ,
235+ endpoints = [PassportEndpoint (api_key = api_key )],
236+ )
237+ self ._doc_configs [("mindee" , "passport" )] = config
207238 return self
208239
209240 def doc_from_path (
0 commit comments