<s> from requests . auth import AuthBase <EOL> import requests <EOL> import json <EOL> from ttrss . exceptions import raise_on_error <EOL> class TTRAuth ( AuthBase ) : <EOL> def __init__ ( self , user , password , http_auth ) : <EOL> self . user = user <EOL> self . password = password <EOL> self . http_auth = http_auth <EOL> self . sid = None <EOL> def response_hook ( self , r , ** kwargs ) : <EOL> j = json . loads ( r . text ) <EOL> if int ( j [ '<STR_LIT:status>' ] ) == <NUM_LIT:0> : <EOL> return r <EOL> self . sid = self . _get_sid ( r . request . url ) <EOL> r . request . deregister_hook ( '<STR_LIT>' , self . response_hook ) <EOL> j = json . loads ( r . request . body ) <EOL> j . update ( { '<STR_LIT>' : self . sid } ) <EOL> req = requests . Request ( '<STR_LIT:POST>' , r . request . url , auth = self . http_auth ) <EOL> req . data = json . dumps ( j ) <EOL> _r = requests . Session ( ) . send ( req . prepare ( ) ) <EOL> raise_on_error ( _r ) <EOL> return _r <EOL> def __call__ ( self , r ) : <EOL> r . register_hook ( '<STR_LIT>' , self . response_hook ) <EOL> data = json . loads ( r . body ) <EOL> if '<STR_LIT>' not in data : <EOL> if self . sid is None : <EOL> self . sid = self . _get_sid ( r . url ) <EOL> data . update ( { '<STR_LIT>' : self . sid } ) <EOL> req = requests . Request ( '<STR_LIT:POST>' , r . url , auth = self . http_auth ) <EOL> req . data = json . dumps ( data ) <EOL> return req . prepare ( ) <EOL> else : <EOL> self . sid = data [ '<STR_LIT>' ] <EOL> return r <EOL> def _get_sid ( self , url ) : <EOL> res = requests . post ( url , auth = self . http_auth , data = json . dumps ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : self . user , <EOL> '<STR_LIT:password>' : self . password <EOL> } ) ) <EOL> raise_on_error ( res ) <EOL> j = json . loads ( res . text ) <EOL> return j [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] </s>
