Skip to content

Commit

Permalink
Migrate to B2 native API v3
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhasar-reef committed Dec 31, 2024
1 parent 7c4bf32 commit ef892da
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 35 deletions.
2 changes: 1 addition & 1 deletion b2sdk/_internal/raw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
]

# API version number to use when calling the service
API_VERSION = 'v2'
API_VERSION = 'v3'

logger = getLogger(__name__)

Expand Down
21 changes: 15 additions & 6 deletions b2sdk/_internal/raw_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,12 +1407,21 @@ def authorize_account(self, realm_url, application_key_id, application_key):
return dict(
accountId=key_sim.account_id,
authorizationToken=auth_token,
apiUrl=self.API_URL,
downloadUrl=self.DOWNLOAD_URL,
recommendedPartSize=self.MIN_PART_SIZE,
absoluteMinimumPartSize=self.MIN_PART_SIZE,
allowed=allowed,
s3ApiUrl=self.S3_API_URL,
apiInfo=dict(
groupsApi=dict(),
storageApi=dict(
apiUrl=self.API_URL,
downloadUrl=self.DOWNLOAD_URL,
recommendedPartSize=self.MIN_PART_SIZE,
absoluteMinimumPartSize=self.MIN_PART_SIZE,
allowed=allowed,
s3ApiUrl=self.S3_API_URL,
bucketId=allowed['bucketId'],
bucketName=allowed['bucketName'],
capabilities=allowed['capabilities'],
namePrefix=allowed['namePrefix'],
),
),
)

def cancel_large_file(self, api_url, account_auth_token, file_id):
Expand Down
21 changes: 15 additions & 6 deletions b2sdk/_internal/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ def authorize_account(self, realm, application_key_id, application_key):
realm_url = REALM_URLS.get(realm, realm)
response = self.raw_api.authorize_account(realm_url, application_key_id, application_key)
account_id = response['accountId']
allowed = response['allowed']
storage_api_info = response['apiInfo']['storageApi']

# `allowed` object has been deprecated in the v3 of the API, but we still
# construct it artificially to avoid changes in all the reliant parts.
allowed = {
'bucketId': storage_api_info['bucketId'],
'bucketName': storage_api_info['bucketName'],
'capabilities': storage_api_info['capabilities'],
'namePrefix': storage_api_info['namePrefix'],
}

# Clear the cache if new account has been used
if not self.account_info.is_same_account(account_id, realm):
Expand All @@ -126,13 +135,13 @@ def authorize_account(self, realm, application_key_id, application_key):
self.account_info.set_auth_data(
account_id=account_id,
auth_token=response['authorizationToken'],
api_url=response['apiUrl'],
download_url=response['downloadUrl'],
absolute_minimum_part_size=response['absoluteMinimumPartSize'],
recommended_part_size=response['recommendedPartSize'],
api_url=storage_api_info['apiUrl'],
download_url=storage_api_info['downloadUrl'],
absolute_minimum_part_size=storage_api_info['absoluteMinimumPartSize'],
recommended_part_size=storage_api_info['recommendedPartSize'],
application_key=application_key,
realm=realm,
s3_api_url=response['s3ApiUrl'],
s3_api_url=storage_api_info['s3ApiUrl'],
allowed=allowed,
application_key_id=application_key_id,
)
Expand Down
19 changes: 14 additions & 5 deletions b2sdk/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ def authorize_account(self, realm, application_key_id, application_key):
realm_url = self.account_info.REALM_URLS.get(realm, realm)
response = self.raw_api.authorize_account(realm_url, application_key_id, application_key)
account_id = response['accountId']
allowed = response['allowed']
storage_api_info = response['apiInfo']['storageApi']

# `allowed` object has been deprecated in the v3 of the API, but we still
# construct it artificially to avoid changes in all the reliant parts.
allowed = {
'bucketId': storage_api_info['bucketId'],
'bucketName': storage_api_info['bucketName'],
'capabilities': storage_api_info['capabilities'],
'namePrefix': storage_api_info['namePrefix'],
}

# Clear the cache if new account has been used
if not self.account_info.is_same_account(account_id, realm):
Expand All @@ -59,12 +68,12 @@ def authorize_account(self, realm, application_key_id, application_key):
self.account_info.set_auth_data(
account_id=account_id,
auth_token=response['authorizationToken'],
api_url=response['apiUrl'],
download_url=response['downloadUrl'],
minimum_part_size=response['recommendedPartSize'],
api_url=storage_api_info['apiUrl'],
download_url=storage_api_info['downloadUrl'],
minimum_part_size=storage_api_info['recommendedPartSize'],
application_key=application_key,
realm=realm,
s3_api_url=response['s3ApiUrl'],
s3_api_url=storage_api_info['s3ApiUrl'],
allowed=allowed,
application_key_id=application_key_id,
)
1 change: 1 addition & 0 deletions changelog.d/+migrate_to_b2_v3_api.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate to B2 Native API v3.
13 changes: 8 additions & 5 deletions test/integration/test_raw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def raw_api_test_helper(raw_api, should_cleanup_old_buckets):

account_id = auth_dict['accountId']
account_auth_token = auth_dict['authorizationToken']
api_url = auth_dict['apiUrl']
download_url = auth_dict['downloadUrl']
api_url = auth_dict['apiInfo']['storageApi']['apiUrl']
download_url = auth_dict['apiInfo']['storageApi']['downloadUrl']

# b2_create_key
print('b2_create_key')
Expand Down Expand Up @@ -599,7 +599,7 @@ def raw_api_test_helper(raw_api, should_cleanup_old_buckets):


def _subtest_bucket_notification_rules(raw_api, auth_dict, api_url, account_auth_token, bucket_id):
if 'writeBucketNotifications' not in auth_dict['allowed']['capabilities']:
if 'writeBucketNotifications' not in auth_dict['apiInfo']['storageApi']['capabilities']:
pytest.skip('Test account does not have writeBucketNotifications capability')

notification_rule = {
Expand Down Expand Up @@ -644,8 +644,11 @@ def _subtest_bucket_notification_rules(raw_api, auth_dict, api_url, account_auth
def cleanup_old_buckets():
raw_api = B2RawHTTPApi(B2Http())
auth_dict = authorize_raw_api(raw_api)

bucket_list_dict = raw_api.list_buckets(
auth_dict['apiUrl'], auth_dict['authorizationToken'], auth_dict['accountId']
auth_dict['apiInfo']['storageApi']['apiUrl'],
auth_dict['authorizationToken'],
auth_dict['accountId'],
)
_cleanup_old_buckets(raw_api, auth_dict, bucket_list_dict)

Expand All @@ -658,7 +661,7 @@ def _cleanup_old_buckets(raw_api, auth_dict, bucket_list_dict):
print('cleaning up old bucket: ' + bucket_name)
_clean_and_delete_bucket(
raw_api,
auth_dict['apiUrl'],
auth_dict['apiInfo']['storageApi']['apiUrl'],
auth_dict['authorizationToken'],
auth_dict['accountId'],
bucket_id,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_ssec_key_id(self):

auth_dict = authorize_raw_api(raw_api)
account_auth_token = auth_dict['authorizationToken']
api_url = auth_dict['apiUrl']
api_url = auth_dict['apiInfo']['storageApi']['apiUrl']
bucket = self.create_bucket()

large_info = raw_api.start_large_file(
Expand Down
23 changes: 13 additions & 10 deletions test/unit/fixtures/raw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
def fake_b2_raw_api_responses():
return {
'authorize_account': {
'absoluteMinimumPartSize': 5000000,
'accountId': '6012deadbeef',
'allowed': {
'bucketId': None,
'bucketName': None,
'capabilities': copy(ALL_CAPABILITIES),
'namePrefix': None,
'apiInfo': {
'groupsApi': {},
'storageApi': {
'bucketId': None,
'bucketName': None,
'capabilities': copy(ALL_CAPABILITIES),
'namePrefix': None,
'downloadUrl': 'https://f000.backblazeb2.xyz:8180',
'absoluteMinimumPartSize': 5000000,
'recommendedPartSize': 100000000,
'apiUrl': 'https://api000.backblazeb2.xyz:8180',
's3ApiUrl': 'https://s3.us-west-000.backblazeb2.xyz:8180',
},
},
'apiUrl': 'https://api000.backblazeb2.xyz:8180',
'authorizationToken': '4_1111111111111111111111111_11111111_111111_1111_1111111111111_1111_11111111=',
'downloadUrl': 'https://f000.backblazeb2.xyz:8180',
'recommendedPartSize': 100000000,
's3ApiUrl': 'https://s3.us-west-000.backblazeb2.xyz:8180',
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/v_all/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ def test_get_download_url_for_fileid(self):

assert (
download_url
== 'http://download.example.com/b2api/v2/b2_download_file_by_id?fileId=file-id'
== 'http://download.example.com/b2api/v3/b2_download_file_by_id?fileId=file-id'
)

0 comments on commit ef892da

Please sign in to comment.