11
11
from .exceptions import ipfsApiError
12
12
13
13
14
+ def pass_defaults (f ):
15
+ """
16
+ Use instance default kwargs updated with those passed to function.
17
+ """
18
+ def wrapper (self , * args , ** kwargs ):
19
+ merged = {}
20
+ merged .update (self .defaults )
21
+ merged .update (kwargs )
22
+ return f (self , * args , ** merged )
23
+ return wrapper
24
+
25
+
14
26
class HTTPClient (object ):
15
27
16
- def __init__ (self , host , port , base , default_enc ):
28
+ def __init__ (self , host , port , base , default_enc , ** defaults ):
17
29
self .host = host
18
30
self .port = port
19
31
self .base = 'http://%s:%s/%s' % (host , port , base )
20
32
33
+ # default request keyword-args
34
+ if 'opts' in defaults :
35
+ defaults ['opts' ].update ({'encoding' : default_enc })
36
+ else :
37
+ defaults .update ({'opts' : {'encoding' : default_enc }})
38
+
21
39
self .default_enc = encoding .get_encoding (default_enc )
22
- self .default_opts = { 'encoding' : default_enc }
40
+ self .defaults = defaults
23
41
self ._session = None
24
42
43
+ @pass_defaults
25
44
def request (self , path ,
26
45
args = [], files = [], opts = {},
27
46
decoder = None , ** kwargs ):
@@ -31,10 +50,7 @@ def request(self, path,
31
50
params = []
32
51
params .append (('stream-channels' , 'true' ))
33
52
34
- merged_opts = {}
35
- merged_opts .update (self .default_opts )
36
- merged_opts .update (opts )
37
- for opt in merged_opts .items ():
53
+ for opt in opts .items ():
38
54
params .append (opt )
39
55
for arg in args :
40
56
params .append (('arg' , arg ))
0 commit comments