It feels a bit weird to pass None as first argument, the casual reader is probably going to wonder what that means, especially considering that this is the only use of discover_server() in non-test code.
If the resolver argument is used for testing only, please consider moving it at the end of the arguments list:
Not really related to this branch, but while I was reading some context, I've noticed that the ping method has grown a bit:
def ping(self):
"""Ask the question.
Hits the URL previously specified with the insecure_id gotten
from the identity.
@return: A deferred resulting in True if there are messages
and False otherwise.
"""
def handle_result(result):
if result is None: logging.warning("Autodiscovery failed. Reverting to previous " "settings.")
else: result = "http://%s/ping" % result
return result
def do_rest(result):
if result is not None: self.url = result
insecure_id = self._identity.insecure_id
if insecure_id is not None: headers = {"Content-Type": "application/x-www-form-urlencoded"} data = urllib.urlencode({"insecure_id": insecure_id}) page_deferred = defer.Deferred()
What do you think of splitting it in methods like:
def ping(self):
"""Ask the question.
Hits the URL previously specified with the insecure_id gotten
from the identity.
@return: A deferred resulting in True if there are messages
and False otherwise.
"""
if self._server_autodiscover: deferred = discover_server( None, self._autodiscover_srv_query_string, self._autodiscover_a_query_string) deferred.addCallback(self._discover_server_success)
else: deferred = defer.succeed(None) deferred.addCallback(self._do_ping)
return deferred
def _discover_server_success(self, result):
if result is None: logging.warning("Autodiscovery failed. Reverting to previous " "settings.")
else:
result = "http://%s/ping" % result
return result
def _do_ping(self, url):
if url is not None: self.url = url
insecure_id = self._identity.insecure_id
if insecure_id is not None:
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = urllib.urlencode({"insecure_id": insecure_id}) page_deferred = defer.Deferred()
Looks good, only a couple of points about style. +1!
[1]
+def discover_ server( resolver= None, autodiscover_ srv_query_ string= "", a_query_ string= ""):
+ autodiscover_
and
+ lookup_deferred = discover_server( ver_srv_ query_string, ver_a_query_ string)
+ None, self._autodisco
+ self._autodisco
It feels a bit weird to pass None as first argument, the casual reader is probably going to wonder what that means, especially considering that this is the only use of discover_server() in non-test code.
If the resolver argument is used for testing only, please consider moving it at the end of the arguments list:
def discover_ server( autodiscover_ srv_query_ string= "",
autodisco ver_a_query_ string= "", resolver=None):
and use it like:
+ lookup_deferred = discover_server( ver_srv_ query_string, ver_a_query_ string)
+ self._autodisco
+ self._autodisco
[2]
Not really related to this branch, but while I was reading some context, I've noticed that the ping method has grown a bit:
def ping(self):
"""Ask the question.
Hits the URL previously specified with the insecure_id gotten
from the identity.
@return: A deferred resulting in True if there are messages result( result) :
logging. warning( "Autodiscovery failed. Reverting to previous "
"settings. ")
result = "http:// %s/ping" % result
and False otherwise.
"""
def handle_
if result is None:
else:
return result
def do_rest(result):
self. url = result
if result is not None:
if insecure_id is not None:
def errback(type, value, tb):
page_ deferred. errback( Failure( value, type, tb))
self. _reactor. call_in_ thread( page_deferred. callback, errback,
self. get_page, self.url,
post= True, data=data,
headers= headers)
page_ deferred. addCallback( self._got_ result)
return page_deferred False)
return defer.succeed(
if self._server_ autodiscover:
lookup_ deferred = discover_server(
None, self._autodisco ver_srv_ query_string,
self. _autodiscover_ a_query_ string)
lookup_ deferred. addCallback( handle_ result)
lookup_ deferred. addCallback( do_rest)
return lookup_deferred
else:
return do_rest(None)
What do you think of splitting it in methods like:
def ping(self):
"""Ask the question.
Hits the URL previously specified with the insecure_id gotten
from the identity.
@return: A deferred resulting in True if there are messages autodiscover:
deferred = discover_server(
None, self._autodisco ver_srv_ query_string,
self. _autodiscover_ a_query_ string)
deferred. addCallback( self._discover_ server_ success)
deferred = defer.succeed(None)
deferred. addCallback( self._do_ ping)
and False otherwise.
"""
if self._server_
else:
return deferred
def _discover_ server_ success( self, result):
logging. warning( "Autodiscovery failed. Reverting to previous "
"settings. ") %s/ping" % result
if result is None:
else:
result = "http://
return result
def _do_ping(self, url):
self. url = url
if url is not None:
insecure_id = self._identity. insecure_ id x-www-form- urlencoded" } urlencode( {"insecure_ id": insecure_id})
page_ deferred = defer.Deferred()
if insecure_id is not None:
headers = {"Content-Type": "application/
data = urllib.
def errback(type, value, tb):
page_ deferred. errback( Failure( value, type, tb))
self. _reactor. call_in_ thread( page_deferred. callback, errback,
self. get_page, self.url,
post= True, data=data,
headers= headers)
page_ deferred. addCallback( self._got_ result) False)
return page_deferred
return defer.succeed(
Note that also renamed do_rest to do_ping, as it's not super clear what "rest" is.