<s> from requests . cookies import extract_cookies_to_jar <EOL> from datetime import datetime <EOL> from betamax import util <EOL> class Interaction ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , interaction , response = None ) : <EOL> self . data = interaction <EOL> self . orig_response = response <EOL> self . recorded_response = self . deserialize ( ) <EOL> self . used = False <EOL> self . ignored = False <EOL> def ignore ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ignored = True <EOL> def as_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . recorded_response = self . deserialize ( ) <EOL> return self . recorded_response <EOL> @ property <EOL> def recorded_at ( self ) : <EOL> return datetime . strptime ( self . data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def deserialize ( self ) : <EOL> """<STR_LIT>""" <EOL> r = util . deserialize_response ( self . data [ '<STR_LIT>' ] ) <EOL> r . request = util . deserialize_prepared_request ( self . data [ '<STR_LIT>' ] ) <EOL> extract_cookies_to_jar ( r . cookies , r . request , r . raw ) <EOL> return r <EOL> def match ( self , matchers ) : <EOL> """<STR_LIT>""" <EOL> request = self . data [ '<STR_LIT>' ] <EOL> return all ( m ( request ) for m in matchers ) <EOL> def replace ( self , text_to_replace , placeholder ) : <EOL> """<STR_LIT>""" <EOL> self . replace_in_headers ( text_to_replace , placeholder ) <EOL> self . replace_in_body ( text_to_replace , placeholder ) <EOL> self . replace_in_uri ( text_to_replace , placeholder ) <EOL> def replace_all ( self , replacements , serializing ) : <EOL> """<STR_LIT>""" <EOL> for placeholder in replacements : <EOL> self . replace ( * placeholder . unpack ( serializing ) ) <EOL> def replace_in_headers ( self , text_to_replace , placeholder ) : <EOL> for obj in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> headers = self . data [ obj ] [ '<STR_LIT>' ] <EOL> for k , v in list ( headers . items ( ) ) : <EOL> v = util . from_list ( v ) <EOL> headers [ k ] = v . replace ( text_to_replace , placeholder ) <EOL> def replace_in_body ( self , text_to_replace , placeholder ) : <EOL> for obj in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> body = self . data [ obj ] [ '<STR_LIT:body>' ] <EOL> old_style = hasattr ( body , '<STR_LIT:replace>' ) <EOL> if not old_style : <EOL> body = body . get ( '<STR_LIT:string>' , '<STR_LIT>' ) <EOL> if text_to_replace in body : <EOL> body = body . replace ( text_to_replace , placeholder ) <EOL> if old_style : <EOL> self . data [ obj ] [ '<STR_LIT:body>' ] = body <EOL> else : <EOL> self . data [ obj ] [ '<STR_LIT:body>' ] [ '<STR_LIT:string>' ] = body <EOL> def replace_in_uri ( self , text_to_replace , placeholder ) : <EOL> for ( obj , key ) in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:url>' ) ) : <EOL> uri = self . data [ obj ] [ key ] <EOL> if text_to_replace in uri : <EOL> self . data [ obj ] [ key ] = uri . replace ( <EOL> text_to_replace , placeholder <EOL> ) </s>
