<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from contextlib import closing <EOL> from cStringIO import StringIO <EOL> import gzip <EOL> import json <EOL> from tornado import escape , web <EOL> from viewfinder . backend . base import constants , util <EOL> from viewfinder . backend . base . exceptions import InvalidRequestError , HttpForbiddenError , NotFoundError <EOL> from viewfinder . backend . base . exceptions import ServiceUnavailableError <EOL> _CONTENT_TYPES = [ '<STR_LIT:application/json>' , '<STR_LIT>' ] <EOL> _CONFIRM_TIME_LIMIT = constants . SECONDS_PER_HOUR <EOL> def ParseJSONRequest ( request ) : <EOL> """<STR_LIT>""" <EOL> content_type = request . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> if not any ( content_type . startswith ( x ) for x in _CONTENT_TYPES ) : <EOL> raise web . HTTPError ( <NUM_LIT> , '<STR_LIT>' % content_type ) <EOL> json_dict = json . loads ( request . body ) <EOL> return json_dict <EOL> def ParseJSONResponse ( response ) : <EOL> """<STR_LIT>""" <EOL> content_type = response . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> if not any ( content_type . startswith ( x ) for x in _CONTENT_TYPES ) : <EOL> raise web . HTTPError ( response . code , '<STR_LIT>' % response . headers ) <EOL> try : <EOL> json_dict = json . loads ( response . body ) <EOL> except : <EOL> if response . code == <NUM_LIT:200> : <EOL> raise <EOL> json_dict = { '<STR_LIT:error>' : response . body } <EOL> if response . code != <NUM_LIT:200> : <EOL> error = '<STR_LIT>' <EOL> if isinstance ( json_dict , dict ) and json_dict . get ( '<STR_LIT:error>' ) : <EOL> error = json_dict . get ( '<STR_LIT:error>' ) <EOL> raise web . HTTPError ( response . code , '<STR_LIT:%s>' % error ) <EOL> return json_dict <EOL> def GzipEncode ( s ) : <EOL> """<STR_LIT>""" <EOL> with closing ( StringIO ( ) ) as sio : <EOL> with gzip . GzipFile ( fileobj = sio , mode = '<STR_LIT:wb>' ) as gzfile : <EOL> gzfile . write ( escape . utf8 ( s ) ) <EOL> return sio . getvalue ( ) <EOL> def GzipDecode ( s ) : <EOL> """<STR_LIT>""" <EOL> with closing ( StringIO ( s ) ) as sio : <EOL> with gzip . GzipFile ( fileobj = sio , mode = '<STR_LIT:rb>' ) as gzfile : <EOL> return gzfile . read ( ) <EOL> def HTTPInfoFromException ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , web . HTTPError ) : <EOL> return value . status_code , value . log_message <EOL> elif isinstance ( value , InvalidRequestError ) : <EOL> return <NUM_LIT> , value . args [ <NUM_LIT:0> ] <EOL> elif isinstance ( value , HttpForbiddenError ) : <EOL> return <NUM_LIT> , value . args [ <NUM_LIT:0> ] <EOL> elif isinstance ( value , NotFoundError ) : <EOL> return <NUM_LIT> , value . args [ <NUM_LIT:0> ] <EOL> elif isinstance ( value , ServiceUnavailableError ) : <EOL> return <NUM_LIT> , value . args [ <NUM_LIT:0> ] <EOL> else : <EOL> return <NUM_LIT> , str ( value ) <EOL> def FormatIntegralLastKey ( value ) : <EOL> """<STR_LIT>""" <EOL> assert value < <NUM_LIT> , value <EOL> return '<STR_LIT>' % value <EOL> def IsConfirmedCookie ( confirm_time ) : <EOL> """<STR_LIT>""" <EOL> return confirm_time is not None and util . GetCurrentTimestamp ( ) < confirm_time + _CONFIRM_TIME_LIMIT </s>
