11import json
22import logging
3- from copy import deepcopy
4- from sseclient import SSEClient
53from collections .abc import Hashable , Iterable
4+ from copy import deepcopy
65from urllib .parse import quote as urlquote
76
7+ from sseclient import SSEClient
8+
89
910class DB ():
1011 def __init__ (self , client , params , ** kwargs ):
@@ -36,6 +37,10 @@ def cache_remove(self, item):
3637 if item in self .__cache :
3738 del self .__cache [item ]
3839
40+ @property
41+ def cached (self ):
42+ return self .__use_cache
43+
3944 @property
4045 def index_by (self ):
4146 return self .__index_by
@@ -52,36 +57,45 @@ def params(self):
5257 def dbname (self ):
5358 return self .__dbname
5459
60+ @property
61+ def id (self ):
62+ return self .__id
63+
5564 @property
5665 def dbtype (self ):
5766 return self .__type
5867
68+ @property
69+ def capabilities (self ):
70+ return deepcopy (self .__params .get ('capabilities' , []))
71+
5972 @property
6073 def queryable (self ):
61- return 'query' in self .__params .get ('capabilities' , {})
74+ return 'query' in self .__params .get ('capabilities' , [])
75+
6276 @property
6377 def putable (self ):
64- return 'put' in self .__params .get ('capabilities' , {} )
78+ return 'put' in self .__params .get ('capabilities' , [] )
6579
6680 @property
6781 def removeable (self ):
68- return 'remove' in self .__params .get ('capabilities' , {} )
82+ return 'remove' in self .__params .get ('capabilities' , [] )
6983
7084 @property
7185 def iterable (self ):
72- return 'iterator' in self .__params .get ('capabilities' , {} )
86+ return 'iterator' in self .__params .get ('capabilities' , [] )
7387
7488 @property
7589 def addable (self ):
76- return 'add' in self .__params .get ('capabilities' , {} )
90+ return 'add' in self .__params .get ('capabilities' , [] )
7791
7892 @property
7993 def valuable (self ):
80- return 'value' in self .__params .get ('capabilities' , {} )
94+ return 'value' in self .__params .get ('capabilities' , [] )
8195
8296 @property
8397 def incrementable (self ):
84- return 'inc' in self .__params .get ('capabilities' , {} )
98+ return 'inc' in self .__params .get ('capabilities' , [] )
8599
86100 @property
87101 def indexed (self ):
@@ -97,7 +111,7 @@ def write_access(self):
97111
98112 def info (self ):
99113 endpoint = '/' .join (['db' , self .__id_safe ])
100- return self .__client ._call ('get ' , endpoint )
114+ return self .__client ._call ('GET ' , endpoint )
101115
102116 def get (self , item , cache = None , unpack = False ):
103117 if cache is None : cache = self .__use_cache
@@ -106,7 +120,7 @@ def get(self, item, cache=None, unpack=False):
106120 result = self .__cache [item ]
107121 else :
108122 endpoint = '/' .join (['db' , self .__id_safe , item ])
109- result = self .__client ._call ('get ' , endpoint )
123+ result = self .__client ._call ('GET ' , endpoint )
110124 if cache : self .__cache [item ] = result
111125 if isinstance (result , Hashable ): return deepcopy (result )
112126 if isinstance (result , Iterable ): return deepcopy (result )
@@ -117,13 +131,13 @@ def get(self, item, cache=None, unpack=False):
117131
118132 def get_raw (self , item ):
119133 endpoint = '/' .join (['db' , self .__id_safe , 'raw' , str (item )])
120- return (self .__client ._call ('get ' , endpoint ))
134+ return (self .__client ._call ('GET ' , endpoint ))
121135
122136 def put (self , item , cache = None ):
123137 if self .__enforce_caps and not self .putable :
124- raise CapabilityError ('Db {} does not have put capability' . format ( self . __dbname ) )
125- if self .indexed and (not hasattr ( item , self .__index_by ) ) and self .__enforce_indexby :
126- raise MissingIndexError ("The provided document doesn't contain field '{}'" . format ( self .__index_by ) )
138+ raise CapabilityError (f 'Db { self . __dbname } does not have put capability' )
139+ if self .indexed and (not self .__index_by in item ) and self .__enforce_indexby :
140+ raise MissingIndexError (f "The provided document { item } doesn't contain field '{ self .__index_by } '" )
127141
128142 if cache is None : cache = self .__use_cache
129143 if cache :
@@ -134,59 +148,77 @@ def put(self, item, cache=None):
134148 if index_val :
135149 self .__cache [index_val ] = item
136150 endpoint = '/' .join (['db' , self .__id_safe , 'put' ])
137- entry_hash = self .__client ._call ('post ' , endpoint , item ).get ('hash' )
151+ entry_hash = self .__client ._call ('POST ' , endpoint , json = item ).get ('hash' )
138152 if cache and entry_hash : self .__cache [entry_hash ] = item
139153 return entry_hash
140154
141155 def add (self , item , cache = None ):
142156 if self .__enforce_caps and not self .addable :
143- raise CapabilityError ('Db {} does not have add capability' . format ( self . __dbname ) )
157+ raise CapabilityError (f 'Db { self . __dbname } does not have add capability' )
144158 if cache is None : cache = self .__use_cache
145159 endpoint = '/' .join (['db' , self .__id_safe , 'add' ])
146- entry_hash = self .__client ._call ('post ' , endpoint , item ).get ('hash' )
160+ entry_hash = self .__client ._call ('POST ' , endpoint , json = item ).get ('hash' )
147161 if cache and entry_hash : self .__cache [entry_hash ] = item
148162 return entry_hash
149163
164+ def inc (self , val ):
165+ val = int (val )
166+ endpoint = '/' .join (['db' , self .__id_safe , 'inc' ])
167+ return self .__client ._call ('POST' , endpoint , json = {'val' :val })
168+
169+ def value (self ):
170+ endpoint = '/' .join (['db' , self .__id_safe , 'value' ])
171+ return self .__client ._call ('GET' , endpoint )
172+
150173 def iterator_raw (self , ** kwargs ):
151174 if self .__enforce_caps and not self .iterable :
152- raise CapabilityError ('Db {} does not have iterator capability' . format ( self . __dbname ) )
175+ raise CapabilityError (f 'Db { self . __dbname } does not have iterator capability' )
153176 endpoint = '/' .join (['db' , self .__id_safe , 'rawiterator' ])
154- return self .__client ._call ('get ' , endpoint , kwargs )
177+ return self .__client ._call ('GET ' , endpoint , json = kwargs )
155178
156179 def iterator (self , ** kwargs ):
157180 if self .__enforce_caps and not self .iterable :
158- raise CapabilityError ('Db {} does not have iterator capability' . format ( self . __dbname ) )
181+ raise CapabilityError (f 'Db { self . __dbname } does not have iterator capability' )
159182 endpoint = '/' .join (['db' , self .__id_safe , 'iterator' ])
160- return self .__client ._call ('get ' , endpoint , kwargs )
183+ return self .__client ._call ('GET ' , endpoint , json = kwargs )
161184
162185 def index (self ):
163186 endpoint = '/' .join (['db' , self .__id_safe , 'index' ])
164- result = self .__client ._call ('get ' , endpoint )
187+ result = self .__client ._call ('GET ' , endpoint )
165188 return result
166189
167190 def all (self ):
168191 endpoint = '/' .join (['db' , self .__id_safe , 'all' ])
169- result = self .__client ._call ('get ' , endpoint )
192+ result = self .__client ._call ('GET ' , endpoint )
170193 if isinstance (result , Hashable ):
171194 self .__cache = result
172195 return result
173196
174197 def remove (self , item ):
175198 if self .__enforce_caps and not self .removeable :
176- raise CapabilityError ('Db {} does not have remove capability' . format ( self . __dbname ) )
199+ raise CapabilityError (f 'Db { self . __dbname } does not have remove capability' )
177200 item = str (item )
178201 endpoint = '/' .join (['db' , self .__id_safe , item ])
179- return self .__client ._call ('delete ' , endpoint )
202+ return self .__client ._call ('DELETE ' , endpoint )
180203
181204 def unload (self ):
182205 endpoint = '/' .join (['db' , self .__id_safe ])
183- return self .__client ._call ('delete ' , endpoint )
206+ return self .__client ._call ('DELETE ' , endpoint )
184207
185208 def events (self , eventname ):
186209 endpoint = '/' .join (['db' , self .__id_safe , 'events' , urlquote (eventname , safe = '' )])
187- #return SSEClient('{}/{}'.format(self.__client.base_url, endpoint), session=self.__client.session)
188- req = self .__client ._call_raw ('get' , endpoint , stream = True )
189- return SSEClient (req ).events ()
210+ res = self .__client ._call_raw ('GET' , endpoint , stream = True )
211+ res .raise_for_status ()
212+ return SSEClient (res .stream ()).events ()
213+
214+ def findPeers (self , ** kwargs ):
215+ endpoint = '/' .join (['peers' ,'searches' ,'db' , self .__id_safe ])
216+ return self .__client ._call ('POST' , endpoint , json = kwargs )
217+
218+ def getPeers (self ):
219+ endpoint = '/' .join (['db' , self .__id_safe , 'peers' ])
220+ return self .__client ._call ('GET' , endpoint )
221+
190222
191223class CapabilityError (Exception ):
192224 pass
0 commit comments