Skip to content

Commit 32362e2

Browse files
committed
update code
1 parent c898bbf commit 32362e2

3 files changed

Lines changed: 113 additions & 38 deletions

File tree

‎docs/config-sharing-implementation.md‎

Lines changed: 107 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,38 @@
22

33
This document describes the implementation of configuration sharing between the legacy `boxsdk` package and the new auto-generated `box_sdk_gen` package.
44

5+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
6+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7+
8+
- [Configuration Sharing Implementation](#configuration-sharing-implementation)
9+
- [Overview](#overview)
10+
- [Implementation Components](#implementation-components)
11+
- [Token Storage Adapter (`boxsdk/util/token_storage_adapter.py`)](#token-storage-adapter-boxsdkutiltoken_storage_adapterpy)
12+
- [Client Methods](#client-methods)
13+
- [`get_sdk_gen_client(auth_options=None, network_options=None)`](#get_sdk_gen_clientauth_optionsnone-network_optionsnone)
14+
- [`get_sdk_gen_authentication(token_storage=None)`](#get_sdk_gen_authenticationtoken_storagenone)
15+
- [`get_sdk_gen_network_session(**options)`](#get_sdk_gen_network_sessionoptions)
16+
- [Usage Examples](#usage-examples)
17+
- [Developer Token](#developer-token)
18+
- [OAuth 2.0 with Token Refresh](#oauth-20-with-token-refresh)
19+
- [JWT Authentication](#jwt-authentication)
20+
- [CCG Authentication](#ccg-authentication)
21+
- [Custom Network Configuration](#custom-network-configuration)
22+
- [Implementation Details](#implementation-details)
23+
- [Token Storage Adapter](#token-storage-adapter)
24+
- [Authentication Conversion](#authentication-conversion)
25+
- [Network Configuration Conversion](#network-configuration-conversion)
26+
27+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
28+
29+
530
## Overview
631

732
The configuration sharing feature allows developers to seamlessly migrate from the legacy SDK to the generated SDK by automatically extracting and converting authentication and network configuration from a legacy client to a generated client.
833

934
## Implementation Components
1035

11-
### 1. Token Storage Adapter (`boxsdk/util/token_storage_adapter.py`)
36+
### Token Storage Adapter (`boxsdk/util/token_storage_adapter.py`)
1237

1338
The `LegacyTokenStorageAdapter` class bridges the gap between legacy OAuth2 token storage mechanisms (callbacks) and the generated SDK's `TokenStorage` interface.
1439

@@ -17,10 +42,81 @@ The `LegacyTokenStorageAdapter` class bridges the gap between legacy OAuth2 toke
1742
- Supports both read and write operations
1843
- Handles token storage callbacks from legacy OAuth2 implementations
1944

20-
### 2. Client Methods
45+
**Example:**
46+
```python
47+
from boxsdk.auth.oauth2 import OAuth2
48+
from boxsdk.util.token_storage_adapter import LegacyTokenStorageAdapter
49+
50+
from box_sdk_gen.box.oauth import BoxOAuth, OAuthConfig
51+
52+
# Legacy OAuth2 with a token persistence callback
53+
stored_tokens = {"access": None, "refresh": None}
54+
55+
56+
def store_tokens(access_token, refresh_token):
57+
stored_tokens["access"] = access_token
58+
stored_tokens["refresh"] = refresh_token
59+
60+
61+
legacy_auth = OAuth2(client_id="...", client_secret="...", store_tokens=store_tokens)
62+
63+
# Bridge legacy token storage to the generated SDK TokenStorage interface
64+
token_storage = LegacyTokenStorageAdapter(
65+
get_tokens=lambda: legacy_auth._get_tokens(),
66+
store_tokens=lambda access_token, refresh_token: legacy_auth._store_tokens(
67+
access_token, refresh_token
68+
),
69+
)
70+
71+
gen_auth = BoxOAuth(
72+
config=OAuthConfig(
73+
client_id="...",
74+
client_secret="...",
75+
token_storage=token_storage,
76+
)
77+
)
78+
```
79+
80+
### Client Methods
2181

2282
Three new methods have been added to the `Client` class in `boxsdk/client/client.py`:
2383

84+
#### `get_sdk_gen_client(auth_options=None, network_options=None)`
85+
86+
Creates a fully configured generated SDK client from the legacy client. This is the main convenience method that combines `get_sdk_gen_authentication()` and `get_sdk_gen_network_session()`.
87+
88+
**Parameters:**
89+
- `auth_options` (optional): Dictionary with authentication options
90+
- `token_storage`: Custom `TokenStorage` instance
91+
- `network_options` (optional): Dictionary with network options
92+
- `network_client`: Custom `NetworkClient` instance
93+
- `retry_strategy`: Custom `RetryStrategy` instance
94+
- `data_sanitizer`: Custom `DataSanitizer` instance
95+
- `additional_headers`: Dictionary of additional HTTP headers
96+
97+
**Returns:**
98+
- `BoxClient` instance from `box_sdk_gen`, fully configured with shared settings
99+
100+
**Example:**
101+
```python
102+
from boxsdk import Client
103+
from boxsdk.auth import OAuth2
104+
105+
# Legacy client setup
106+
legacy_auth = OAuth2(client_id="...", client_secret="...")
107+
legacy_client = Client(legacy_auth)
108+
109+
# Get generated SDK client with one line
110+
gen_client = legacy_client.get_sdk_gen_client()
111+
112+
# Use generated client for new features
113+
user = gen_client.users.get_user_by_id("me")
114+
folders = gen_client.folders.get_folder_items("0")
115+
116+
# Legacy client still works for existing code
117+
legacy_user = legacy_client.user().get()
118+
```
119+
24120
#### `get_sdk_gen_authentication(token_storage=None)`
25121

26122
Extracts authentication configuration from the legacy client and converts it to a generated SDK `Authentication` object.
@@ -41,13 +137,15 @@ Extracts authentication configuration from the legacy client and converts it to
41137
```python
42138
from boxsdk import Client
43139
from boxsdk.auth import OAuth2
140+
from box_sdk_gen.client import BoxClient
44141

45142
# Legacy client
46143
legacy_auth = OAuth2(client_id="...", client_secret="...")
47144
legacy_client = Client(legacy_auth)
48145

49146
# Get generated SDK authentication
50147
gen_auth = legacy_client.get_sdk_gen_authentication()
148+
gen_client = BoxClient(auth=gen_auth)
51149
```
52150

53151
#### `get_sdk_gen_network_session(**options)`
@@ -69,47 +167,20 @@ Extracts network configuration from the legacy client and converts it to a gener
69167
- Retry strategy: Extracted from `API.MAX_RETRY_ATTEMPTS` and session retry settings
70168
- Custom headers: Merged from session default headers and additional headers
71169

72-
**Example:**
73-
```python
74-
network_session = legacy_client.get_sdk_gen_network_session(
75-
additional_headers={"X-Custom-Header": "value"}
76-
)
77-
```
78-
79-
#### `get_sdk_gen_client(auth_options=None, network_options=None)`
80-
81-
Creates a fully configured generated SDK client from the legacy client. This is the main convenience method that combines `get_sdk_gen_authentication()` and `get_sdk_gen_network_session()`.
82-
83-
**Parameters:**
84-
- `auth_options` (optional): Dictionary with authentication options
85-
- `token_storage`: Custom `TokenStorage` instance
86-
- `network_options` (optional): Dictionary with network options
87-
- `network_client`: Custom `NetworkClient` instance
88-
- `retry_strategy`: Custom `RetryStrategy` instance
89-
- `data_sanitizer`: Custom `DataSanitizer` instance
90-
- `additional_headers`: Dictionary of additional HTTP headers
91-
92-
**Returns:**
93-
- `BoxClient` instance from `box_sdk_gen`, fully configured with shared settings
94-
95170
**Example:**
96171
```python
97172
from boxsdk import Client
98-
from boxsdk.auth import OAuth2
173+
from box_sdk_gen.client import BoxClient
174+
from box_sdk_gen.box.developer_token_auth import BoxDeveloperTokenAuth
99175

100-
# Legacy client setup
101-
legacy_auth = OAuth2(client_id="...", client_secret="...")
102176
legacy_client = Client(legacy_auth)
103177

104-
# Get generated SDK client with one line
105-
gen_client = legacy_client.get_sdk_gen_client()
106-
107-
# Use generated client for new features
108-
user = gen_client.users.get_user_by_id("me")
109-
folders = gen_client.folders.get_folder_items("0")
178+
network_session = legacy_client.get_sdk_gen_network_session(
179+
additional_headers={"X-Custom-Header": "value"}
180+
)
110181

111-
# Legacy client still works for existing code
112-
legacy_user = legacy_client.user().get()
182+
gen_auth = BoxDeveloperTokenAuth(token="...")
183+
gen_client = BoxClient(auth=gen_auth, network_session=network_session)
113184
```
114185

115186
## Usage Examples

‎test/boxsdk/integration/test_config_sharing.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ def test_get_authentication_with_custom_token_storage(self, mock_box_session):
282282

283283
custom_storage = InMemoryTokenStorage()
284284
legacy_client = Client(legacy_auth, session=mock_box_session)
285-
gen_auth = legacy_client.get_sdk_gen_authentication(token_storage=custom_storage)
285+
gen_auth = legacy_client.get_sdk_gen_authentication(
286+
token_storage=custom_storage
287+
)
286288

287289
assert isinstance(gen_auth, BoxOAuth)
288290
assert gen_auth.config.token_storage is custom_storage

‎test/boxsdk/unit/client/test_config_sharing.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ def test_get_network_session_with_custom_retry_strategy(self, mock_box_session):
269269
client = Client(auth, session=mock_box_session)
270270

271271
custom_retry = BoxRetryStrategy(max_attempts=10, retry_base_interval=2.0)
272-
network_session = client.get_sdk_gen_network_session(retry_strategy=custom_retry)
272+
network_session = client.get_sdk_gen_network_session(
273+
retry_strategy=custom_retry
274+
)
273275

274276
assert network_session.retry_strategy is custom_retry
275277
assert network_session.retry_strategy.max_attempts == 10

0 commit comments

Comments
 (0)