-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocument_reader_api.py
More file actions
executable file
·58 lines (44 loc) · 2.24 KB
/
document_reader_api.py
File metadata and controls
executable file
·58 lines (44 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import Union
from regula.documentreader.webclient import ProcessResponse
from regula.documentreader.webclient.gen import ApiClient
from regula.documentreader.webclient.ext.models.recognition_response import RecognitionResponse
from regula.documentreader.webclient.gen.api import HealthcheckApi, ProcessApi
from regula.documentreader.webclient.gen.configuration import Configuration
from regula.documentreader.webclient.gen.models import ProcessRequest
Base64String = str
class DocumentReaderApi(HealthcheckApi, ProcessApi):
def __init__(self, host=None, debug=False, verify_ssl=True, api_client=None):
if api_client:
self.api_client = api_client
else:
configuration = Configuration(host=host)
configuration.debug = debug
configuration.verify_ssl = verify_ssl
self.api_client = ApiClient(configuration=configuration)
super().__init__(self.api_client)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def set_configuration(self, configuration) -> None:
self.api_client.configuration = configuration
def process(self, process_request: ProcessRequest) -> RecognitionResponse:
return RecognitionResponse(self.api_process(process_request))
def deserialize_to_recognition_response(self, content: Union[bytes, bytearray, str]) -> RecognitionResponse:
response = self.__to_response_object(content)
if hasattr(response, 'to_str'):
response_text = response.to_str()
elif hasattr(response, 'data'):
data = response.data
if isinstance(data, (bytes, bytearray)):
response_text = data.decode('utf-8')
else:
response_text = str(data)
else:
response_text = str(content) if not isinstance(content, str) else content
content_type = "application/json"
process_response = self.api_client.deserialize(response_text, "ProcessResponse", content_type)
return RecognitionResponse(process_response)
@staticmethod
def __to_response_object(content: Union[bytes, bytearray, str]):
return type(ProcessResponse.__name__, (object,), {"data": content})()