Skip to content

Commit 8386801

Browse files
committed
Add http_timeout configuration setting to configure timeout when getting document via HTTP.
1 parent 298cc5a commit 8386801

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,9 @@ If you are carefully curating your `title` properties already to ensure no dupli
165165

166166
If this option results in conflicts, you will need to manually override class names instead via the `class_overrides` option.
167167

168+
### http_timeout
169+
170+
By default, the timeout for retrieving the schema file via HTTP is 5 seconds. In case there is an error when retrieving the schema, you might try and increase this setting to a higher value.
171+
168172
[changelog.md]: CHANGELOG.md
169173
[poetry]: https://python-poetry.org/

openapi_python_client/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def _get_project_for_url_or_path( # pylint: disable=too-many-arguments
311311
custom_template_path: Optional[Path] = None,
312312
file_encoding: str = "utf-8",
313313
) -> Union[Project, GeneratorError]:
314-
data_dict = _get_document(url=url, path=path)
314+
data_dict = _get_document(url=url, path=path, timeout=config.http_timeout)
315315
if isinstance(data_dict, GeneratorError):
316316
return data_dict
317317
openapi = GeneratorData.from_dict(data_dict, config=config)
@@ -395,14 +395,14 @@ def _load_yaml_or_json(data: bytes, content_type: Optional[str]) -> Union[Dict[s
395395
return GeneratorError(header=f"Invalid YAML from provided source: {err}")
396396

397397

398-
def _get_document(*, url: Optional[str], path: Optional[Path]) -> Union[Dict[str, Any], GeneratorError]:
398+
def _get_document(*, url: Optional[str], path: Optional[Path], timeout: int) -> Union[Dict[str, Any], GeneratorError]:
399399
yaml_bytes: bytes
400400
content_type: Optional[str]
401401
if url is not None and path is not None:
402402
return GeneratorError(header="Provide URL or Path, not both.")
403403
if url is not None:
404404
try:
405-
response = httpx.get(url)
405+
response = httpx.get(url, timeout=timeout)
406406
yaml_bytes = response.content
407407
if "content-type" in response.headers:
408408
content_type = response.headers["content-type"].split(";")[0]

openapi_python_client/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Config(BaseModel):
3434
"black .",
3535
]
3636
field_prefix: str = "field_"
37+
http_timeout: int = 5
3738

3839
@staticmethod
3940
def load_from_path(path: Path) -> "Config":

tests/test___init__.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from openapi_python_client import Config, ErrorLevel, GeneratorError, Project
99

10+
default_http_timeout = Config.schema()["properties"]["http_timeout"]["default"]
11+
1012

1113
def test__get_project_for_url_or_path(mocker):
1214
data_dict = mocker.MagicMock()
@@ -17,12 +19,13 @@ def test__get_project_for_url_or_path(mocker):
1719
url = mocker.MagicMock()
1820
path = mocker.MagicMock()
1921
config = mocker.MagicMock()
22+
config.http_timeout = default_http_timeout
2023

2124
from openapi_python_client import MetaType, _get_project_for_url_or_path
2225

2326
project = _get_project_for_url_or_path(url=url, path=path, meta=MetaType.POETRY, config=config)
2427

25-
_get_document.assert_called_once_with(url=url, path=path)
28+
_get_document.assert_called_once_with(url=url, path=path, timeout=default_http_timeout)
2629
from_dict.assert_called_once_with(data_dict, config=config)
2730
_Project.assert_called_once_with(
2831
openapi=openapi, custom_template_path=None, meta=MetaType.POETRY, file_encoding="utf-8", config=config
@@ -39,12 +42,13 @@ def test__get_project_for_url_or_path_generator_error(mocker):
3942
url = mocker.MagicMock()
4043
path = mocker.MagicMock()
4144
config = mocker.MagicMock()
45+
config.http_timeout = default_http_timeout
4246

4347
from openapi_python_client import MetaType, _get_project_for_url_or_path
4448

4549
project = _get_project_for_url_or_path(url=url, path=path, meta=MetaType.POETRY, config=config)
4650

47-
_get_document.assert_called_once_with(url=url, path=path)
51+
_get_document.assert_called_once_with(url=url, path=path, timeout=default_http_timeout)
4852
from_dict.assert_called_once_with(data_dict, config=config)
4953
_Project.assert_not_called()
5054
assert project == error
@@ -62,7 +66,7 @@ def test__get_project_for_url_or_path_document_error(mocker):
6266

6367
project = _get_project_for_url_or_path(url=url, path=path, meta=MetaType.POETRY, config=Config())
6468

65-
_get_document.assert_called_once_with(url=url, path=path)
69+
_get_document.assert_called_once_with(url=url, path=path, timeout=default_http_timeout)
6670
from_dict.assert_not_called()
6771
assert project == error
6872

@@ -153,7 +157,7 @@ def test__get_document_no_url_or_path(self, mocker):
153157

154158
from openapi_python_client import _get_document
155159

156-
result = _get_document(url=None, path=None)
160+
result = _get_document(url=None, path=None, timeout=default_http_timeout)
157161

158162
assert result == GeneratorError(header="No URL or Path provided")
159163
get.assert_not_called()
@@ -167,7 +171,7 @@ def test__get_document_url_and_path(self, mocker):
167171

168172
from openapi_python_client import _get_document
169173

170-
result = _get_document(url=mocker.MagicMock(), path=mocker.MagicMock())
174+
result = _get_document(url=mocker.MagicMock(), path=mocker.MagicMock(), timeout=default_http_timeout)
171175

172176
assert result == GeneratorError(header="Provide URL or Path, not both.")
173177
get.assert_not_called()
@@ -182,10 +186,10 @@ def test__get_document_bad_url(self, mocker):
182186
from openapi_python_client import _get_document
183187

184188
url = mocker.MagicMock()
185-
result = _get_document(url=url, path=None)
189+
result = _get_document(url=url, path=None, timeout=default_http_timeout)
186190

187191
assert result == GeneratorError(header="Could not get OpenAPI document from provided URL")
188-
get.assert_called_once_with(url)
192+
get.assert_called_once_with(url, timeout=default_http_timeout)
189193
_Path.assert_not_called()
190194
loads.assert_not_called()
191195

@@ -197,9 +201,9 @@ def test__get_document_url_no_path(self, mocker):
197201
from openapi_python_client import _get_document
198202

199203
url = "test"
200-
_get_document(url=url, path=None)
204+
_get_document(url=url, path=None, timeout=default_http_timeout)
201205

202-
get.assert_called_once_with(url)
206+
get.assert_called_once_with(url, timeout=default_http_timeout)
203207
_Path.assert_not_called()
204208
loads.assert_called_once_with(get().content)
205209

@@ -211,7 +215,7 @@ def test__get_document_path_no_url(self, tmp_path, mocker):
211215

212216
from openapi_python_client import _get_document
213217

214-
_get_document(url=None, path=path)
218+
_get_document(url=None, path=path, timeout=default_http_timeout)
215219

216220
get.assert_not_called()
217221
loads.assert_called_once_with(b"some test data")
@@ -222,7 +226,7 @@ def test__get_document_bad_yaml(self, mocker, tmp_path):
222226

223227
path = tmp_path / "test.yaml"
224228
path.write_text("'")
225-
result = _get_document(url=None, path=path)
229+
result = _get_document(url=None, path=path, timeout=default_http_timeout)
226230

227231
get.assert_not_called()
228232
assert isinstance(result, GeneratorError)
@@ -241,7 +245,7 @@ class FakeResponse:
241245
from openapi_python_client import _get_document
242246

243247
url = mocker.MagicMock()
244-
result = _get_document(url=url, path=None)
248+
result = _get_document(url=url, path=None, timeout=default_http_timeout)
245249

246250
get.assert_called_once()
247251
json_loads.assert_called_once_with(FakeResponse.content.decode())
@@ -258,7 +262,7 @@ class FakeResponse:
258262
from openapi_python_client import _get_document
259263

260264
url = mocker.MagicMock()
261-
result = _get_document(url=url, path=None)
265+
result = _get_document(url=url, path=None, timeout=default_http_timeout)
262266

263267
get.assert_called_once()
264268
assert result == GeneratorError(

0 commit comments

Comments
 (0)