import httplib
import logging

from lxml import etree
from urlparse import urlparse

logger = logging.getLogger(__name__)

def xml_from_string(xmlstr):
    """Creates an XML document from a string"""
    if xmlstr == None or xmlstr == "":
        return None
    return etree.fromstring(xmlstr)

def send_request(uri, request_handler, response_handler, method, data=None):
    parseduri = urlparse(uri)
    connection = httplib.HTTPConnection(parseduri.netloc)
    try:
        request_handler(connection, parseduri, data)
        response = connection.getresponse()
        logger.info("Response was %s", response.reason)
        return response_handler(response)
    except httplib.HTTPException:
        logger.exception("%s failed for %s", method, uri)
        raise

def get_resource(uri):
    """Return a string representation of a resource"""

    def request_handler(connection, uri, *args):
        connection.request("GET", uri.path + "?" + uri.query)

    def response_handler(response):
        if response.status == httplib.OK:
            return response.read()
        elif response.status == httplib.NO_CONTENT:
            return True
        elif response.status == httplib.INTERNAL_SERVER_ERROR:
            return None
        else:
            return None

    return send_request(uri, request_handler, response_handler, "GET")

def get_resource_as_xml(uri):
    """Return an XML document representing a resource"""
    resource = get_resource(uri)
    if resource != None:
        return xml_from_string(resource)
    else:
        return None

def post_resource(uri, data):
    """Post a new resource"""

    def request_handler(connection, uri, data):
        connection.request("POST", uri.path + "?" + uri.query, data)

    def response_handler(response):
        if response.status == httplib.CREATED:
            return xml_from_string(response.read())
        elif response.status == httplib.INTERNAL_SERVER_ERROR:
            return None
        else:
            return None

    return send_request(uri, request_handler, response_handler, "POST", data=data)

def delete_resource(uri):
    """Delete a resource from the server"""

    def request_handler(connection, uri, *args):
        connection.request("DELETE", uri.path + "?" + uri.query)

    def response_handler(response):
        if response.status == httplib.NO_CONTENT:
            return True
        elif response.status == httplib.INTERNAL_SERVER_ERROR:
            return None
        else:
            return None

    return send_request(uri, request_handler, response_handler, "DELETE")

@@ -149,115 +149,8 @@
 r):%0A
-    %22%22%22Creates an XML document from a string%22%22%22%0A    if xmlstr == None or xmlstr == %22%22:%0A        return None%0A
     
@@ -180,16 +180,36 @@
 (xmlstr)
+ if xmlstr else None
 %0A%0Adef se
@@ -996,174 +996,85 @@
   el
-if response.status == httplib.NO_CONTENT:%0A            return True%0A        elif response.status == httplib.INTERNAL_SERVER_ERROR:%0A            return None%0A        else:
+se:%0A            logger.warn(%22Unexpected Server Response %25s%22, response.status)
 %0A   
@@ -1291,37 +1291,8 @@
 ri)%0A
-    if resource != None:%0A    
     
@@ -1327,38 +1327,8 @@
 rce)
-%0A    else:%0A        return None
 %0A%0Ade
@@ -1653,98 +1653,85 @@
   el
-if response.status == httplib.INTERNAL_SERVER_ERROR:%0A            return None%0A        else:
+se:%0A            logger.warn(%22Unexpected Server Response %25s%22, response.status)
 %0A   
@@ -2146,98 +2146,85 @@
   el
-if response.status == httplib.INTERNAL_SERVER_ERROR:%0A            return None%0A        else:
+se:%0A            logger.warn(%22Unexpected Server Response %25s%22, response.status)
 %0A   

Simplify the error handling