1
|
|
|
""" |
2
|
|
|
Copyright George Sibble 2018 |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class B2ApplicationKeyNotSet(Exception): |
7
|
|
|
""" You must set the B2_KEY_ID environment variable before running the application """ |
8
|
|
|
|
9
|
1 |
|
pass |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
class B2KeyIDNotSet(Exception): |
13
|
|
|
""" You must set the B2_APPLICATION_KEY environment variable before running the application """ |
14
|
|
|
|
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
|
|
|
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
|
|
|
try: |
37
|
|
|
response_json = response.json() |
38
|
|
|
message = response_json["message"] |
39
|
|
|
code = response_json["code"] |
40
|
|
|
status = int(response_json["status"]) |
41
|
|
|
|
42
|
|
|
# Return B2Exception if unrecognized status code |
43
|
|
|
if not status in API_EXCEPTION_CODES: |
|
|
|
|
44
|
|
|
return B2Exception("{} - {}: {}".format(status, code, message)) |
45
|
|
|
|
46
|
|
|
ErrorClass = API_EXCEPTION_CODES[status] |
|
|
|
|
47
|
|
|
return ErrorClass("{} - {}: {}".format(status, code, message)) |
48
|
|
|
|
49
|
|
|
except: |
|
|
|
|
50
|
|
|
return Exception( |
51
|
|
|
"error parsing response. status code - {} Response JSON: {}".format( |
52
|
|
|
response.status_code, response_json |
53
|
|
|
) |
54
|
|
|
) |
55
|
|
|
|
56
|
|
|
|
57
|
1 |
|
class B2FileNotFoundError(Exception): |
58
|
|
|
""" 404 Not Found """ |
59
|
|
|
|
60
|
1 |
|
pass |
61
|
|
|
|
62
|
|
|
|
63
|
1 |
|
class B2RequestError(Exception): |
64
|
|
|
""" There is a problem with a passed in request parameters. See returned message for details """ |
65
|
|
|
|
66
|
1 |
|
pass |
67
|
|
|
|
68
|
|
|
|
69
|
1 |
|
class B2UnauthorizedError(Exception): |
70
|
|
|
""" 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. |
|
|
|
|
71
|
|
|
|
72
|
|
|
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. |
|
|
|
|
73
|
|
|
""" |
74
|
|
|
|
75
|
1 |
|
pass |
76
|
|
|
|
77
|
|
|
|
78
|
1 |
|
class B2ForbiddenError(Exception): |
79
|
|
|
""" You have a reached a storage cap limit, or account access may be impacted in some other way; see the human-readable message. |
|
|
|
|
80
|
|
|
""" |
81
|
|
|
|
82
|
1 |
|
pass |
83
|
|
|
|
84
|
|
|
|
85
|
1 |
|
class B2RequestTimeoutError(Exception): |
86
|
|
|
""" The service timed out trying to read your request. """ |
87
|
|
|
|
88
|
1 |
|
pass |
89
|
|
|
|
90
|
|
|
|
91
|
1 |
|
class B2OutOfRangeError(Exception): |
92
|
|
|
""" The Range header in the request is outside the size of the file.. """ |
93
|
|
|
|
94
|
1 |
|
pass |
95
|
|
|
|
96
|
|
|
|
97
|
1 |
|
class B2TooManyRequestsError(Exception): |
98
|
|
|
""" B2 may limit API requests on a per-account basis. """ |
99
|
|
|
|
100
|
1 |
|
pass |
101
|
|
|
|
102
|
|
|
|
103
|
1 |
|
class B2InternalError(Exception): |
104
|
|
|
""" An unexpected error has occurred. """ |
105
|
|
|
|
106
|
1 |
|
pass |
107
|
|
|
|
108
|
|
|
|
109
|
1 |
|
class B2ServiceUnavailableError(Exception): |
110
|
|
|
""" 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. |
|
|
|
|
111
|
|
|
""" |
112
|
|
|
|
113
|
1 |
|
pass |
114
|
|
|
|
115
|
|
|
|
116
|
1 |
|
class B2InvalidBucketName(Exception): |
117
|
|
|
""" Bucket name must be alphanumeric or '-' """ |
118
|
|
|
|
119
|
1 |
|
pass |
120
|
|
|
|
121
|
|
|
|
122
|
1 |
|
class B2InvalidBucketConfiguration(Exception): |
123
|
|
|
""" Value error in bucket configuration """ |
124
|
|
|
|
125
|
1 |
|
pass |
126
|
|
|
|
127
|
|
|
|
128
|
1 |
|
class B2AuthorizationError(Exception): |
129
|
|
|
""" An error with the authorization request """ |
130
|
|
|
|
131
|
1 |
|
pass |
132
|
|
|
|
133
|
|
|
|
134
|
1 |
|
class B2InvalidRequestType(Exception): |
135
|
|
|
""" Request type must be get or post """ |
136
|
|
|
|
137
|
|
|
pass |
138
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.