Skip to content

Commit 2250967

Browse files
committed
Merge pull request #541 from reef-technologies/fix-api-v2
Fix incorrect reliance on v3 abstractions in apiver v2
2 parents 22fb916 + d2f7e5d commit 2250967

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed

b2sdk/_internal/raw_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def _post_json(self, base_url: str, endpoint: str, auth: str, **params) -> JSON:
553553
:param args: the rest of the parameters are passed to b2
554554
:return: the decoded JSON response
555555
"""
556-
url = f'{base_url}/b2api/{API_VERSION}/{endpoint}'
556+
url = f'{base_url}/b2api/{self.API_VERSION}/{endpoint}'
557557
headers = {'Authorization': auth}
558558
return self.b2_http.post_json_return_json(url, headers, params)
559559

b2sdk/v2/api.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from .file_version import FileVersionFactory
2626
from .large_file import LargeFileServices
2727
from .application_key import FullApplicationKey, ApplicationKey, BaseApplicationKey
28+
from .account_info import AbstractAccountInfo
29+
from .api_config import DEFAULT_HTTP_API_CONFIG, B2HttpApiConfig
2830

2931

3032
class Services(v3.Services):
@@ -48,10 +50,29 @@ class B2Api(v3.B2Api):
4850
API_VERSION = RAW_API_VERSION
4951

5052
# Legacy init in case something depends on max_workers defaults = 10
51-
def __init__(self, *args, **kwargs):
52-
kwargs.setdefault('max_upload_workers', 10)
53-
kwargs.setdefault('max_copy_workers', 10)
54-
super().__init__(*args, **kwargs)
53+
def __init__(
54+
self,
55+
account_info: AbstractAccountInfo | None = None,
56+
cache: v3.AbstractCache | None = None,
57+
max_upload_workers: int | None = 10,
58+
max_copy_workers: int | None = 10,
59+
api_config: B2HttpApiConfig = DEFAULT_HTTP_API_CONFIG,
60+
max_download_workers: int | None = None,
61+
save_to_buffer_size: int | None = None,
62+
check_download_hash: bool = True,
63+
max_download_streams_per_file: int | None = None,
64+
):
65+
super().__init__(
66+
account_info=account_info,
67+
cache=cache,
68+
max_upload_workers=max_upload_workers,
69+
max_copy_workers=max_copy_workers,
70+
api_config=api_config,
71+
max_download_workers=max_download_workers,
72+
save_to_buffer_size=save_to_buffer_size,
73+
check_download_hash=check_download_hash,
74+
max_download_streams_per_file=max_download_streams_per_file,
75+
)
5576

5677
def get_bucket_by_id(self, bucket_id: str) -> v3.Bucket:
5778
try:

b2sdk/v2/api_config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/v2/api_config.py
4+
#
5+
# Copyright 2025 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from b2sdk import v3
11+
from .raw_api import B2RawHTTPApi
12+
13+
14+
class B2HttpApiConfig(v3.B2HttpApiConfig):
15+
DEFAULT_RAW_API_CLASS = B2RawHTTPApi
16+
17+
18+
DEFAULT_HTTP_API_CONFIG = B2HttpApiConfig()

b2sdk/v2/session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
from b2sdk import v3
1313
from .b2http import B2Http
1414

15+
from .account_info import SqliteAccountInfo
1516
from ._compat import _file_infos_rename
16-
from .._internal import api_config as _api_config
17+
from . import api_config as _api_config
1718
from .._internal import cache as _cache
1819
from .._internal.account_info import abstract as _abstract
1920

2021

2122
# Override to use legacy B2Http
2223
class B2Session(v3.B2Session):
24+
SQLITE_ACCOUNT_INFO_CLASS = staticmethod(SqliteAccountInfo)
2325
B2HTTP_CLASS = staticmethod(B2Http)
2426

2527
def __init__(
@@ -31,6 +33,7 @@ def __init__(
3133
if account_info is not None and cache is None:
3234
# preserve legacy behavior https://github.com/Backblaze/b2-sdk-python/issues/497#issuecomment-2147461352
3335
cache = _cache.DummyCache()
36+
3437
super().__init__(account_info, cache, api_config)
3538

3639
def create_key(

changelog.d/540.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect reliance on v3 abstractions in apiver v2.

test/unit/v2/test_session.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from apiver_deps_exception import Unauthorized
1616

1717
from b2sdk import v3
18-
from b2sdk.v2 import B2Session
18+
from b2sdk.v2 import B2Http, B2RawHTTPApi, B2Session
1919
from test.helpers import patch_bind_params
2020

2121
from ..account_info.fixtures import * # noqa
@@ -74,6 +74,20 @@ def dummy_session():
7474
return B2Session()
7575

7676

77+
@pytest.mark.xdist_group('dummy_session')
78+
def test_session__default_classes_v2():
79+
session = B2Session()
80+
81+
assert isinstance(session.raw_api, B2RawHTTPApi), 'Expected v2.B2RawHTTPApi, got %s' % type(
82+
session.raw_api
83+
)
84+
85+
assert isinstance(session.raw_api.b2_http, B2Http), 'Expected v2.B2Http, got %s' % type(
86+
session.raw_api.b2_http
87+
)
88+
89+
90+
@pytest.mark.xdist_group('dummy_session')
7791
def test_session__upload_file__supports_file_infos(dummy_session, file_info):
7892
"""Test v2.B2Session.upload_file support of deprecated file_infos param"""
7993
with patch_bind_params(v3.B2Session, 'upload_file') as mock_method, pytest.warns(

test/unit/v_all/test_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from apiver_deps import (
1515
B2Api,
1616
B2HttpApiConfig,
17+
B2Session,
1718
Bucket,
1819
EncryptionMode,
1920
EncryptionSetting,
@@ -73,6 +74,8 @@ def test_api_initialization(self, kwargs, _raw_api_class):
7374

7475
self.api = B2Api(self.account_info, self.cache, api_config=api_config, **kwargs)
7576

77+
assert isinstance(self.api.session, B2Session) # ensure correct apiver session is used
78+
7679
assert self.api.account_info is self.account_info
7780
assert self.api.api_config is api_config
7881
assert self.api.cache is self.cache

0 commit comments

Comments
 (0)