<s> from __future__ import absolute_import <EOL> import urllib3 <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from . decorators import cached_property <EOL> from . errors import NSQHttpError <EOL> class HTTPClient ( object ) : <EOL> @ cached_property <EOL> def http ( self ) : <EOL> return urllib3 . connection_from_url ( url = self . base_url ) <EOL> def http_request ( self , method , url , ** kwargs ) : <EOL> response = self . http . request_encode_url ( method , url , ** kwargs ) <EOL> if '<STR_LIT:application/json>' in response . getheader ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return self . _http_check_json ( response ) <EOL> return self . _http_check ( response ) <EOL> def _http_check ( self , response ) : <EOL> if response . status != <NUM_LIT:200> : <EOL> raise NSQHttpError ( '<STR_LIT>' % response . status ) <EOL> return response . data <EOL> def _http_check_json ( self , response ) : <EOL> try : <EOL> data = json . loads ( response . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except ValueError : <EOL> return self . _http_check ( response ) <EOL> if response . status != <NUM_LIT:200> : <EOL> status_txt = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> raise NSQHttpError ( '<STR_LIT>' % ( status_txt , response . status ) ) <EOL> return data [ '<STR_LIT:data>' ] <EOL> def http_get ( self , url , ** kwargs ) : <EOL> return self . http_request ( '<STR_LIT:GET>' , url , ** kwargs ) <EOL> def http_post ( self , url , ** kwargs ) : <EOL> return self . http_request ( '<STR_LIT:POST>' , url , ** kwargs ) </s>
