<s> from __future__ import absolute_import <EOL> import json <EOL> import logging <EOL> import sys <EOL> from time import sleep <EOL> from cached_property import cached_property <EOL> from standardjson import StandardJSONEncoder <EOL> import requests <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> class Senderable ( object ) : <EOL> def __init__ ( self , wrapped , dkwargs , hash_value , attempts , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . wrapped = wrapped <EOL> self . dkwargs = dkwargs <EOL> self . hash_value = hash_value <EOL> self . attempts = attempts <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . attempt = <NUM_LIT:0> <EOL> self . success = False <EOL> self . response = None <EOL> @ cached_property <EOL> def url ( self ) : <EOL> return self . get_url ( ) <EOL> def get_url ( self ) : <EOL> return "<STR_LIT>" <EOL> @ cached_property <EOL> def payload ( self ) : <EOL> return self . get_payload ( ) <EOL> def get_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = self . wrapped ( * self . args , ** self . kwargs ) <EOL> if self . hash_value is not None and len ( self . hash_value ) > <NUM_LIT:0> : <EOL> payload [ '<STR_LIT>' ] = self . hash_value <EOL> return payload <EOL> @ cached_property <EOL> def jsonified_payload ( self ) : <EOL> return self . get_jsonified_payload ( ) <EOL> def get_jsonified_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( self . payload , cls = StandardJSONEncoder ) <EOL> def notify ( self , message ) : <EOL> logging . debug ( message ) <EOL> def send ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _send ( ) <EOL> def _send ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = self . payload <EOL> payload [ '<STR_LIT:url>' ] = self . url <EOL> for i , wait in enumerate ( range ( len ( self . attempts ) - <NUM_LIT:1> ) ) : <EOL> self . attempt = i + <NUM_LIT:1> <EOL> payload [ '<STR_LIT>' ] = self . attempt <EOL> self . response = requests . post ( self . url , self . payload ) <EOL> if sys . version > '<STR_LIT:3>' : <EOL> self . response_content = self . response . content . decode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> self . response_content = self . response . content <EOL> payload [ '<STR_LIT>' ] = self . response . status_code <EOL> if self . response . status_code >= <NUM_LIT:200> and self . response . status_code < <NUM_LIT> : <EOL> self . success = True <EOL> self . notify ( "<STR_LIT>" . format ( <EOL> self . attempt , self . hash_value ) <EOL> ) <EOL> payload [ '<STR_LIT>' ] = self . response_content <EOL> break <EOL> self . notify ( "<STR_LIT>" . format ( <EOL> self . attempt , self . hash_value ) <EOL> ) <EOL> sleep ( wait ) <EOL> return payload <EOL> def value_in ( key , dkwargs , kwargs ) : <EOL> if key in kwargs : <EOL> return kwargs [ key ] <EOL> elif key in dkwargs : <EOL> return dkwargs [ key ] <EOL> else : <EOL> msg = "<STR_LIT>" % key <EOL> raise TypeError ( msg ) </s>
