2828from swift .common .bufferedhttp import http_connect
2929from swift .common .client import ClientException , json_loads
3030from swift .common .utils import normalize_timestamp
31+ from swift .common .http import HTTP_NO_CONTENT , HTTP_INSUFFICIENT_STORAGE , \
32+ is_success , is_server_error
3133
3234
3335def quote (value , safe = '/' ):
@@ -69,7 +71,7 @@ def direct_get_account(node, part, account, marker=None, limit=None,
6971 'GET' , path , query_string = qs )
7072 with Timeout (response_timeout ):
7173 resp = conn .getresponse ()
72- if resp . status < 200 or resp .status >= 300 :
74+ if not is_success ( resp .status ) :
7375 resp .read ()
7476 raise ClientException (
7577 'Account server %s:%s direct GET %s gave status %s' % (node ['ip' ],
@@ -81,7 +83,7 @@ def direct_get_account(node, part, account, marker=None, limit=None,
8183 resp_headers = {}
8284 for header , value in resp .getheaders ():
8385 resp_headers [header .lower ()] = value
84- if resp .status == 204 :
86+ if resp .status == HTTP_NO_CONTENT :
8587 resp .read ()
8688 return resp_headers , []
8789 return resp_headers , json_loads (resp .read ())
@@ -108,7 +110,7 @@ def direct_head_container(node, part, account, container, conn_timeout=5,
108110 with Timeout (response_timeout ):
109111 resp = conn .getresponse ()
110112 resp .read ()
111- if resp . status < 200 or resp .status >= 300 :
113+ if not is_success ( resp .status ) :
112114 raise ClientException (
113115 'Container server %s:%s direct HEAD %s gave status %s' %
114116 (node ['ip' ], node ['port' ],
@@ -157,7 +159,7 @@ def direct_get_container(node, part, account, container, marker=None,
157159 'GET' , path , query_string = qs )
158160 with Timeout (response_timeout ):
159161 resp = conn .getresponse ()
160- if resp . status < 200 or resp .status >= 300 :
162+ if not is_success ( resp .status ) :
161163 resp .read ()
162164 raise ClientException (
163165 'Container server %s:%s direct GET %s gave stats %s' % (node ['ip' ],
@@ -169,7 +171,7 @@ def direct_get_container(node, part, account, container, marker=None,
169171 resp_headers = {}
170172 for header , value in resp .getheaders ():
171173 resp_headers [header .lower ()] = value
172- if resp .status == 204 :
174+ if resp .status == HTTP_NO_CONTENT :
173175 resp .read ()
174176 return resp_headers , []
175177 return resp_headers , json_loads (resp .read ())
@@ -185,7 +187,7 @@ def direct_delete_container(node, part, account, container, conn_timeout=5,
185187 with Timeout (response_timeout ):
186188 resp = conn .getresponse ()
187189 resp .read ()
188- if resp . status < 200 or resp .status >= 300 :
190+ if not is_success ( resp .status ) :
189191 raise ClientException (
190192 'Container server %s:%s direct DELETE %s gave status %s' %
191193 (node ['ip' ], node ['port' ],
@@ -218,7 +220,7 @@ def direct_head_object(node, part, account, container, obj, conn_timeout=5,
218220 with Timeout (response_timeout ):
219221 resp = conn .getresponse ()
220222 resp .read ()
221- if resp . status < 200 or resp .status >= 300 :
223+ if not is_success ( resp .status ) :
222224 raise ClientException (
223225 'Object server %s:%s direct HEAD %s gave status %s' %
224226 (node ['ip' ], node ['port' ],
@@ -256,7 +258,7 @@ def direct_get_object(node, part, account, container, obj, conn_timeout=5,
256258 'GET' , path , headers = headers )
257259 with Timeout (response_timeout ):
258260 resp = conn .getresponse ()
259- if resp . status < 200 or resp .status >= 300 :
261+ if not is_success ( resp .status ) :
260262 resp .read ()
261263 raise ClientException (
262264 'Object server %s:%s direct GET %s gave status %s' %
@@ -326,7 +328,7 @@ def direct_put_object(node, part, account, container, name, contents,
326328 with Timeout (response_timeout ):
327329 resp = conn .getresponse ()
328330 resp .read ()
329- if resp . status < 200 or resp .status >= 300 :
331+ if not is_success ( resp .status ) :
330332 raise ClientException (
331333 'Object server %s:%s direct PUT %s gave status %s' %
332334 (node ['ip' ], node ['port' ],
@@ -361,7 +363,7 @@ def direct_post_object(node, part, account, container, name, headers,
361363 with Timeout (response_timeout ):
362364 resp = conn .getresponse ()
363365 resp .read ()
364- if resp . status < 200 or resp .status >= 300 :
366+ if not is_success ( resp .status ) :
365367 raise ClientException (
366368 'Object server %s:%s direct POST %s gave status %s' %
367369 (node ['ip' ], node ['port' ],
@@ -394,7 +396,7 @@ def direct_delete_object(node, part, account, container, obj,
394396 with Timeout (response_timeout ):
395397 resp = conn .getresponse ()
396398 resp .read ()
397- if resp . status < 200 or resp .status >= 300 :
399+ if not is_success ( resp .status ) :
398400 raise ClientException (
399401 'Object server %s:%s direct DELETE %s gave status %s' %
400402 (node ['ip' ], node ['port' ],
@@ -440,8 +442,8 @@ def retry(func, *args, **kwargs):
440442 except ClientException , err :
441443 if error_log :
442444 error_log (err )
443- if attempts > retries or err .http_status < 500 or \
444- err .http_status == 507 or err . http_status > 599 :
445+ if attempts > retries or not is_server_error ( err .http_status ) or \
446+ err .http_status == HTTP_INSUFFICIENT_STORAGE :
445447 raise
446448 sleep (backoff )
447449 backoff *= 2
0 commit comments