Commit 5ecc02c2 authored by Marco Mariani's avatar Marco Mariani

directly return content from GET() and POST() - no need for self.response

parent 6144a4b3
...@@ -90,13 +90,12 @@ class SlapRequester(SlapDocument): ...@@ -90,13 +90,12 @@ class SlapRequester(SlapDocument):
""" """
def _requestComputerPartition(self, request_dict): def _requestComputerPartition(self, request_dict):
try: try:
self._connection_helper.POST('/requestComputerPartition', request_dict) xml = self._connection_helper.POST('/requestComputerPartition', request_dict)
except ResourceNotReady: except ResourceNotReady:
return ComputerPartition( return ComputerPartition(
request_dict=request_dict, request_dict=request_dict,
connection_helper=self._connection_helper, connection_helper=self._connection_helper,
) )
xml = self._connection_helper.response.read()
software_instance = xml_marshaller.loads(xml) software_instance = xml_marshaller.loads(xml)
computer_partition = ComputerPartition( computer_partition = ComputerPartition(
software_instance.slap_computer_id.encode('UTF-8'), software_instance.slap_computer_id.encode('UTF-8'),
...@@ -252,9 +251,8 @@ class OpenOrder(SlapRequester): ...@@ -252,9 +251,8 @@ class OpenOrder(SlapRequester):
""" """
Requests a computer. Requests a computer.
""" """
self._connection_helper.POST('/requestComputer', xml = self._connection_helper.POST('/requestComputer',
{'computer_title': computer_reference}) {'computer_title': computer_reference})
xml = self._connection_helper.response.read()
computer = xml_marshaller.loads(xml) computer = xml_marshaller.loads(xml)
computer._connection_helper = self._connection_helper computer._connection_helper = self._connection_helper
return computer return computer
...@@ -316,9 +314,8 @@ class Computer(SlapDocument): ...@@ -316,9 +314,8 @@ class Computer(SlapDocument):
'use_string': computer_usage}) 'use_string': computer_usage})
def updateConfiguration(self, xml): def updateConfiguration(self, xml):
self._connection_helper.POST( return self._connection_helper.POST(
'/loadComputerConfigurationFromXML', { 'xml' : xml }) '/loadComputerConfigurationFromXML', { 'xml' : xml })
return self._connection_helper.response.read()
def bang(self, message): def bang(self, message):
self._connection_helper.POST('/computerBang', { self._connection_helper.POST('/computerBang', {
...@@ -326,18 +323,17 @@ class Computer(SlapDocument): ...@@ -326,18 +323,17 @@ class Computer(SlapDocument):
'message': message}) 'message': message})
def getStatus(self): def getStatus(self):
self._connection_helper.GET( xml = self._connection_helper.GET(
'/getComputerStatus?computer_id=%s' % self._computer_id) '/getComputerStatus?computer_id=%s' % self._computer_id)
return xml_marshaller.loads(self._connection_helper.response.read()) return xml_marshaller.loads(xml)
def revokeCertificate(self): def revokeCertificate(self):
self._connection_helper.POST('/revokeComputerCertificate', { self._connection_helper.POST('/revokeComputerCertificate', {
'computer_id': self._computer_id}) 'computer_id': self._computer_id})
def generateCertificate(self): def generateCertificate(self):
self._connection_helper.POST('/generateComputerCertificate', { xml = self._connection_helper.POST('/generateComputerCertificate', {
'computer_id': self._computer_id}) 'computer_id': self._computer_id})
xml = self._connection_helper.response.read()
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
...@@ -521,16 +517,16 @@ class ComputerPartition(SlapRequester): ...@@ -521,16 +517,16 @@ class ComputerPartition(SlapRequester):
self.usage = usage_log self.usage = usage_log
def getCertificate(self): def getCertificate(self):
self._connection_helper.GET( xml = self._connection_helper.GET(
'/getComputerPartitionCertificate?computer_id=%s&' '/getComputerPartitionCertificate?computer_id=%s&'
'computer_partition_id=%s' % (self._computer_id, self._partition_id)) 'computer_partition_id=%s' % (self._computer_id, self._partition_id))
return xml_marshaller.loads(self._connection_helper.response.read()) return xml_marshaller.loads(xml)
def getStatus(self): def getStatus(self):
self._connection_helper.GET( xml = self._connection_helper.GET(
'/getComputerPartitionStatus?computer_id=%s&' '/getComputerPartitionStatus?computer_id=%s&'
'computer_partition_id=%s' % (self._computer_id, self._partition_id)) 'computer_partition_id=%s' % (self._computer_id, self._partition_id))
return xml_marshaller.loads(self._connection_helper.response.read()) return xml_marshaller.loads(xml)
class ConnectionHelper: class ConnectionHelper:
error_message_timeout = "\nThe connection timed out. Please try again later." error_message_timeout = "\nThe connection timed out. Please try again later."
...@@ -551,8 +547,8 @@ class ConnectionHelper: ...@@ -551,8 +547,8 @@ class ConnectionHelper:
self.timeout = timeout self.timeout = timeout
def getComputerInformation(self, computer_id): def getComputerInformation(self, computer_id):
self.GET('/getComputerInformation?computer_id=%s' % computer_id) xml = self.GET('/getComputerInformation?computer_id=%s' % computer_id)
return xml_marshaller.loads(self.response.read()) return xml_marshaller.loads(xml)
def getFullComputerInformation(self, computer_id): def getFullComputerInformation(self, computer_id):
""" """
...@@ -564,13 +560,13 @@ class ConnectionHelper: ...@@ -564,13 +560,13 @@ class ConnectionHelper:
# XXX-Cedric: should raise something smarter than "NotFound". # XXX-Cedric: should raise something smarter than "NotFound".
raise NotFoundError(method) raise NotFoundError(method)
try: try:
self.GET(method) xml = self.GET(method)
except NotFoundError: except NotFoundError:
# XXX: This is a ugly way to keep backward compatibility, # XXX: This is a ugly way to keep backward compatibility,
# We should stablise slap library soon. # We should stablise slap library soon.
self.GET('/getComputerInformation?computer_id=%s' % computer_id) xml = self.GET('/getComputerInformation?computer_id=%s' % computer_id)
return xml_marshaller.loads(self.response.read()) return xml_marshaller.loads(xml)
def connect(self): def connect(self):
connection_dict = dict( connection_dict = dict(
...@@ -590,7 +586,7 @@ class ConnectionHelper: ...@@ -590,7 +586,7 @@ class ConnectionHelper:
try: try:
self.connect() self.connect()
self.connection.request('GET', self.path + path) self.connection.request('GET', self.path + path)
self.response = self.connection.getresponse() response = self.connection.getresponse()
# If ssl error : may come from bad configuration # If ssl error : may come from bad configuration
except ssl.SSLError, e: except ssl.SSLError, e:
if e.message == "The read operation timed out": if e.message == "The read operation timed out":
...@@ -600,22 +596,25 @@ class ConnectionHelper: ...@@ -600,22 +596,25 @@ class ConnectionHelper:
if e.message == "timed out": if e.message == "timed out":
raise socket.error(str(e) + self.error_message_timeout) raise socket.error(str(e) + self.error_message_timeout)
raise socket.error(self.error_message_connect_fail + str(e)) raise socket.error(self.error_message_connect_fail + str(e))
# check self.response.status and raise exception early # check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT: if response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready # resource is not ready
raise ResourceNotReady(path) raise ResourceNotReady(path)
elif self.response.status == httplib.NOT_FOUND: elif response.status == httplib.NOT_FOUND:
raise NotFoundError(path) raise NotFoundError(path)
elif self.response.status == httplib.FORBIDDEN: elif response.status == httplib.FORBIDDEN:
raise Unauthorized(path) raise Unauthorized(path)
elif self.response.status != httplib.OK: elif response.status != httplib.OK:
message = parsed_error_message(self.response.status, message = parsed_error_message(response.status,
self.response.read(), response.read(),
path) path)
raise ServerError(message) raise ServerError(message)
finally: finally:
socket.setdefaulttimeout(default_timeout) socket.setdefaulttimeout(default_timeout)
return response.read()
def POST(self, path, parameter_dict, def POST(self, path, parameter_dict,
content_type="application/x-www-form-urlencoded"): content_type="application/x-www-form-urlencoded"):
try: try:
...@@ -631,23 +630,27 @@ class ConnectionHelper: ...@@ -631,23 +630,27 @@ class ConnectionHelper:
raise ssl.SSLError(self.ssl_error_message_connect_fail + str(e)) raise ssl.SSLError(self.ssl_error_message_connect_fail + str(e))
except socket.error, e: except socket.error, e:
raise socket.error(self.error_message_connect_fail + str(e)) raise socket.error(self.error_message_connect_fail + str(e))
self.response = self.connection.getresponse()
response = self.connection.getresponse()
# check self.response.status and raise exception early # check self.response.status and raise exception early
if self.response.status == httplib.REQUEST_TIMEOUT: if response.status == httplib.REQUEST_TIMEOUT:
# resource is not ready # resource is not ready
raise ResourceNotReady("%s - %s" % (path, parameter_dict)) raise ResourceNotReady("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.NOT_FOUND: elif response.status == httplib.NOT_FOUND:
raise NotFoundError("%s - %s" % (path, parameter_dict)) raise NotFoundError("%s - %s" % (path, parameter_dict))
elif self.response.status == httplib.FORBIDDEN: elif response.status == httplib.FORBIDDEN:
raise Unauthorized("%s - %s" % (path, parameter_dict)) raise Unauthorized("%s - %s" % (path, parameter_dict))
elif self.response.status != httplib.OK: elif response.status != httplib.OK:
message = parsed_error_message(self.response.status, message = parsed_error_message(response.status,
self.response.read(), response.read(),
path) path)
raise ServerError(message) raise ServerError(message)
finally: finally:
socket.setdefaulttimeout(default_timeout) socket.setdefaulttimeout(default_timeout)
return response.read()
class slap: class slap:
zope.interface.implements(interface.slap) zope.interface.implements(interface.slap)
...@@ -699,10 +702,10 @@ class slap: ...@@ -699,10 +702,10 @@ class slap:
# XXX-Cedric: should raise something smarter than NotFound # XXX-Cedric: should raise something smarter than NotFound
raise NotFoundError raise NotFoundError
self._connection_helper.GET('/registerComputerPartition?' \ xml = self._connection_helper.GET('/registerComputerPartition?' \
'computer_reference=%s&computer_partition_reference=%s' % ( 'computer_reference=%s&computer_partition_reference=%s' % (
computer_guid, partition_id)) computer_guid, partition_id))
result = xml_marshaller.loads(self._connection_helper.response.read()) result = xml_marshaller.loads(xml)
# XXX: dirty hack to make computer partition usable. xml_marshaller is too # XXX: dirty hack to make computer partition usable. xml_marshaller is too
# low-level for our needs here. # low-level for our needs here.
result._connection_helper = self._connection_helper result._connection_helper = self._connection_helper
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment