from requests.exceptions import ConnectionError
import requests
import urlparse

class _Request(object):
    """
    Issues requests to the collections API
    """
    def __init__(self,connection):
        self.connection = connection
        self.client = requests.Session()

    def request(self,path,params,method='GET',body=None):
        headers = {'content-type': 'application/json'}
        params['wt'] = 'json'

        servers = list(self.connection.servers)
        host = servers.pop(0)

        def make_request(host,path):
            fullpath = urlparse.urljoin(host,path)
            try:
                r = self.client.request(method,fullpath,
                                        params=params,
                                        headers=headers,data=body)

                if r.status_code == requests.codes.ok:
                    response = r.json()
                else:
                    response = r.text
                return response

            except ConnectionError:
                host = servers.pop(0)
                return make_request(host,path)

        result = make_request(host,path)
        return result

    def update(self,path,params,body):
        return self.request(path,params,'POST',body)

    def get(self,path,params):
        return self.request(path,params,method='GET')



class DictObject(object):
    '''Recursive class for building and representing objects with'''
    def __init__(self, obj):
        if not obj:
            return

        for k, v in obj.iteritems():
            if isinstance(v, dict):
                setattr(self, k, DictObject(v))
            else:
                setattr(self, k.encode('utf8','ignore'), v)

    def __getitem__(self, val):
        return self.__dict__[val]

    def __repr__(self):
        value = str(', '.join('%s : %s' % (k, repr(v)) for
                              (k, v) in self.__dict__.iteritems()))
        return 'DictObject %s' % value

class SolrResponse(DictObject):
    """ A generic representation of a solr response """
    def __repr__(self):
        value = str(', '.join('%s : %s' % (k, repr(v)) for
                              (k, v) in self.__dict__.iteritems()))
        return 'SolrResponse <%s>' % value

class SolrException(Exception):
    pass

@@ -72,16 +72,28 @@
 urlparse
+%0Aimport json
 %0A%0Aclass 
@@ -1344,18 +1344,16 @@
 'GET')%0A%0A
-%0A%0A
 class Di
@@ -1819,176 +1819,182 @@
 e = 
-str(', '.join('%25s : %25s' %25 (k, repr(v)) for%0A                              (k, v) in self.__dict__.iteritems()))%0A        return 'DictObject %25s' %25 value%0A%0Aclass SolrRespons
+DictObjectJSONEncoder(indent=4).encode(self.__dict__)%0A        return value%0A%0Aclass DictObjectJSONEncoder(json.JSONEncoder):%0A    def default(self, o):%0A        if type(o) == typ
 e(Di
@@ -2005,260 +2005,409 @@
 ject
+(%7B%7D)
 ):%0A    
-%22%22%22 A generic representation of a solr response %22%22%22%0A    def __repr__(self):%0A        value = str(', '.join('%25s : %25s' %25 (k, repr(v)) for%0A                              (k, v) in self.__dict__.iteritems()))%0A        return 'SolrResponse %3C%25s%3E' %25 value
+        val = str(o.__dict__)%0A            if len(val) %3E 200:%0A                s = val%5B:100%5D + ' ... '%0A            else:%0A                s = val%0A            return %22DictObject %3C%3C %25s %3E%3E%22 %25 s%0A%0A        # Let the base class default method raise the TypeError%0A        return json.JSONEncoder.default(self, o)%0A%0Aclass SolrResponse(DictObject):%0A    %22%22%22 A generic representation of a solr response %22%22%22
 %0A%0Acl

improve pretty-print of solr responses