Skip to content

Commit 7869447

Browse files
Merge pull request #19 from Deadpool2000/enhance-18
Add configurable network timeouts to all HTTP clients
2 parents 512e43d + 0b10211 commit 7869447

6 files changed

Lines changed: 32 additions & 10 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ session.mount("https://", adapter)
132132
client = Client("token", client=session)
133133
```
134134

135+
### Configuring Network Timeouts
136+
137+
By default, the SDK uses a 30-second timeout for all network requests to avoid hanging on slow network connections or heavy API operations.
138+
139+
You can easily override it passing a `timeout` explicitly during initialization to all client variants:
140+
141+
```python
142+
from openapi_python_sdk import Client
143+
144+
client = Client(token="token", timeout=60.0) # 60 seconds
145+
```
146+
135147
## Async Usage
136148

137149
The SDK provides `AsyncClient` and `AsyncOauthClient` for use with asynchronous frameworks like FastAPI or `aiohttp`.

docs/readme-pypi.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ resp = client.request(
6060
)
6161
```
6262

63+
### Configuring Network Timeouts
64+
65+
By default, the SDK uses a 30-second timeout for all network requests. You can easily override it passing a `timeout` explicitly during initialization:
66+
67+
```python
68+
from openapi_python_sdk.client import Client
69+
70+
client = Client(token="token", timeout=60.0) # 60 seconds
71+
```
72+
6373
## Testing
6474

6575
```bash
@@ -71,7 +81,7 @@ pytest
7181

7282
| Method | Description |
7383
|---|---|
74-
| `OauthClient(username, apikey, test=False)` | Initialize the OAuth client. Set `test=True` for sandbox. |
84+
| `OauthClient(username, apikey, test=False, timeout=30.0)` | Initialize the OAuth client. Set `test=True` for sandbox. |
7585
| `create_token(scopes, ttl)` | Create a bearer token for the given scopes and TTL (seconds). |
7686
| `get_token(scope)` | Retrieve an existing token by scope. |
7787
| `delete_token(id)` | Revoke a token by ID. |
@@ -82,7 +92,7 @@ pytest
8292

8393
| Method | Description |
8494
|---|---|
85-
| `Client(token)` | Initialize the client with a bearer token. |
95+
| `Client(token, timeout=30.0)` | Initialize the client with a bearer token. |
8696
| `request(method, url, payload, params)` | Execute an HTTP request against any Openapi endpoint. |
8797

8898
## Links

openapi_python_sdk/async_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class AsyncClient:
1010
Suitable for use with FastAPI, aiohttp, etc.
1111
"""
1212

13-
def __init__(self, token: str, client: Any = None):
14-
self.client = client if client is not None else httpx.AsyncClient()
13+
def __init__(self, token: str, client: Any = None, timeout: float = 30.0):
14+
self.client = client if client is not None else httpx.AsyncClient(timeout=timeout)
1515
self.auth_header: str = f"Bearer {token}"
1616
self.headers: Dict[str, str] = {
1717
"Authorization": self.auth_header,

openapi_python_sdk/async_oauth_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class AsyncOauthClient:
1212
Suitable for use with FastAPI, aiohttp, etc.
1313
"""
1414

15-
def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None):
16-
self.client = client if client is not None else httpx.AsyncClient()
15+
def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None, timeout: float = 30.0):
16+
self.client = client if client is not None else httpx.AsyncClient(timeout=timeout)
1717
self.url: str = TEST_OAUTH_BASE_URL if test else OAUTH_BASE_URL
1818
self.auth_header: str = (
1919
"Basic " + base64.b64encode(f"{username}:{apikey}".encode("utf-8")).decode()

openapi_python_sdk/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Client:
1414
Synchronous client for making authenticated requests to Openapi endpoints.
1515
"""
1616

17-
def __init__(self, token: str, client: Any = None):
18-
self.client = client if client is not None else httpx.Client()
17+
def __init__(self, token: str, client: Any = None, timeout: float = 30.0):
18+
self.client = client if client is not None else httpx.Client(timeout=timeout)
1919
self.auth_header: str = f"Bearer {token}"
2020
self.headers: Dict[str, str] = {
2121
"Authorization": self.auth_header,

openapi_python_sdk/oauth_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class OauthClient:
1212
Synchronous client for handling Openapi authentication and token management.
1313
"""
1414

15-
def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None):
16-
self.client = client if client is not None else httpx.Client()
15+
def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None, timeout: float = 30.0):
16+
self.client = client if client is not None else httpx.Client(timeout=timeout)
1717
self.url: str = TEST_OAUTH_BASE_URL if test else OAUTH_BASE_URL
1818
self.auth_header: str = (
1919
"Basic " + base64.b64encode(f"{username}:{apikey}".encode("utf-8")).decode()

0 commit comments

Comments
 (0)