Package grassyknoll :: Package client :: Module ClientCollection
[hide private]

Source Code for Module grassyknoll.client.ClientCollection

 1  from grassyknoll.collection import Collection 
 2  from grassyknoll.serial import JsonSerial 
 3  import RestClient 
 4   
5 -class ClientError(StandardError):
6 """Error raise by L{ClientCollection} 7 8 XXX this should die and get rolled up into concurrent.errors""" 9 pass
10
11 -class ClientCollection(Collection.Collection):
12 """a collection that is a client to another remote collection, over HTTP. 13 14 @ivar restclient: A client to a server collection 15 @type restclient: L{RestClient.RestClient} 16 """ 17
18 - def __init__(self, base_uri="http://localhost:8080/"):
19 """ 20 @arg base_uri: url to the server. Must end in / 21 @type base_uri: string 22 """ 23 self.restclient=RestClient.RestClient(base_uri, mimetype='application/json')
24 25 @Collection.addMetaData
26 - def create(self, docs):
27 docs=[dict(d) for d in docs] 28 response, content=self.restclient.createMany(docs) 29 30 if response.status != 201: raise ClientError(response.status, response.reason) 31 obj=JsonSerial.loads(content) 32 return Collection.CollectionIds(items=obj['ids'], metadata=obj['metadata'])
33 34 @Collection.addMetaData
35 - def delete(self, ids):
36 response, content=self.restclient.deleteMany(ids) 37 if response.status != 200: raise ClientError(response.status, response.reason) 38 39 obj=JsonSerial.loads(content) 40 return Collection.CollectionIds(items=obj['ids'], metadata=obj['metadata'])
41 42 @Collection.addMetaData
43 - def list(self):
44 response, content=self.restclient.list() 45 if response.status != 200: raise ClientError(response.status, response.reason) 46 47 obj=JsonSerial.loads(content) 48 return Collection.CollectionIds(items=obj['ids'], metadata=obj['metadata'])
49 50 @Collection.addMetaData
51 - def retrieve(self, ids, fields=None):
52 if fields is not None and '__id__' not in fields: fields+=('__id__',) 53 response, content=self.restclient.retrieveMany(ids, fields) 54 if response.status != 200: raise ClientError(response.status, response.reason) 55 56 obj=JsonSerial.loads(content) 57 results=Collection.CollectionResultSet(items=(Collection.CollectionResult(r) 58 for r in obj['results']), 59 metadata=obj['metadata']) 60 # XXX delete extra __url__ ? 61 for r in results: 62 del r['__url__'] 63 return results
64 65 # XXX a better way to do this would be to load the list of available 66 # queries at creation time, by GETting /__query__/. Those names could then 67 # be bound as methods on the instance.
68 - def __getattr__(self, name):
69 if name.endswith('Query'): 70 return lambda **kwargs: self.__query(name[:-5], **kwargs) 71 else: 72 raise AttributeError(name)
73 74 @Collection.addMetaData
75 - def __query(self, name, **kwargs):
76 response, content=self.restclient.query(name, **kwargs) 77 78 if response.status != 200: raise ClientError(response.status, response.reason) 79 80 obj=JsonSerial.loads(content) 81 results=Collection.CollectionResultSet(items=(Collection.CollectionResult(r) 82 for r in obj['results']), 83 metadata=obj['metadata']) 84 # XXX delete extra __url__ ? 85 for r in results: 86 del r['__url__'] 87 return results
88