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