Skip to content

Commit c898bbf

Browse files
committed
update code
1 parent 59af33c commit c898bbf

5 files changed

Lines changed: 58 additions & 67 deletions

File tree

‎boxsdk/client/client.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ def get_ai_agent_default_config(
20152015
response_object=box_response.json(),
20162016
)
20172017

2018-
def get_authentication(
2018+
def get_sdk_gen_authentication(
20192019
self, *, token_storage: Optional[TokenStorage] = None
20202020
) -> Authentication:
20212021
"""
@@ -2038,7 +2038,7 @@ def get_authentication(
20382038
"""
20392039
return config_adapter.get_authentication(self, token_storage=token_storage)
20402040

2041-
def get_network_session(
2041+
def get_sdk_gen_network_session(
20422042
self,
20432043
*,
20442044
network_client=None,
@@ -2071,6 +2071,32 @@ def get_network_session(
20712071
additional_headers=additional_headers,
20722072
)
20732073

2074+
def get_authentication(
2075+
self, *, token_storage: Optional[TokenStorage] = None
2076+
) -> Authentication:
2077+
"""
2078+
Backward-compatible alias for `get_sdk_gen_authentication()`.
2079+
"""
2080+
return self.get_sdk_gen_authentication(token_storage=token_storage)
2081+
2082+
def get_network_session(
2083+
self,
2084+
*,
2085+
network_client=None,
2086+
retry_strategy=None,
2087+
data_sanitizer=None,
2088+
additional_headers=None,
2089+
) -> NetworkSession:
2090+
"""
2091+
Backward-compatible alias for `get_sdk_gen_network_session()`.
2092+
"""
2093+
return self.get_sdk_gen_network_session(
2094+
network_client=network_client,
2095+
retry_strategy=retry_strategy,
2096+
data_sanitizer=data_sanitizer,
2097+
additional_headers=additional_headers,
2098+
)
2099+
20742100
def get_sdk_gen_client(
20752101
self,
20762102
*,

‎boxsdk/client/config_adapter.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from box_sdk_gen.networking.base_urls import BaseUrls
2626
from box_sdk_gen.networking.network import NetworkSession
2727
from box_sdk_gen.networking.retries import BoxRetryStrategy
28-
from box_sdk_gen.schemas.access_token import AccessToken
2928

3029

3130
def get_authentication(

‎docs/config-sharing-implementation.md‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The `LegacyTokenStorageAdapter` class bridges the gap between legacy OAuth2 toke
2121

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

24-
#### `get_authentication(token_storage=None)`
24+
#### `get_sdk_gen_authentication(token_storage=None)`
2525

2626
Extracts authentication configuration from the legacy client and converts it to a generated SDK `Authentication` object.
2727

@@ -47,10 +47,10 @@ legacy_auth = OAuth2(client_id="...", client_secret="...")
4747
legacy_client = Client(legacy_auth)
4848

4949
# Get generated SDK authentication
50-
gen_auth = legacy_client.get_authentication()
50+
gen_auth = legacy_client.get_sdk_gen_authentication()
5151
```
5252

53-
#### `get_network_session(**options)`
53+
#### `get_sdk_gen_network_session(**options)`
5454

5555
Extracts network configuration from the legacy client and converts it to a generated SDK `NetworkSession` object.
5656

@@ -71,14 +71,14 @@ Extracts network configuration from the legacy client and converts it to a gener
7171

7272
**Example:**
7373
```python
74-
network_session = legacy_client.get_network_session(
74+
network_session = legacy_client.get_sdk_gen_network_session(
7575
additional_headers={"X-Custom-Header": "value"}
7676
)
7777
```
7878

7979
#### `get_sdk_gen_client(auth_options=None, network_options=None)`
8080

81-
Creates a fully configured generated SDK client from the legacy client. This is the main convenience method that combines `get_authentication()` and `get_network_session()`.
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()`.
8282

8383
**Parameters:**
8484
- `auth_options` (optional): Dictionary with authentication options
@@ -237,37 +237,3 @@ Network settings are extracted from:
237237
- `Session.proxy_config`: Proxy settings
238238
- `Session._default_headers`: Custom headers
239239
- `API.MAX_RETRY_ATTEMPTS`: Retry configuration
240-
241-
## Limitations
242-
243-
1. **JWT Private Key**: If the JWT private key was originally encrypted, the passphrase cannot be extracted from the normalized `RSAPrivateKey` object. The key will be serialized unencrypted.
244-
245-
2. **Token Expiry**: The legacy SDK doesn't always track token expiry times, so the adapter uses a default value (3600 seconds) when converting to `AccessToken`.
246-
247-
3. **Custom Token Storage**: If using custom token storage callbacks in the legacy SDK, ensure they're thread-safe if both clients will be used concurrently.
248-
249-
## Error Handling
250-
251-
The implementation raises:
252-
253-
- `ValueError`: for unsupported auth types or missing credentials
254-
255-
Note: Since `boxsdk` and `box_sdk_gen` are always installed together, import errors for `box_sdk_gen` should not occur in practice.
256-
257-
## Testing
258-
259-
Unit tests should cover:
260-
- Token storage adapter conversion
261-
- Each authentication type conversion
262-
- Network configuration extraction
263-
- Error cases (missing credentials, unsupported types)
264-
- Integration tests for `get_sdk_gen_client()`
265-
266-
## Future Enhancements
267-
268-
Potential improvements:
269-
1. Support for additional authentication types
270-
2. Better token expiry tracking
271-
3. Support for encrypted JWT keys with passphrase extraction
272-
4. More comprehensive network configuration mapping
273-

‎test/boxsdk/integration/test_config_sharing.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_parallel_client_usage(self, mock_box_session):
270270
assert gen_client.network_session is not None
271271

272272
def test_get_authentication_with_custom_token_storage(self, mock_box_session):
273-
"""Test get_authentication() with custom token storage."""
273+
"""Test get_sdk_gen_authentication() with custom token storage."""
274274
from box_sdk_gen.box.token_storage import InMemoryTokenStorage
275275

276276
legacy_auth = OAuth2(
@@ -282,13 +282,13 @@ 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_authentication(token_storage=custom_storage)
285+
gen_auth = legacy_client.get_sdk_gen_authentication(token_storage=custom_storage)
286286

287287
assert isinstance(gen_auth, BoxOAuth)
288288
assert gen_auth.config.token_storage is custom_storage
289289

290290
def test_get_network_session_with_custom_options(self, mock_box_session):
291-
"""Test get_network_session() with custom options."""
291+
"""Test get_sdk_gen_network_session() with custom options."""
292292
from box_sdk_gen.networking.retries import BoxRetryStrategy
293293

294294
legacy_auth = DeveloperTokenAuth(get_new_token_callback=lambda: 'token')
@@ -297,7 +297,7 @@ def test_get_network_session_with_custom_options(self, mock_box_session):
297297
custom_retry = BoxRetryStrategy(max_attempts=20, retry_base_interval=2.5)
298298
custom_headers = {'X-Custom': 'value'}
299299

300-
network_session = legacy_client.get_network_session(
300+
network_session = legacy_client.get_sdk_gen_network_session(
301301
retry_strategy=custom_retry, additional_headers=custom_headers
302302
)
303303

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Unit tests for configuration sharing methods in Client class.
33
4-
Tests get_authentication(), get_network_session(), and get_sdk_gen_client() methods.
4+
Tests get_sdk_gen_authentication(), get_sdk_gen_network_session(), and get_sdk_gen_client() methods.
55
"""
66

77
from unittest.mock import Mock
@@ -22,16 +22,16 @@
2222
from box_sdk_gen.client import BoxClient
2323

2424

25-
class TestGetAuthentication:
26-
"""Test cases for get_authentication() method."""
25+
class TestGetSdkGenAuthentication:
26+
"""Test cases for get_sdk_gen_authentication() method."""
2727

2828
def test_get_authentication_developer_token(self, mock_box_session):
2929
"""Test converting DeveloperTokenAuth to BoxDeveloperTokenAuth."""
3030
token = 'dev_token_123'
3131
auth = DeveloperTokenAuth(get_new_token_callback=lambda: token)
3232

3333
client = Client(auth, session=mock_box_session)
34-
gen_auth = client.get_authentication()
34+
gen_auth = client.get_sdk_gen_authentication()
3535

3636
assert isinstance(gen_auth, BoxDeveloperTokenAuth)
3737
assert gen_auth.token == token
@@ -45,7 +45,7 @@ def test_get_authentication_developer_token_missing_token(self, mock_box_session
4545
client = Client(auth, session=mock_box_session)
4646

4747
with pytest.raises(ValueError, match="Developer token is not available"):
48-
client.get_authentication()
48+
client.get_sdk_gen_authentication()
4949

5050
def test_get_authentication_oauth2(self, mock_box_session):
5151
"""Test converting OAuth2 to BoxOAuth."""
@@ -62,7 +62,7 @@ def test_get_authentication_oauth2(self, mock_box_session):
6262
)
6363

6464
client = Client(auth, session=mock_box_session)
65-
gen_auth = client.get_authentication()
65+
gen_auth = client.get_sdk_gen_authentication()
6666

6767
assert isinstance(gen_auth, BoxOAuth)
6868
assert gen_auth.config.client_id == client_id
@@ -77,7 +77,7 @@ def test_get_authentication_oauth2_missing_credentials(self, mock_box_session):
7777
with pytest.raises(
7878
ValueError, match="OAuth2 client_id and client_secret are required"
7979
):
80-
client.get_authentication()
80+
client.get_sdk_gen_authentication()
8181

8282
def test_get_authentication_oauth2_with_custom_token_storage(
8383
self, mock_box_session
@@ -94,7 +94,7 @@ def test_get_authentication_oauth2_with_custom_token_storage(
9494

9595
custom_storage = InMemoryTokenStorage()
9696
client = Client(auth, session=mock_box_session)
97-
gen_auth = client.get_authentication(token_storage=custom_storage)
97+
gen_auth = client.get_sdk_gen_authentication(token_storage=custom_storage)
9898

9999
assert isinstance(gen_auth, BoxOAuth)
100100
assert gen_auth.config.token_storage is custom_storage
@@ -124,7 +124,7 @@ def test_get_authentication_jwt(self, mock_box_session):
124124
)
125125

126126
client = Client(auth, session=mock_box_session)
127-
gen_auth = client.get_authentication()
127+
gen_auth = client.get_sdk_gen_authentication()
128128

129129
assert isinstance(gen_auth, BoxJWTAuth)
130130
assert gen_auth.config.client_id == client_id
@@ -154,7 +154,7 @@ def test_get_authentication_jwt_missing_credentials(self, mock_box_session):
154154
client = Client(auth, session=mock_box_session)
155155

156156
with pytest.raises(ValueError, match="JWT authentication requires"):
157-
client.get_authentication()
157+
client.get_sdk_gen_authentication()
158158

159159
def test_get_authentication_ccg(self, mock_box_session):
160160
"""Test converting CCGAuth to BoxCCGAuth."""
@@ -169,7 +169,7 @@ def test_get_authentication_ccg(self, mock_box_session):
169169
)
170170

171171
client = Client(auth, session=mock_box_session)
172-
gen_auth = client.get_authentication()
172+
gen_auth = client.get_sdk_gen_authentication()
173173

174174
assert isinstance(gen_auth, BoxCCGAuth)
175175
assert gen_auth.config.client_id == client_id
@@ -183,7 +183,7 @@ def test_get_authentication_ccg_missing_credentials(self, mock_box_session):
183183
client = Client(auth, session=mock_box_session)
184184

185185
with pytest.raises(ValueError, match="CCG authentication requires"):
186-
client.get_authentication()
186+
client.get_sdk_gen_authentication()
187187

188188
def test_get_authentication_unsupported_type(self, mock_box_session):
189189
"""Test that unsupported auth type raises ValueError."""
@@ -194,18 +194,18 @@ def test_get_authentication_unsupported_type(self, mock_box_session):
194194
client = Client(mock_auth, session=mock_box_session)
195195

196196
with pytest.raises(ValueError, match="Unsupported authentication type"):
197-
client.get_authentication()
197+
client.get_sdk_gen_authentication()
198198

199199

200-
class TestGetNetworkSession:
201-
"""Test cases for get_network_session() method."""
200+
class TestGetSdkGenNetworkSession:
201+
"""Test cases for get_sdk_gen_network_session() method."""
202202

203203
def test_get_network_session_default(self, mock_box_session):
204204
"""Test extracting network session with default settings."""
205205
auth = Mock(OAuth2)
206206
client = Client(auth, session=mock_box_session)
207207

208-
network_session = client.get_network_session()
208+
network_session = client.get_sdk_gen_network_session()
209209

210210
assert isinstance(network_session, NetworkSession)
211211
assert isinstance(network_session.base_urls, BaseUrls)
@@ -216,7 +216,7 @@ def test_get_network_session_with_custom_headers(self, mock_box_session):
216216
client = Client(auth, session=mock_box_session)
217217

218218
additional_headers = {'X-Custom-Header': 'custom_value'}
219-
network_session = client.get_network_session(
219+
network_session = client.get_sdk_gen_network_session(
220220
additional_headers=additional_headers
221221
)
222222

@@ -232,7 +232,7 @@ def test_get_network_session_with_proxy(self, mock_box_session):
232232
auth = Mock(OAuth2)
233233
client = Client(auth, session=mock_box_session)
234234

235-
network_session = client.get_network_session()
235+
network_session = client.get_sdk_gen_network_session()
236236

237237
# Proxy URL should be set
238238
assert network_session.proxy_url is not None
@@ -250,7 +250,7 @@ def test_get_network_session_with_authenticated_proxy(self, mock_box_session):
250250
auth = Mock(OAuth2)
251251
client = Client(auth, session=mock_box_session)
252252

253-
network_session = client.get_network_session()
253+
network_session = client.get_sdk_gen_network_session()
254254

255255
# Proxy URL should include authentication
256256
assert network_session.proxy_url is not None
@@ -269,7 +269,7 @@ 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_network_session(retry_strategy=custom_retry)
272+
network_session = client.get_sdk_gen_network_session(retry_strategy=custom_retry)
273273

274274
assert network_session.retry_strategy is custom_retry
275275
assert network_session.retry_strategy.max_attempts == 10
@@ -286,7 +286,7 @@ def test_get_network_session_base_urls(self, mock_box_session):
286286
auth = Mock(OAuth2)
287287
client = Client(auth, session=mock_box_session)
288288

289-
network_session = client.get_network_session()
289+
network_session = client.get_sdk_gen_network_session()
290290

291291
# URLs should have version suffix removed
292292
assert 'custom.api.box.com' in network_session.base_urls.base_url

0 commit comments

Comments
 (0)