1
|
|
|
""" |
2
|
|
|
Copyright George Sibble 2018 |
3
|
|
|
""" |
4
|
|
|
|
5
|
1 |
|
import json |
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
1 |
|
class B2ApplicationKeyNotSet(Exception): |
9
|
|
|
""" You must set the B2_KEY_ID environment variable before running the application """ |
10
|
1 |
|
pass |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
class B2KeyIDNotSet(Exception): |
14
|
|
|
""" You must set the B2_APPLICATION_KEY environment variable before running the application """ |
15
|
1 |
|
pass |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
class B2Exception(Exception): |
19
|
|
|
""" Base exception class for the Backblaze API """ |
20
|
|
|
|
21
|
1 |
|
@staticmethod |
22
|
|
|
def parse(response): |
23
|
|
|
""" Parse the response error code and return the related error type. """ |
24
|
|
|
|
25
|
1 |
|
API_EXCEPTION_CODES = { |
|
|
|
|
26
|
|
|
400 : B2RequestError, |
27
|
|
|
401 : B2UnauthorizedError, |
28
|
|
|
403 : B2ForbiddenError, |
29
|
|
|
404 : B2FileNotFoundError, |
30
|
|
|
408 : B2RequestTimeoutError, |
31
|
|
|
429 : B2TooManyRequestsError, |
32
|
|
|
500 : B2InternalError, |
33
|
|
|
503 : B2ServiceUnavailableError, |
34
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
1 |
|
try: |
37
|
1 |
|
response_json = response.json() |
38
|
1 |
|
message = response_json['message'] |
39
|
1 |
|
code = response_json['code'] |
40
|
1 |
|
status = int(response_json['status']) |
41
|
|
|
|
42
|
|
|
# Return B2Exception if unrecognized status code |
43
|
1 |
|
if not status in API_EXCEPTION_CODES: |
|
|
|
|
44
|
|
|
return B2Exception('{} - {}: {}'.format(status, code, message)) |
45
|
|
|
|
|
|
|
|
46
|
1 |
|
ErrorClass = API_EXCEPTION_CODES[status] |
|
|
|
|
47
|
1 |
|
return ErrorClass('{} - {}: {}'.format(status, code, message)) |
48
|
|
|
|
49
|
|
|
except: |
|
|
|
|
50
|
|
|
return Exception('error parsing response. status code - {} Response JSON: {}'.format(response.status_code, response_json) ) |
|
|
|
|
51
|
|
|
|
52
|
1 |
|
class B2FileNotFoundError(Exception): |
53
|
|
|
""" 404 Not Found """ |
54
|
1 |
|
pass |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
class B2RequestError(Exception): |
58
|
|
|
""" There is a problem with a passed in request parameters. See returned message for details """ |
59
|
1 |
|
pass |
60
|
|
|
|
61
|
|
|
|
62
|
1 |
|
class B2UnauthorizedError(Exception): |
63
|
|
|
""" 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. |
|
|
|
|
64
|
|
|
|
65
|
|
|
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. |
|
|
|
|
66
|
|
|
""" |
67
|
1 |
|
pass |
68
|
|
|
|
69
|
|
|
|
70
|
1 |
|
class B2ForbiddenError(Exception): |
71
|
|
|
""" You have a reached a storage cap limit, or account access may be impacted in some other way; see the human-readable message. |
|
|
|
|
72
|
|
|
""" |
73
|
1 |
|
pass |
74
|
|
|
|
75
|
|
|
|
76
|
1 |
|
class B2RequestTimeoutError(Exception): |
77
|
|
|
""" The service timed out trying to read your request. """ |
78
|
1 |
|
pass |
79
|
|
|
|
80
|
1 |
|
class B2OutOfRangeError(Exception): |
81
|
|
|
""" The Range header in the request is outside the size of the file.. """ |
82
|
1 |
|
pass |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
class B2TooManyRequestsError(Exception): |
86
|
|
|
""" B2 may limit API requests on a per-account basis. """ |
87
|
1 |
|
pass |
88
|
|
|
|
89
|
|
|
|
90
|
1 |
|
class B2InternalError(Exception): |
91
|
|
|
""" An unexpected error has occurred. """ |
92
|
1 |
|
pass |
93
|
|
|
|
94
|
|
|
|
95
|
1 |
|
class B2ServiceUnavailableError(Exception): |
96
|
|
|
""" 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. |
|
|
|
|
97
|
|
|
""" |
98
|
1 |
|
pass |
99
|
|
|
|
100
|
|
|
|
101
|
1 |
|
class B2InvalidBucketName(Exception): |
102
|
|
|
""" Bucket name must be alphanumeric or '-' """ |
103
|
1 |
|
pass |
104
|
|
|
|
105
|
|
|
|
106
|
1 |
|
class B2InvalidBucketConfiguration(Exception): |
107
|
|
|
""" Value error in bucket configuration """ |
108
|
1 |
|
pass |
109
|
|
|
|
110
|
1 |
|
class B2AuthorizationError(Exception): |
111
|
|
|
""" An error with the authorization request """ |
112
|
1 |
|
pass |
113
|
|
|
|
114
|
1 |
|
class B2InvalidRequestType(Exception): |
115
|
|
|
""" Request type must be get or post """ |
116
|
|
|
pass |
117
|
|
|
|