@@ -100,16 +100,13 @@ def register_config(self, config):
100100 name = inspect .getmodule (inspect .stack ()[1 ][0 ]).__file__
101101 self .logger .info ('Registering config for {}.' .format (name ))
102102 request = {'hash' : state_id }
103- r = requests .post (self .broker + REGISTER_STATE , data = json .dumps (request ))
104- r .raise_for_status ()
105- r = r .json ()
106- self ._check_result (r .get ('result' ), REGISTER_STATE )
103+ reply = self ._send (REGISTER_STATE , request )
107104
108105 # Does the broker ask for the state?
109- if r .get ('request' ) == 'get_state' :
110- if r .get ('hash' ) != state_id :
106+ if reply .get ('request' ) == 'get_state' :
107+ if reply .get ('hash' ) != state_id :
111108 raise BrokerError ('The broker is asking for state {} when state {} (config) was '
112- 'registered.' .format (r .get ('hash' ), state_id ))
109+ 'registered.' .format (reply .get ('hash' ), state_id ))
113110 config ['initial_config' ] = name
114111 self ._send_state (state_id , config )
115112
@@ -118,13 +115,21 @@ def register_config(self, config):
118115
119116 return
120117
118+ def _send (self , endpoint , data ):
119+ try :
120+ reply = requests .post (self .broker + endpoint , data = json .dumps (data ))
121+ reply .raise_for_status ()
122+ except requests .exceptions .ConnectionError :
123+ raise BrokerError ('Failure connecting to comet.broker at {}{}: make sure it is '
124+ 'running.' .format (self .broker , endpoint ))
125+ reply = reply .json ()
126+ self ._check_result (reply .get ('result' ), endpoint )
127+ return reply
128+
121129 def _send_state (self , state_id , state ):
122130 self .logger .debug ('sending state {}' .format (state_id ))
123131 request = {'hash' : state_id , 'state' : state }
124- r = requests .post (self .broker + SEND_STATE , data = json .dumps (request ))
125- r .raise_for_status ()
126- r = r .json ()
127- self ._check_result (r .get ('result' ), SEND_STATE )
132+ self ._send (SEND_STATE , request )
128133
129134 def _check_result (self , result , endpoint ):
130135 if result != 'success' :
0 commit comments