Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ session.mount("https://", adapter)
client = Client("token", client=session)
```

### Configuring Network Timeouts

By default, the SDK uses a 30-second timeout for all network requests to avoid hanging on slow network connections or heavy API operations.

You can easily override it passing a `timeout` explicitly during initialization to all client variants:

```python
from openapi_python_sdk import Client

client = Client(token="token", timeout=60.0) # 60 seconds
```

## Async Usage

The SDK provides `AsyncClient` and `AsyncOauthClient` for use with asynchronous frameworks like FastAPI or `aiohttp`.
Expand Down
14 changes: 12 additions & 2 deletions docs/readme-pypi.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ resp = client.request(
)
```

### Configuring Network Timeouts

By default, the SDK uses a 30-second timeout for all network requests. You can easily override it passing a `timeout` explicitly during initialization:

```python
from openapi_python_sdk.client import Client

client = Client(token="token", timeout=60.0) # 60 seconds
```

## Testing

```bash
Expand All @@ -71,7 +81,7 @@ pytest

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

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

## Links
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_sdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class AsyncClient:
Suitable for use with FastAPI, aiohttp, etc.
"""

def __init__(self, token: str, client: Any = None):
self.client = client if client is not None else httpx.AsyncClient()
def __init__(self, token: str, client: Any = None, timeout: float = 30.0):
self.client = client if client is not None else httpx.AsyncClient(timeout=timeout)
self.auth_header: str = f"Bearer {token}"
self.headers: Dict[str, str] = {
"Authorization": self.auth_header,
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_sdk/async_oauth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class AsyncOauthClient:
Suitable for use with FastAPI, aiohttp, etc.
"""

def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None):
self.client = client if client is not None else httpx.AsyncClient()
def __init__(self, username: str, apikey: str, test: bool = False, client: Any = None, timeout: float = 30.0):
self.client = client if client is not None else httpx.AsyncClient(timeout=timeout)
self.url: str = TEST_OAUTH_BASE_URL if test else OAUTH_BASE_URL
self.auth_header: str = (
"Basic " + base64.b64encode(f"{username}:{apikey}".encode("utf-8")).decode()
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Client:
Synchronous client for making authenticated requests to Openapi endpoints.
"""

def __init__(self, token: str, client: Any = None):
self.client = client if client is not None else httpx.Client()
def __init__(self, token: str, client: Any = None, timeout: float = 30.0):
self.client = client if client is not None else httpx.Client(timeout=timeout)
self.auth_header: str = f"Bearer {token}"
self.headers: Dict[str, str] = {
"Authorization": self.auth_header,
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_sdk/oauth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class OauthClient:
Synchronous client for handling Openapi authentication and token management.
"""

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