28
28
import urlparse as parse
29
29
30
30
from .command import Command
31
+ from .errorhandler import ErrorCode
31
32
from . import utils
32
33
33
34
LOGGER = logging .getLogger (__name__ )
@@ -397,9 +398,17 @@ def _request(self, url, data=None, method=None):
397
398
if response .code > 399 and response .code < 500 :
398
399
return {'status' : response .code , 'value' : response .read ()}
399
400
body = response .read ().decode ('utf-8' ).replace ('\x00 ' , '' ).strip ()
400
- content_type = response .info ()['Content-Type' ] or []
401
- if 'application/json' in content_type :
402
- data = utils .load_json (body .strip ())
401
+ content_type = response .info ().getheaders ('Content-Type' )
402
+ if not any ([x .startswith ('image/png' ) for x in content_type ]):
403
+ try :
404
+ data = utils .load_json (body .strip ())
405
+ except ValueError :
406
+ if response .code > 199 and response .code < 300 :
407
+ status = ErrorCode .SUCCESS
408
+ else :
409
+ status = ErrorCode .UNKNOWN_ERROR
410
+ return {'status' : status , 'value' : body .strip ()}
411
+
403
412
assert type (data ) is dict , (
404
413
'Invalid server response body: %s' % body )
405
414
assert 'status' in data , (
@@ -409,7 +418,7 @@ def _request(self, url, data=None, method=None):
409
418
if 'value' not in data :
410
419
data ['value' ] = None
411
420
return data
412
- elif 'image/png' in content_type :
421
+ else :
413
422
data = {'status' : 0 , 'value' : body .strip ()}
414
423
return data
415
424
finally :
0 commit comments