Skip to content

Commit 89674ca

Browse files
authored
✨ allow setting timeout value from env (#108)
1 parent 4979c80 commit 89674ca

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ input_doc = mindee_client.doc_from_path("/path/to/the/invoice.pdf")
2929
api_response = input_doc.parse(documents.TypeInvoiceV3)
3030

3131
# Print a brief summary of the parsed data
32-
print(str(api_response.document))
32+
print(api_response.document)
3333
```
3434

3535
#### Region-Specific Documents
@@ -46,7 +46,7 @@ input_doc = mindee_client.doc_from_path("/path/to/the/check.jpg")
4646
api_response = input_doc.parse(documents.us.TypeBankCheckV1)
4747

4848
# Print a brief summary of the parsed data
49-
print(str(api_response.document))
49+
print(api_response.document)
5050
```
5151

5252
#### Custom Document (API Builder)
@@ -67,7 +67,7 @@ api_response = mindee_client.doc_from_path(
6767
).parse(documents.TypeCustomV1, endpoint_name="wnine")
6868

6969
# Print a brief summary of the parsed data
70-
print(str(api_response.document))
70+
print(api_response.document)
7171

7272
# Iterate over all the fields in the document
7373
for field_name, field_values in api_response.document.fields.items():

mindee/documents/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict, List, Tuple, Type
22

33
from mindee.documents.base import Document
4-
from mindee.endpoints import MINDEE_API_KEY_NAME, Endpoint
4+
from mindee.endpoints import API_KEY_ENV_NAME, Endpoint
55

66
_docT = Type[Document]
77

@@ -30,7 +30,7 @@ def check_api_keys(self) -> None:
3030
f"Missing API key for '{self.document_type}',"
3131
"check your Client configuration.\n"
3232
"You can set this using the "
33-
f"'{MINDEE_API_KEY_NAME}' environment variable."
33+
f"'{API_KEY_ENV_NAME}' environment variable."
3434
)
3535
)
3636

mindee/endpoints.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
import os
2-
from typing import Dict, Optional
2+
from typing import Dict, Optional, Union
33

44
import requests
55

66
from mindee.input.sources import InputSource
77
from mindee.logger import logger
88
from mindee.versions import __version__, get_platform, python_version
99

10-
MINDEE_BASE_URL = "https://api.mindee.net/v1"
11-
MINDEE_BASE_URL_NAME = "MINDEE_BASE_URL"
12-
MINDEE_API_KEY_NAME = "MINDEE_API_KEY"
10+
API_KEY_ENV_NAME = "MINDEE_API_KEY"
11+
API_KEY_DEFAULT = ""
12+
13+
BASE_URL_ENV_NAME = "MINDEE_BASE_URL"
14+
BASE_URL_DEFAULT = "https://api.mindee.net/v1"
15+
16+
REQUEST_TIMEOUT_ENV_NAME = "MINDEE_REQUEST_TIMEOUT"
17+
TIMEOUT_DEFAULT = 120
1318

1419
PLATFORM = get_platform()
1520
USER_AGENT = f"mindee-api-python@v{__version__} python-v{python_version} {PLATFORM}"
1621

1722
OTS_OWNER = "mindee"
1823

19-
DEFAULT_TIMEOUT = 120
20-
2124

2225
class Endpoint:
2326
"""Generic API endpoint for a product."""
2427

2528
owner: str
2629
url_name: str
2730
version: str
28-
api_key: str = ""
29-
timeout: int = DEFAULT_TIMEOUT
30-
_mindee_url: str = MINDEE_BASE_URL
31+
api_key: str = API_KEY_DEFAULT
32+
_request_timeout: int = TIMEOUT_DEFAULT
33+
_base_url: str = BASE_URL_DEFAULT
3134
_url_root: str
3235

3336
def __init__(
@@ -51,10 +54,10 @@ def __init__(
5154
self.api_key = api_key
5255
else:
5356
self.set_api_key_from_env()
54-
self.set_base_url_from_env()
57+
self.set_from_env()
5558

5659
self._url_root = (
57-
f"{self._mindee_url}/products/{self.owner}/{self.url_name}/v{self.version}"
60+
f"{self._base_url}/products/{self.owner}/{self.url_name}/v{self.version}"
5861
)
5962

6063
@property
@@ -65,16 +68,29 @@ def base_headers(self) -> Dict[str, str]:
6568
"User-Agent": USER_AGENT,
6669
}
6770

68-
def set_base_url_from_env(self) -> None:
69-
"""Set the base URL from an environment variable, if present."""
70-
env_val = os.getenv(MINDEE_BASE_URL_NAME, "")
71-
if env_val:
72-
self._mindee_url = env_val
73-
logger.debug("Base URL set from environment")
71+
def set_from_env(self) -> None:
72+
"""Set various parameters from environment variables, if present."""
73+
env_vars = {
74+
BASE_URL_ENV_NAME: self.set_base_url,
75+
REQUEST_TIMEOUT_ENV_NAME: self.set_timeout,
76+
}
77+
for name, func in env_vars.items():
78+
env_val = os.getenv(name, "")
79+
if env_val:
80+
func(env_val)
81+
logger.debug("Value was set from env: %s", name)
82+
83+
def set_timeout(self, value: Union[str, int]) -> None:
84+
"""Set the timeout for all requests."""
85+
self._request_timeout = int(value)
86+
87+
def set_base_url(self, value: str) -> None:
88+
"""Set the base URL for all requests."""
89+
self._base_url = value
7490

7591
def set_api_key_from_env(self) -> None:
7692
"""Set the endpoint's API key from an environment variable, if present."""
77-
env_val = os.getenv(MINDEE_API_KEY_NAME, "")
93+
env_val = os.getenv(API_KEY_ENV_NAME, "")
7894
if env_val:
7995
self.api_key = env_val
8096
logger.debug("API key set from environment")
@@ -110,7 +126,7 @@ def predict_req_post(
110126
headers=self.base_headers,
111127
data=data,
112128
params=params,
113-
timeout=self.timeout,
129+
timeout=self._request_timeout,
114130
)
115131
return response
116132

@@ -134,7 +150,7 @@ def training_req_post(
134150
files=files,
135151
headers=self.base_headers,
136152
params=params,
137-
timeout=self.timeout,
153+
timeout=self._request_timeout,
138154
)
139155
return response
140156

@@ -156,7 +172,7 @@ def training_async_req_post(
156172
files=files,
157173
headers=self.base_headers,
158174
params=params,
159-
timeout=self.timeout,
175+
timeout=self._request_timeout,
160176
)
161177
return response
162178

@@ -175,7 +191,7 @@ def document_req_get(self, document_id: str) -> requests.Response:
175191
f"{self._url_root}/documents/{document_id}",
176192
headers=self.base_headers,
177193
params=params,
178-
timeout=self.timeout,
194+
timeout=self._request_timeout,
179195
)
180196
return response
181197

@@ -188,7 +204,7 @@ def document_req_del(self, document_id: str) -> requests.Response:
188204
response = requests.delete(
189205
f"{self._url_root}/documents/{document_id}",
190206
headers=self.base_headers,
191-
timeout=self.timeout,
207+
timeout=self._request_timeout,
192208
)
193209
return response
194210

@@ -205,7 +221,7 @@ def documents_req_get(self, page_n: int = 1) -> requests.Response:
205221
f"{self._url_root}/documents",
206222
headers=self.base_headers,
207223
params=params,
208-
timeout=self.timeout,
224+
timeout=self._request_timeout,
209225
)
210226
return response
211227

@@ -223,7 +239,7 @@ def annotations_req_post(
223239
f"{self._url_root}/documents/{document_id}/annotations",
224240
headers=self.base_headers,
225241
json=annotations,
226-
timeout=self.timeout,
242+
timeout=self._request_timeout,
227243
)
228244
return response
229245

@@ -241,7 +257,7 @@ def annotations_req_put(
241257
f"{self._url_root}/documents/{document_id}/annotations",
242258
headers=self.base_headers,
243259
json=annotations,
244-
timeout=self.timeout,
260+
timeout=self._request_timeout,
245261
)
246262
return response
247263

@@ -255,7 +271,7 @@ def annotations_req_del(self, document_id: str) -> requests.Response:
255271
response = requests.delete(
256272
f"{self._url_root}/documents/{document_id}/annotations",
257273
headers=self.base_headers,
258-
timeout=self.timeout,
274+
timeout=self._request_timeout,
259275
)
260276
return response
261277

tests/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
from mindee.endpoints import MINDEE_API_KEY_NAME
1+
from mindee.endpoints import (
2+
API_KEY_ENV_NAME,
3+
BASE_URL_ENV_NAME,
4+
REQUEST_TIMEOUT_ENV_NAME,
5+
)
26

37

48
def clear_envvars(monkeypatch):
59
"""
610
If we have envvars set, the test will pick them up and fail,
711
so let's make sure they're empty.
812
"""
9-
monkeypatch.setenv(MINDEE_API_KEY_NAME, "")
13+
monkeypatch.setenv(API_KEY_ENV_NAME, "")
14+
monkeypatch.setenv(BASE_URL_ENV_NAME, "")
15+
monkeypatch.setenv(REQUEST_TIMEOUT_ENV_NAME, "")
1016

1117

1218
def dummy_envvars(monkeypatch):
1319
"""
1420
Set all API keys to 'dummy'.
1521
"""
16-
monkeypatch.setenv(MINDEE_API_KEY_NAME, "dummy")
22+
monkeypatch.setenv(API_KEY_ENV_NAME, "dummy")

0 commit comments

Comments
 (0)