7
7
from urllib .parse import quote_plus
8
8
import requests_unixsocket
9
9
from jumpscale .core .base import fields
10
+
11
+
10
12
class Object (NamedTuple ):
11
13
id : int
12
14
data : bytes
13
15
tags : dict
14
16
15
17
@property
16
18
def acl (self ):
17
- return int (self .tags [' :acl' ]) if ' :acl' in self .tags else None
19
+ return int (self .tags [" :acl" ]) if " :acl" in self .tags else None
18
20
19
21
@property
20
22
def size (self ):
21
- return int (self .tags [' :size' ]) if ' :size' in self .tags else 0
23
+ return int (self .tags [" :size" ]) if " :size" in self .tags else 0
22
24
23
25
@property
24
26
def created (self ):
25
- return int (self .tags [' :created' ]) if ' :created' in self .tags else 0
27
+ return int (self .tags [" :created" ]) if " :created" in self .tags else 0
26
28
27
29
@property
28
30
def updated (self ):
29
- return int (self .tags [':updated' ]) if ':updated' in self .tags else 0
30
-
31
+ return int (self .tags [":updated" ]) if ":updated" in self .tags else 0
31
32
32
33
33
34
class HTTPClient (BaseClient ):
34
35
sock = fields .String (default = "/tmp/bcdb.sock" )
36
+
35
37
def __init__ (self , * args , ** kwargs ):
36
38
super ().__init__ (* args , ** kwargs )
37
- url = ' http+unix://%s/' % quote_plus (self .sock )
39
+ url = " http+unix://%s/" % quote_plus (self .sock )
38
40
self .__session = requests_unixsocket .Session ()
39
41
self .__url = url
40
42
@@ -47,7 +49,7 @@ def headers(self, **args):
47
49
for k , v in args .items ():
48
50
if v is None :
49
51
continue
50
- k = k .replace ('_' , '-' ).lower ()
52
+ k = k .replace ("_" , "-" ).lower ()
51
53
output [k ] = str (v )
52
54
53
55
return output
@@ -88,10 +90,7 @@ def create(self, perm, users):
88
90
:returns: newly created key
89
91
"""
90
92
91
- data = {
92
- "perm" : perm ,
93
- 'users' : users
94
- }
93
+ data = {"perm" : perm , "users" : users }
95
94
96
95
return self .session .post (self .url (), json = data , headers = self .headers ()).json ()
97
96
@@ -104,9 +103,7 @@ def set(self, key, perm):
104
103
:returns: new object id
105
104
"""
106
105
107
- data = {
108
- 'perm' : perm
109
- }
106
+ data = {"perm" : perm }
110
107
111
108
self .session .put (self .url (key ), json = data , headers = self .headers ())
112
109
@@ -129,9 +126,7 @@ def grant(self, key, users):
129
126
:returns: updated id
130
127
"""
131
128
132
- data = {
133
- 'users' : users
134
- }
129
+ data = {"users" : users }
135
130
136
131
return self .session .post (self .url (f"{ key } /grant" ), json = data , headers = self .headers ()).json ()
137
132
@@ -144,9 +139,7 @@ def revoke(self, key, users):
144
139
:returns: updated id
145
140
"""
146
141
147
- data = {
148
- 'users' : users
149
- }
142
+ data = {"users" : users }
150
143
151
144
return self .session .post (self .url (f"{ key } /revoke" ), json = data , headers = self .headers ()).json ()
152
145
@@ -156,8 +149,7 @@ def list(self):
156
149
157
150
:returns: acl list
158
151
"""
159
- response = self .session .get (
160
- self .url (), headers = self .headers ())
152
+ response = self .session .get (self .url (), headers = self .headers ())
161
153
162
154
# this should instead read response "stream" and parse each object individually
163
155
content = response .text
@@ -219,9 +211,7 @@ def get(self, key):
219
211
return Object (
220
212
id = key ,
221
213
data = response .content ,
222
- tags = json .loads (
223
- response .headers .get ('x-tags' , 'null' )
224
- ),
214
+ tags = json .loads (response .headers .get ("x-tags" , "null" )),
225
215
)
226
216
227
217
def delete (self , key ):
@@ -251,19 +241,18 @@ def find(self, **kwargs):
251
241
# due to a bug in the warp router (server side)
252
242
# this call does not match if no queries are supplied
253
243
# hence we add a dummy query that is ignred by the server
254
- kwargs = {'_' : '' }
244
+ kwargs = {"_" : "" }
255
245
256
246
# this should instead read response "stream" and parse each object individually
257
- response = self .session .get (
258
- self .url (), params = kwargs , headers = self .headers ())
247
+ response = self .session .get (self .url (), params = kwargs , headers = self .headers ())
259
248
260
249
content = response .text
261
250
dec = json .JSONDecoder ()
262
251
while content :
263
252
obj , idx = dec .raw_decode (content )
264
253
yield Object (
265
- id = obj ['id' ],
266
- tags = obj [' tags' ],
254
+ id = obj ["id" ],
255
+ tags = obj [" tags" ],
267
256
data = None ,
268
257
)
269
258
0 commit comments