Skip to content

Commit

Permalink
create cache by default in B2Api (resolves Backblaze#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Jun 14, 2024
1 parent 9e2e8ee commit 9f3b2de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
7 changes: 4 additions & 3 deletions b2sdk/_internal/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ def __init__(
self.raw_api = api_config.raw_api_class(self.B2HTTP_CLASS(api_config))
if account_info is None:
account_info = self.SQLITE_ACCOUNT_INFO_CLASS()
if cache is None:
cache = AuthInfoCache(account_info)
if cache is None:
cache = DummyCache()
if account_info:
cache = AuthInfoCache(account_info)
else:
cache = DummyCache()

self.account_info = account_info
self.cache = cache
Expand Down
14 changes: 14 additions & 0 deletions b2sdk/v2/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@
from .b2http import B2Http

from ._compat import _file_infos_rename
from .._internal import api_config as _api_config
from .._internal import cache as _cache
from .._internal.account_info import abstract as _abstract


# Override to use legacy B2Http
class B2Session(v3.B2Session):
B2HTTP_CLASS = staticmethod(B2Http)

def __init__(
self,
account_info: _abstract.AbstractAccountInfo | None = None,
cache: _cache.AbstractCache | None = None,
api_config: _api_config.B2HttpApiConfig = _api_config.DEFAULT_HTTP_API_CONFIG
):
if account_info is not None and cache is None:
# preserve legacy behavior https://github.com/Backblaze/b2-sdk-python/issues/497#issuecomment-2147461352
cache = _cache.DummyCache()
super().__init__(account_info, cache, api_config)

@_file_infos_rename
def upload_file(
self,
Expand Down
3 changes: 3 additions & 0 deletions changelog.d/497.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In `b2sdk.v3` the `B2Api` will always create `cache` from `AccountInfo` object, unless `cache` is provided explicitly.
The current stable `b2sdk.v2` remains unchanged, i.e. `DummyCache` is created by default if `account_info` was provided, but not `cache`.
Documentation for `b2sdk.v2` was updated with the new recommended usage, e.g. `B2Api(info, cache=AuthInfoCache(info))`, to achieve the same behavior as `b2sdk.v3`.
2 changes: 1 addition & 1 deletion doc/source/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Prepare b2sdk
>>> from b2sdk.v2 import *
>>> info = InMemoryAccountInfo()
>>> b2_api = B2Api(info)
>>> b2_api = B2Api(info, cache=AuthInfoCache(info))
>>> application_key_id = '4a5b6c7d8e9f'
>>> application_key = '001b8e23c26ff6efb941e237deb182b9599a84bef7'
>>> b2_api.authorize_account("production", application_key_id, application_key)
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from unittest import mock

from apiver_deps import AuthInfoCache, B2Session, DummyCache, InMemoryAccountInfo

from .account_info.fixtures import * # noqa
from .fixtures import * # noqa

Expand Down Expand Up @@ -71,3 +73,16 @@ def test_clear_cache(self):
self.b2_session.authorize_account('dev', '123', '456')

assert self.b2_session.cache.clear.called is True


def test_session__with_in_memory_account_info(apiver_int):
memory_info = InMemoryAccountInfo()
b2_session = B2Session(account_info=memory_info,)

assert b2_session.account_info is memory_info

if apiver_int < 3:
assert isinstance(b2_session.cache, DummyCache)
else:
assert isinstance(b2_session.cache, AuthInfoCache)
assert b2_session.cache.info is memory_info

0 comments on commit 9f3b2de

Please sign in to comment.