Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed B2B2 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aiob2/b2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import json

from .api import API

class B2():

Expand Down
10 changes: 5 additions & 5 deletions aiob2/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, session):
:param key_id:
:param application_key:
"""
self.api_session = session
self.session = session
self.key_id = None
self.application_key = None
self.account_id = None
Expand All @@ -53,8 +53,8 @@ async def is_authorized(self):
return True

async def _authorize(self, key_id, application_key):
self.key_id = None
self.application_key = None
self.key_id = key_id
self.application_key = application_key

path = BASE_URL + API.authorize

Expand All @@ -70,7 +70,7 @@ async def _authorize(self, key_id, application_key):
self.recommended_part_size = response_json['recommendedPartSize']

async def get(self, path, headers={}):
if not self.authorized:
if not self.authorized_at:
raise B2AuthorizationError('Not authorized.')
url = self.api_url + path
headers.update({'Authorization': self.auth_token})
Expand All @@ -79,7 +79,7 @@ async def get(self, path, headers={}):

async def put(self, path, headers={}, params={},
account_id_required=False):
if not self.authorized:
if not self.authorized_at:
raise B2AuthorizationError('Not authorized.')
url = self.api_url + path
headers.update({'Authorization': self.auth_token})
Expand Down
29 changes: 15 additions & 14 deletions aiob2/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import json


API_EXCEPTION_CODES = {
400: B2RequestError,
401: B2UnauthorizedError,
403: B2ForbiddenError,
404: B2FileNotFoundError,
408: B2RequestTimeoutError,
429: B2TooManyRequestsError,
500: B2InternalError,
503: B2ServiceUnavailableError,
}


class B2B2Exception(Exception):
class B2Exception(Exception):
""" Base exception class for the Backblaze API """

@staticmethod
Expand All @@ -24,8 +13,8 @@ async def parse(response):
response_json = await response.json()
status = int(response_json['status'])

# Return B2B2Exception if unrecognized status code
ErrorClass = API_EXCEPTION_CODES.get(status, B2B2Exception)
# Return B2Exception if unrecognized status code
ErrorClass = API_EXCEPTION_CODES.get(status, B2Exception)
return ErrorClass('{} - {}: {}'.format(status,
response_json['code'],
response_json['message']))
Expand Down Expand Up @@ -126,3 +115,15 @@ class B2AuthorizationError(B2Exception):
class B2InvalidRequestType(B2Exception):
""" Request type must be get or post """
pass


API_EXCEPTION_CODES = {
400: B2RequestError,
401: B2UnauthorizedError,
403: B2ForbiddenError,
404: B2FileNotFoundError,
408: B2RequestTimeoutError,
429: B2TooManyRequestsError,
500: B2InternalError,
503: B2ServiceUnavailableError,
}