Skip to content

Commit b7bc605

Browse files
committed
♻️ rework client API definitions
1 parent a676bc7 commit b7bc605

File tree

2 files changed

+87
-84
lines changed

2 files changed

+87
-84
lines changed

mindee/client.py

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import BinaryIO, Dict, Optional, Type
2+
from typing import BinaryIO, Dict, List, NamedTuple, Optional, Type
33

44
from mindee.documents import (
55
CropperV1,
@@ -168,6 +168,12 @@ def close(self) -> None:
168168
self.input_doc.file_object.close()
169169

170170

171+
class ConfigSpec(NamedTuple):
172+
klass: Type[Document]
173+
url_name: str
174+
version: str
175+
176+
171177
class Client:
172178
"""
173179
Mindee API Client.
@@ -191,100 +197,97 @@ def __init__(self, api_key: str = "", raise_on_error: bool = True):
191197
self.api_key = api_key
192198
self._init_default_endpoints()
193199

200+
def _standard_doc_config(
201+
self, klass: Type[Document], url_name: str, version: str
202+
) -> DocumentConfig:
203+
return DocumentConfig(
204+
document_class=klass,
205+
endpoints=[
206+
StandardEndpoint(
207+
url_name=url_name, version=version, api_key=self.api_key
208+
)
209+
],
210+
)
211+
194212
def _init_default_endpoints(self) -> None:
195-
self._doc_configs = {
196-
(OTS_OWNER, InvoiceV3.__name__): DocumentConfig(
197-
document_class=InvoiceV3,
198-
endpoints=[
199-
StandardEndpoint(
200-
url_name="invoices", version="3", api_key=self.api_key
201-
)
202-
],
213+
configs: List[ConfigSpec] = [
214+
ConfigSpec(
215+
klass=InvoiceV3,
216+
url_name="invoices",
217+
version="3",
203218
),
204-
(OTS_OWNER, InvoiceV4.__name__): DocumentConfig(
205-
document_class=InvoiceV4,
206-
endpoints=[
207-
StandardEndpoint(
208-
url_name="invoices", version="4", api_key=self.api_key
209-
)
210-
],
219+
ConfigSpec(
220+
klass=InvoiceV4,
221+
url_name="invoices",
222+
version="4",
211223
),
212-
(OTS_OWNER, ReceiptV3.__name__): DocumentConfig(
213-
document_class=ReceiptV3,
214-
endpoints=[
215-
StandardEndpoint(
216-
url_name="expense_receipts", version="3", api_key=self.api_key
217-
)
218-
],
224+
ConfigSpec(
225+
klass=ReceiptV3,
226+
url_name="expense_receipts",
227+
version="3",
219228
),
220-
(OTS_OWNER, ReceiptV4.__name__): DocumentConfig(
221-
document_class=ReceiptV4,
222-
endpoints=[
223-
StandardEndpoint(
224-
url_name="expense_receipts", version="4", api_key=self.api_key
225-
)
226-
],
229+
ConfigSpec(
230+
klass=ReceiptV4,
231+
url_name="expense_receipts",
232+
version="4",
227233
),
228-
(OTS_OWNER, FinancialV1.__name__): DocumentConfig(
229-
document_class=FinancialV1,
230-
endpoints=[
231-
StandardEndpoint(
232-
url_name="invoices", version="3", api_key=self.api_key
233-
),
234-
StandardEndpoint(
235-
url_name="expense_receipts", version="3", api_key=self.api_key
236-
),
237-
],
234+
ConfigSpec(
235+
klass=FinancialDocumentV1,
236+
url_name="financial_document",
237+
version="1",
238238
),
239-
(OTS_OWNER, FinancialDocumentV1.__name__): DocumentConfig(
240-
document_class=FinancialDocumentV1,
241-
endpoints=[
242-
StandardEndpoint(
243-
url_name="financial_document", version="1", api_key=self.api_key
244-
),
245-
],
239+
ConfigSpec(
240+
klass=PassportV1,
241+
url_name="passport",
242+
version="1",
246243
),
247-
(OTS_OWNER, PassportV1.__name__): DocumentConfig(
248-
document_class=PassportV1,
249-
endpoints=[
250-
StandardEndpoint(
251-
url_name="passport", version="1", api_key=self.api_key
252-
)
253-
],
244+
ConfigSpec(
245+
klass=ProofOfAddressV1,
246+
url_name="proof_of_address",
247+
version="1",
254248
),
255-
(OTS_OWNER, us.BankCheckV1.__name__): DocumentConfig(
256-
document_class=us.BankCheckV1,
257-
endpoints=[
258-
StandardEndpoint(
259-
url_name="bank_check", version="1", api_key=self.api_key
260-
)
261-
],
249+
ConfigSpec(
250+
klass=CropperV1,
251+
url_name="cropper",
252+
version="1",
262253
),
263-
(OTS_OWNER, fr.CarteGriseV1.__name__): DocumentConfig(
264-
document_class=fr.CarteGriseV1,
265-
endpoints=[
266-
StandardEndpoint(
267-
url_name="carte_grise", version="1", api_key=self.api_key
268-
)
269-
],
254+
ConfigSpec(
255+
klass=us.BankCheckV1,
256+
url_name="bank_check",
257+
version="1",
270258
),
271-
(OTS_OWNER, ProofOfAddressV1.__name__): DocumentConfig(
272-
document_class=ProofOfAddressV1,
273-
endpoints=[
274-
StandardEndpoint(
275-
url_name="proof_of_address", version="1", api_key=self.api_key
276-
)
277-
],
259+
ConfigSpec(
260+
klass=fr.CarteGriseV1,
261+
url_name="carte_grise",
262+
version="1",
278263
),
279-
(OTS_OWNER, CropperV1.__name__): DocumentConfig(
280-
document_class=CropperV1,
281-
endpoints=[
282-
StandardEndpoint(
283-
url_name="cropper", version="1", api_key=self.api_key
284-
)
285-
],
264+
ConfigSpec(
265+
klass=fr.IdCardV1,
266+
url_name="idcard_fr",
267+
version="1",
286268
),
287-
}
269+
ConfigSpec(
270+
klass=fr.CarteVitaleV1,
271+
url_name="carte_vitale",
272+
version="1",
273+
),
274+
]
275+
for config in configs:
276+
config_key = (OTS_OWNER, config.klass.__name__)
277+
self._doc_configs[config_key] = self._standard_doc_config(
278+
config.klass, config.url_name, config.version
279+
)
280+
self._doc_configs[OTS_OWNER, FinancialV1.__name__] = DocumentConfig(
281+
document_class=FinancialV1,
282+
endpoints=[
283+
StandardEndpoint(
284+
url_name="invoices", version="3", api_key=self.api_key
285+
),
286+
StandardEndpoint(
287+
url_name="expense_receipts", version="3", api_key=self.api_key
288+
),
289+
],
290+
)
288291

289292
def add_endpoint(
290293
self,

0 commit comments

Comments
 (0)