8
8
#
9
9
######################################################################
10
10
11
- from typing import Any , Dict , Optional , Tuple
11
+ from typing import Optional , Tuple , List , Generator
12
12
13
+ from .account_info .abstract import AbstractAccountInfo
14
+ from .api_config import B2HttpApiConfig , DEFAULT_HTTP_API_CONFIG
15
+ from .application_key import ApplicationKey , BaseApplicationKey , FullApplicationKey
16
+ from .cache import AbstractCache
13
17
from .bucket import Bucket , BucketFactory
14
18
from .encryption .setting import EncryptionSetting
15
19
from .exception import NonExistentBucket , RestrictedBucket
16
20
from .file_lock import FileRetentionSetting , LegalHold
17
- from .file_version import DownloadVersionFactory , FileIdAndName , FileVersionFactory
21
+ from .file_version import DownloadVersionFactory , FileIdAndName , FileVersion , FileVersionFactory
18
22
from .large_file .services import LargeFileServices
19
23
from .raw_api import API_VERSION
20
24
from .progress import AbstractProgressListener
@@ -68,7 +72,7 @@ class B2Api(metaclass=B2TraceMeta):
68
72
"""
69
73
Provide file-level access to B2 services.
70
74
71
- While :class:`b2sdk.v1.B2RawApi ` provides direct access to the B2 web APIs, this
75
+ While :class:`b2sdk.v1.B2RawHTTPApi ` provides direct access to the B2 web APIs, this
72
76
class handles several things that simplify the task of uploading
73
77
and downloading files:
74
78
@@ -89,42 +93,32 @@ class handles several things that simplify the task of uploading
89
93
SESSION_CLASS = staticmethod (B2Session )
90
94
FILE_VERSION_FACTORY_CLASS = staticmethod (FileVersionFactory )
91
95
DOWNLOAD_VERSION_FACTORY_CLASS = staticmethod (DownloadVersionFactory )
96
+ DEFAULT_LIST_KEY_COUNT = 1000
92
97
93
98
def __init__ (
94
99
self ,
95
- account_info = None ,
96
- cache = None ,
97
- raw_api = None ,
98
- max_upload_workers = 10 ,
99
- max_copy_workers = 10
100
+ account_info : Optional [ AbstractAccountInfo ] = None ,
101
+ cache : Optional [ AbstractCache ] = None ,
102
+ max_upload_workers : int = 10 ,
103
+ max_copy_workers : int = 10 ,
104
+ api_config : B2HttpApiConfig = DEFAULT_HTTP_API_CONFIG ,
100
105
):
101
106
"""
102
107
Initialize the API using the given account info.
103
108
104
- :param account_info: an instance of :class:`~b2sdk.v1.UrlPoolAccountInfo`,
105
- or any custom class derived from
106
- :class:`~b2sdk.v1.AbstractAccountInfo`
107
- To learn more about Account Info objects, see here
109
+ :param account_info: To learn more about Account Info objects, see here
108
110
:class:`~b2sdk.v1.SqliteAccountInfo`
109
111
110
- :param cache: an instance of the one of the following classes:
111
- :class:`~b2sdk.cache.DummyCache`, :class:`~b2sdk.cache.InMemoryCache`,
112
- :class:`~b2sdk.cache.AuthInfoCache`,
113
- or any custom class derived from :class:`~b2sdk.cache.AbstractCache`
114
- It is used by B2Api to cache the mapping between bucket name and bucket ids.
112
+ :param cache: It is used by B2Api to cache the mapping between bucket name and bucket ids.
115
113
default is :class:`~b2sdk.cache.DummyCache`
116
114
117
- :param raw_api: an instance of one of the following classes:
118
- :class:`~b2sdk.raw_api.B2RawApi`, :class:`~b2sdk.raw_simulator.RawSimulator`,
119
- or any custom class derived from :class:`~b2sdk.raw_api.AbstractRawApi`
120
- It makes network-less unit testing simple by using :class:`~b2sdk.raw_simulator.RawSimulator`,
121
- in tests and :class:`~b2sdk.raw_api.B2RawApi` in production.
122
- default is :class:`~b2sdk.raw_api.B2RawApi`
123
-
124
- :param int max_upload_workers: a number of upload threads, default is 10
125
- :param int max_copy_workers: a number of copy threads, default is 10
115
+ :param max_upload_workers: a number of upload threads
116
+ :param max_copy_workers: a number of copy threads
117
+ :param api_config:
126
118
"""
127
- self .session = self .SESSION_CLASS (account_info = account_info , cache = cache , raw_api = raw_api )
119
+ self .session = self .SESSION_CLASS (
120
+ account_info = account_info , cache = cache , api_config = api_config
121
+ )
128
122
self .file_version_factory = self .FILE_VERSION_FACTORY_CLASS (self )
129
123
self .download_version_factory = self .DOWNLOAD_VERSION_FACTORY_CLASS (self )
130
124
self .services = Services (
@@ -145,8 +139,8 @@ def cache(self):
145
139
def raw_api (self ):
146
140
"""
147
141
.. warning::
148
- :class:`~b2sdk.raw_api.B2RawApi ` attribute is deprecated.
149
- :class:`~b2sdk.session.B2Session` expose all :class:`~b2sdk.raw_api.B2RawApi ` methods now."""
142
+ :class:`~b2sdk.raw_api.B2RawHTTPApi ` attribute is deprecated.
143
+ :class:`~b2sdk.session.B2Session` expose all :class:`~b2sdk.raw_api.B2RawHTTPApi ` methods now."""
150
144
return self .session .raw_api
151
145
152
146
def authorize_automatically (self ):
@@ -424,20 +418,20 @@ def get_download_url_for_file_name(self, bucket_name, file_name):
424
418
# keys
425
419
def create_key (
426
420
self ,
427
- capabilities ,
428
- key_name ,
429
- valid_duration_seconds = None ,
430
- bucket_id = None ,
431
- name_prefix = None ,
421
+ capabilities : List [ str ] ,
422
+ key_name : str ,
423
+ valid_duration_seconds : Optional [ int ] = None ,
424
+ bucket_id : Optional [ str ] = None ,
425
+ name_prefix : Optional [ str ] = None ,
432
426
):
433
427
"""
434
428
Create a new :term:`application key`.
435
429
436
- :param list capabilities: a list of capabilities
437
- :param str key_name: a name of a key
438
- :param int,None valid_duration_seconds: key auto-expire time after it is created, in seconds, or ``None`` to not expire
439
- :param str,None bucket_id: a bucket ID to restrict the key to, or ``None`` to not restrict
440
- :param str,None name_prefix: a remote filename prefix to restrict the key to or ``None`` to not restrict
430
+ :param capabilities: a list of capabilities
431
+ :param key_name: a name of a key
432
+ :param valid_duration_seconds: key auto-expire time after it is created, in seconds, or ``None`` to not expire
433
+ :param bucket_id: a bucket ID to restrict the key to, or ``None`` to not restrict
434
+ :param name_prefix: a remote filename prefix to restrict the key to or ``None`` to not restrict
441
435
"""
442
436
account_id = self .account_info .get_account_id ()
443
437
@@ -453,43 +447,60 @@ def create_key(
453
447
assert set (response ['capabilities' ]) == set (capabilities )
454
448
assert response ['keyName' ] == key_name
455
449
456
- return response
450
+ return FullApplicationKey .from_create_response (response )
451
+
452
+ def delete_key (self , application_key : BaseApplicationKey ):
453
+ """
454
+ Delete :term:`application key`.
455
+
456
+ :param application_key: an :term:`application key`
457
+ """
458
+
459
+ return self .delete_key_by_id (application_key .id_ )
457
460
458
- def delete_key (self , application_key_id ):
461
+ def delete_key_by_id (self , application_key_id : str ):
459
462
"""
460
463
Delete :term:`application key`.
461
464
462
- :param str application_key_id: an :term:`application key ID`
465
+ :param application_key_id: an :term:`application key ID`
463
466
"""
464
467
465
468
response = self .session .delete_key (application_key_id = application_key_id )
466
- return response
469
+ return ApplicationKey . from_api_response ( response )
467
470
468
- def list_keys (self , start_application_key_id = None ):
471
+ def list_keys (self , start_application_key_id : Optional [str ] = None
472
+ ) -> Generator [ApplicationKey , None , None ]:
469
473
"""
470
- List application keys.
474
+ List application keys. Lazily perform requests to B2 cloud and return all keys.
471
475
472
- :param str,None start_application_key_id: an :term:`application key ID` to start from or ``None`` to start from the beginning
476
+ :param start_application_key_id: an :term:`application key ID` to start from or ``None`` to start from the beginning
473
477
"""
474
478
account_id = self .account_info .get_account_id ()
475
479
476
- return self .session .list_keys (
477
- account_id , max_key_count = 1000 , start_application_key_id = start_application_key_id
478
- )
480
+ while True :
481
+ response = self .session .list_keys (
482
+ account_id ,
483
+ max_key_count = self .DEFAULT_LIST_KEY_COUNT ,
484
+ start_application_key_id = start_application_key_id ,
485
+ )
486
+ for entry in response ['keys' ]:
487
+ yield ApplicationKey .from_api_response (entry )
488
+
489
+ next_application_key_id = response ['nextApplicationKeyId' ]
490
+ if next_application_key_id is None :
491
+ return
492
+ start_application_key_id = next_application_key_id
479
493
480
494
# other
481
- def get_file_info (self , file_id : str ) -> Dict [ str , Any ] :
495
+ def get_file_info (self , file_id : str ) -> FileVersion :
482
496
"""
483
- Legacy interface which just returns whatever remote API returns.
484
-
485
- .. todo::
486
- get_file_info() should return a File with .delete(), copy(), rename(), read() and so on
497
+ Gets info about file version.
487
498
488
499
:param str file_id: the id of the file who's info will be retrieved.
489
- :return: The parsed response
490
- :rtype: dict
491
500
"""
492
- return self .session .get_file_info_by_id (file_id )
501
+ return self .file_version_factory .from_api_response (
502
+ self .session .get_file_info_by_id (file_id )
503
+ )
493
504
494
505
def check_bucket_name_restrictions (self , bucket_name : str ):
495
506
"""
0 commit comments