Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from WardPearce/Development
Browse files Browse the repository at this point in the history
0.1.1 - Fixed setting & http base bugs
  • Loading branch information
WardPearce committed Sep 25, 2021
2 parents 9928957 + a06c9bb commit e3d6551
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
2 changes: 1 addition & 1 deletion backblaze/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .cache import Cache


__version__ = "0.1.0"
__version__ = "0.1.1"
__url__ = "https://backblaze.readthedocs.io/en/latest/"
__description__ = "Wrapper for Backblaze's B2."
__author__ = "WardPearce"
Expand Down
32 changes: 13 additions & 19 deletions backblaze/http/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
)


HTTP_ERRORS = {
400: BadRequest,
401: UnAuthorized,
403: Forbidden,
408: RequestTimeout,
429: TooManyRequests,
500: InternalError,
503: ServiceUnavailable
}


class BaseHTTP:
def handle_resp(self, resp: Response, json: bool = True
) -> Union[dict, bytes, None]:
Expand All @@ -38,11 +49,9 @@ def handle_resp(self, resp: Response, json: bool = True
TooManyRequests
InternalError
ServiceUnavailable
HTTPStatusError
Raised when none of the above are.
"""

if resp.status_code == 200:
if resp.status_code not in HTTP_ERRORS:
if json:
return resp.json()
else:
Expand All @@ -53,19 +62,4 @@ def handle_resp(self, resp: Response, json: bool = True
except JSONDecodeError:
pass

if resp.status_code == 400:
raise BadRequest()
elif resp.status_code == 401:
raise UnAuthorized()
elif resp.status_code == 403:
raise Forbidden()
elif resp.status_code == 408:
raise RequestTimeout()
elif resp.status_code == 429:
raise TooManyRequests()
elif resp.status_code == 500:
raise InternalError()
elif resp.status_code == 503:
raise ServiceUnavailable()
else:
resp.raise_for_status()
raise HTTP_ERRORS[resp.status_code]()
7 changes: 4 additions & 3 deletions backblaze/models/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BucketModel:
info : str
revision : str
options : list
lifecycle : list
lifecycle : List[LifecycleModel]
"""

def __init__(self, data: dict) -> None:
Expand All @@ -57,8 +57,9 @@ def __init__(self, data: dict) -> None:
self.info = data["bucketInfo"]
self.revision = data["revision"]
self.options = data["options"]
self.lifecycle = LifecycleModel(
data["lifecycleRules"]) if data["lifecycleRules"] else None
self.lifecycles = [
LifecycleModel(Lifecycle) for Lifecycle in data["lifecycleRules"]
] if data["lifecycleRules"] else None

self.__cors = data["corsRules"]

Expand Down
68 changes: 34 additions & 34 deletions backblaze/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def __init__(self, private: bool = True,
"bucketType": "allPrivate" if private else "allPublic"
}

if info:
if info is not None:
self.payload["bucketInfo"] = info

if lifecycle:
if lifecycle is not None:
self.payload["lifecycleRules"] = lifecycle.payload

if cors:
if cors is not None:
self.payload["corsRules"] = []
cors_append = self.payload["corsRules"].append

Expand Down Expand Up @@ -130,9 +130,9 @@ def __init__(self, capabilities: list, name: str,

if duration is not None:
self.payload["validDurationInSeconds"] = duration
if bucket_id:
if bucket_id is not None:
self.payload["bucketId"] = bucket_id
if prefix:
if prefix is not None:
self.payload["namePrefix"] = prefix


Expand All @@ -155,22 +155,22 @@ def __init__(self, start_name: str = None,
delimiter: str = None) -> None:
self.payload = {}

if start_name:
if start_name is not None:
self.payload["startFileName"] = encode_name(
start_name, replace=False)
if limit:
if limit is not None:
self.payload["maxFileCount"] = limit
if prefix:
if prefix is not None:
self.payload["prefix"] = prefix
if delimiter:
if delimiter is not None:
self.payload["delimiter"] = delimiter


class DownloadSettings:
"""
Parameters
----------
range : int, optional
range : str, optional
by default None
disposition : str, optional
by default None
Expand All @@ -186,7 +186,7 @@ class DownloadSettings:
by default None
"""

def __init__(self, range: int = None, disposition: str = None,
def __init__(self, range: str = None, disposition: str = None,
language: str = None, expires: datetime = None,
cache_control: str = None, encoding: str = None,
content_type: str = None) -> None:
Expand All @@ -195,17 +195,17 @@ def __init__(self, range: int = None, disposition: str = None,

if range is not None:
self.headers["Range"] = range
if disposition:
if disposition is not None:
self.parameters["b2ContentDisposition"] = disposition
if language:
if language is not None:
self.parameters["b2ContentLanguage"] = language
if expires:
if expires is not None:
self.parameters["b2Expires"] = expires.timestamp() * 1000
if cache_control:
if cache_control is not None:
self.parameters["b2CacheControl"] = cache_control
if encoding:
if encoding is not None:
self.parameters["b2ContentEncoding"] = encoding
if content_type:
if content_type is not None:
self.parameters["b2ContentType"] = content_type


Expand Down Expand Up @@ -247,27 +247,27 @@ def __init__(self, name: str, content_type: str = "b2/x-auto",
"Content-Type": content_type
}

if last_modified:
if last_modified is not None:
self.headers[
"X-Bz-Info-src_last_modified_millis"
] = last_modified.timestamp() * 1000

if disposition:
if disposition is not None:
self.headers["X-Bz-Info-b2-content-disposition"] = disposition

if language:
if language is not None:
self.headers["X-Bz-Info-b2-content-language"] = language

if expires:
if expires is not None:
self.headers["X-Bz-Info-b2-expires"] = expires.timestamp() * 1000

if cache_control:
if cache_control is not None:
self.headers["X-Bz-Info-b2-cache-control"] = cache_control

if encoding:
if encoding is not None:
self.headers["X-Bz-Info-b2-content-encoding"] = encoding

if custom_headers:
if custom_headers is not None:
for header, value in custom_headers.items():
self.headers["X-Bz-Info-{}".format(header)] = value

Expand All @@ -292,12 +292,12 @@ def __init__(self, name: str, content_type: str = "b2/x-auto",
"contentType": content_type
}

if last_modified:
if last_modified is not None:
self.payload["fileInfo"] = {
"src_last_modified_millis": last_modified.timestamp() * 1000
}

if sha1:
if sha1 is not None:
if "fileInfo" not in self.payload:
self.payload["fileInfo"] = {
"large_file_sha1": sha1
Expand All @@ -318,7 +318,7 @@ class CopyFileSettings:
by default None
destination_bucket_id : str, optional
by default None
range : int, optional
range : str, optional
by default None
directive : str, optional
by default None
Expand All @@ -327,21 +327,21 @@ class CopyFileSettings:
"""

def __init__(self, name: str, content_type: str = None,
destination_bucket_id: str = None, range: int = None,
destination_bucket_id: str = None, range: str = None,
directive: str = None, info: dict = None) -> None:
self.payload: Dict[str, Union[str, int, dict]] = {
"fileName": encode_name(name, replace=False),
}

if content_type:
if content_type is not None:
self.payload["contentType"] = content_type
if destination_bucket_id:
if destination_bucket_id is not None:
self.payload["destinationBucketId"] = destination_bucket_id
if range is not None:
self.payload["range"] = range
if directive:
if directive is not None:
self.payload["metadataDirective"] = directive
if info:
if info is not None:
self.payload["fileInfo"] = info


Expand All @@ -350,11 +350,11 @@ class CopyPartSettings:
Parameters
----------
file_id : str
range : int, optional
range : str, optional
by default None
"""

def __init__(self, file_id: str, range: int = None) -> None:
def __init__(self, file_id: str, range: str = None) -> None:
self.payload: Dict[str, Union[str, int]] = {
"largeFileId": file_id
}
Expand Down
2 changes: 1 addition & 1 deletion backblaze/tests/blocking/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_bucket(self):
))

bucket.update(BucketUpdateSettings(
private=False
private=True
))

self.assertIsInstance(
Expand Down

0 comments on commit e3d6551

Please sign in to comment.