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

Fix: Fixes connection aborted issue. #29

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion b2blaze/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from b2blaze.b2lib import B2
from .api import API_VERSION, BASE_URL, API
from .api import API_VERSION, BASE_URL, API
34 changes: 17 additions & 17 deletions b2blaze/api.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# api.py
# BackBlaze API endpoints

API_VERSION = '/b2api/v2'
BASE_URL = 'https://api.backblazeb2.com' + API_VERSION
API_VERSION = "/b2api/v2"
BASE_URL = "https://api.backblazeb2.com" + API_VERSION


class API():
authorize = '/b2_authorize_account'
delete_file = '/b2_hide_file'
delete_file_version = '/b2_delete_file_version'
file_info = '/b2_get_file_info'
download_file_by_id = '/b2_download_file_by_id'
list_all_files = '/b2_list_file_names'
list_file_versions = '/b2_list_file_versions'
upload_url = '/b2_get_upload_url'
upload_large = '/b2_start_large_file'
upload_large_part = '/b2_get_upload_part_url'
upload_large_finish = '/b2_finish_large_file'
create_bucket = '/b2_create_bucket'
delete_bucket = '/b2_delete_bucket'
list_all_buckets = '/b2_list_buckets'
class API:
authorize = "/b2_authorize_account"
delete_file = "/b2_hide_file"
delete_file_version = "/b2_delete_file_version"
file_info = "/b2_get_file_info"
download_file_by_id = "/b2_download_file_by_id"
list_all_files = "/b2_list_file_names"
list_file_versions = "/b2_list_file_versions"
upload_url = "/b2_get_upload_url"
upload_large = "/b2_start_large_file"
upload_large_part = "/b2_get_upload_part_url"
upload_large_finish = "/b2_finish_large_file"
create_bucket = "/b2_create_bucket"
delete_bucket = "/b2_delete_bucket"
list_all_buckets = "/b2_list_buckets"
63 changes: 42 additions & 21 deletions b2blaze/b2_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
Copyright George Sibble 2018
"""

import json


class B2ApplicationKeyNotSet(Exception):
""" You must set the B2_KEY_ID environment variable before running the application """

pass


class B2KeyIDNotSet(Exception):
""" You must set the B2_APPLICATION_KEY environment variable before running the application """

pass


Expand All @@ -23,94 +23,115 @@ def parse(response):
""" Parse the response error code and return the related error type. """

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

try:
response_json = response.json()
message = response_json['message']
code = response_json['code']
status = int(response_json['status'])
message = response_json["message"]
code = response_json["code"]
status = int(response_json["status"])

# Return B2Exception if unrecognized status code
if not status in API_EXCEPTION_CODES:
return B2Exception('{} - {}: {}'.format(status, code, message))
return B2Exception("{} - {}: {}".format(status, code, message))

ErrorClass = API_EXCEPTION_CODES[status]
return ErrorClass('{} - {}: {}'.format(status, code, message))
return ErrorClass("{} - {}: {}".format(status, code, message))

except:
return Exception('error parsing response. status code - {} Response JSON: {}'.format(response.status_code, response_json) )
return Exception(
"error parsing response. status code - {} Response JSON: {}".format(
response.status_code, response_json
)
)


class B2FileNotFoundError(Exception):
""" 404 Not Found """

pass


class B2RequestError(Exception):
""" There is a problem with a passed in request parameters. See returned message for details """

pass


class B2UnauthorizedError(Exception):
""" When calling b2_authorize_account, this means that there was something wrong with the accountId/applicationKeyId or with the applicationKey that was provided. The code unauthorized means that the application key is bad. The code unsupported means that the application key is only valid in a later version of the API.
""" When calling b2_authorize_account, this means that there was something wrong with the accountId/applicationKeyId or with the applicationKey that was provided. The code unauthorized means that the application key is bad. The code unsupported means that the application key is only valid in a later version of the API.

The code unauthorized means that the auth token is valid, but does not allow you to make this call with these parameters. When the code is either bad_auth_token or expired_auth_token you should call b2_authorize_account again to get a new auth token.
"""

pass


class B2ForbiddenError(Exception):
""" You have a reached a storage cap limit, or account access may be impacted in some other way; see the human-readable message.
""" You have a reached a storage cap limit, or account access may be impacted in some other way; see the human-readable message.
"""

pass


class B2RequestTimeoutError(Exception):
""" The service timed out trying to read your request. """

pass


class B2OutOfRangeError(Exception):
""" The Range header in the request is outside the size of the file.. """

pass


class B2TooManyRequestsError(Exception):
""" B2 may limit API requests on a per-account basis. """

pass


class B2InternalError(Exception):
""" An unexpected error has occurred. """

pass


class B2ServiceUnavailableError(Exception):
""" The service is temporarily unavailable. The human-readable message identifies the nature of the issue, in general we recommend retrying with an exponential backoff between retries in response to this error.
""" The service is temporarily unavailable. The human-readable message identifies the nature of the issue, in general we recommend retrying with an exponential backoff between retries in response to this error.
"""

pass


class B2InvalidBucketName(Exception):
""" Bucket name must be alphanumeric or '-' """

pass


class B2InvalidBucketConfiguration(Exception):
""" Value error in bucket configuration """

pass


class B2AuthorizationError(Exception):
""" An error with the authorization request """

pass


class B2InvalidRequestType(Exception):
""" Request type must be get or post """

pass
13 changes: 8 additions & 5 deletions b2blaze/b2lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,35 @@
Copyright George Sibble 2018
"""
import os

from b2blaze.b2_exceptions import B2ApplicationKeyNotSet, B2KeyIDNotSet
from b2blaze.connector import B2Connector
from b2blaze.models.bucket_list import B2Buckets


class B2(object):
"""

"""

def __init__(self, key_id=None, application_key=None):
"""

:param key_id:
:param application_key:
"""
if key_id is None or application_key is None:
key_id = os.environ.get('B2_KEY_ID', None)
application_key = os.environ.get('B2_APPLICATION_KEY', None)
key_id = os.environ.get("B2_KEY_ID", None)
application_key = os.environ.get("B2_APPLICATION_KEY", None)
if key_id is None:
raise B2KeyIDNotSet
if application_key is None:
raise B2ApplicationKeyNotSet
self.key_id = key_id
self.application_key = application_key
self.connector = B2Connector(key_id=self.key_id, application_key=self.application_key)

self.connector = B2Connector(
key_id=self.key_id, application_key=self.application_key
)

@property
def buckets(self):
Expand All @@ -35,4 +39,3 @@ def buckets(self):
:return:
"""
return B2Buckets(connector=self.connector)

Loading