Skip to content

Commit 2a54e77

Browse files
author
FaydSpeare
committed
specify timeout per endpoint. no retry on read timeout.
1 parent c5abcbb commit 2a54e77

File tree

6 files changed

+280
-113
lines changed

6 files changed

+280
-113
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ if __name__ == "__main__":
119119
asyncio.run(main())
120120
```
121121

122+
### Network Retries
123+
124+
By default the `EDClient` will perform 3 retries when it encounters network issues. If you would like to customise this behaviour, you can pass in the `max_network_retries` param as show below:
125+
126+
Note: if the request times out, it will not be retried.
127+
128+
```python
129+
from ensembledata.api import EDClient
130+
131+
client = EDClient("API-TOKEN", max_network_retries=5)
132+
```
133+
134+
### Configure Timeout
135+
136+
If you would like control over the read timeout, you can configure this either for all request by setting `timeout` when creating the `EDClient`, or you can specify the `timeout` per request, on any of the individual methods as shown below:
137+
138+
Note: the timeout is specified in seconds.
139+
140+
```python
141+
from ensembledata.api import EDClient
142+
143+
client = EDClient("API-TOKEN", timeout=120)
144+
result = client.tiktok.user_info_from_username(username="daviddobrik", timeout=10)
145+
```
146+
122147
### Types
123148

124149
The package uses type hints, and is type checked with the latest version of `mypy`. If you experience any type checking related issues with the package, please let us know by creating an issue.

codegen/templates/client.jinja

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class {{ key }}Endpoints:
3636
{% endif %}
3737
{% endfor %}
3838
extra_params: Mapping[str, Any] | None = None,
39+
timeout: float | None = None,
3940
) -> EDResponse:
4041
params: dict[str, Any] = {
4142
{% for item in x.params %}
@@ -49,14 +50,14 @@ class {{ key }}Endpoints:
4950
if extra_params is not None:
5051
params = {**extra_params, **params}
5152
params = {k: v for k, v in params.items() if not (v is None or v is USE_DEFAULT)}
52-
return {{ "await " if async_methods else "" }}self.requester.get("{{ x.path }}", params=params{{ ", return_top_level_data=True" if x.return_top_level_data else "" }})
53+
return {{ "await " if async_methods else "" }}self.requester.get("{{ x.path }}", params=params, timeout=timeout{{ ", return_top_level_data=True" if x.return_top_level_data else "" }})
5354

5455
{% endfor %}
5556

5657
{% endfor %}
5758

5859
class ED{{ "Async" if async_methods else "" }}Client:
59-
def __init__(self, token: str, *, timeout: int = 600, max_network_retries: int = 3):
60+
def __init__(self, token: str, *, timeout: float = 600, max_network_retries: int = 3):
6061
self.requester = {{ "Async" if async_methods else "" }}Requester(
6162
token, timeout=timeout, max_network_retries=max_network_retries
6263
)
@@ -65,4 +66,4 @@ class ED{{ "Async" if async_methods else "" }}Client:
6566
{% endfor %}
6667

6768
async def request(self, uri: str, params: Mapping[str, Any] | None = None) -> EDResponse:
68-
return {{ "await " if async_methods else "" }}self.requester.get(uri, params=params or {})
69+
return {{ "await " if async_methods else "" }}self.requester.get(uri, params=params or {})

0 commit comments

Comments
 (0)