@@ -134,15 +134,15 @@ class NcSessionBasic(ABC):
134
134
adapter : Client
135
135
adapter_dav : Client
136
136
cfg : BasicConfig
137
- user : str
138
137
custom_headers : dict
139
- _capabilities : dict
140
138
response_headers : HttpxHeaders
139
+ _user : str
140
+ _capabilities : dict
141
141
142
142
@abstractmethod
143
143
def __init__ (self , ** kwargs ):
144
144
self ._capabilities = {}
145
- self .user = kwargs .get ("user" , "" )
145
+ self ._user = kwargs .get ("user" , "" )
146
146
self .custom_headers = kwargs .get ("headers" , {})
147
147
self .limits = Limits (max_keepalive_connections = 20 , max_connections = 20 , keepalive_expiry = 60.0 )
148
148
self .init_adapter ()
@@ -168,22 +168,33 @@ def _get_stream(self, path_params: str, headers: dict, **kwargs) -> Iterator[Res
168
168
"GET" , f"{ self .cfg .endpoint } { path_params } " , headers = headers , timeout = timeout , ** kwargs
169
169
)
170
170
171
- def request_json (
171
+ def request (
172
172
self ,
173
173
method : str ,
174
174
path : str ,
175
175
params : Optional [dict ] = None ,
176
176
data : Optional [Union [bytes , str ]] = None ,
177
177
json : Optional [Union [dict , list ]] = None ,
178
178
** kwargs ,
179
- ) -> dict :
179
+ ):
180
180
method = method .upper ()
181
181
if params is None :
182
182
params = {}
183
183
params .update ({"format" : "json" })
184
184
headers = kwargs .pop ("headers" , {})
185
185
data_bytes = self .__data_to_bytes (headers , data , json )
186
- r = self ._ocs (method , f"{ quote (path )} ?{ urlencode (params , True )} " , headers , data_bytes , not_parse = True )
186
+ return self ._ocs (method , f"{ quote (path )} ?{ urlencode (params , True )} " , headers , data_bytes , not_parse = True )
187
+
188
+ def request_json (
189
+ self ,
190
+ method : str ,
191
+ path : str ,
192
+ params : Optional [dict ] = None ,
193
+ data : Optional [Union [bytes , str ]] = None ,
194
+ json : Optional [Union [dict , list ]] = None ,
195
+ ** kwargs ,
196
+ ) -> dict :
197
+ r = self .request (method , path , params , data , json , ** kwargs )
187
198
return loads (r .text ) if r .status_code != 304 else {}
188
199
189
200
def ocs (
@@ -319,6 +330,17 @@ def _create_adapter(self) -> Client:
319
330
def update_server_info (self ) -> None :
320
331
self ._capabilities = self .ocs (method = "GET" , path = "/ocs/v1.php/cloud/capabilities" )
321
332
333
+ @property
334
+ def user (self ) -> str :
335
+ """Current user ID. Can be different from the login name."""
336
+ if isinstance (self , NcSession ) and not self ._user : # do not trigger for NextcloudApp
337
+ self ._user = self .ocs (method = "GET" , path = "/ocs/v1.php/cloud/user" )["id" ]
338
+ return self ._user
339
+
340
+ @user .setter
341
+ def user (self , value : str ):
342
+ self ._user = value
343
+
322
344
@property
323
345
def capabilities (self ) -> dict :
324
346
if not self ._capabilities :
@@ -360,7 +382,7 @@ class NcSession(NcSessionBasic):
360
382
361
383
def __init__ (self , ** kwargs ):
362
384
self .cfg = Config (** kwargs )
363
- super ().__init__ (user = self . cfg . auth [ 0 ] )
385
+ super ().__init__ ()
364
386
365
387
def _create_adapter (self ) -> Client :
366
388
return Client (auth = self .cfg .auth , follow_redirects = True , limits = self .limits , verify = self .cfg .options .nc_cert )
@@ -401,7 +423,7 @@ def _create_adapter(self) -> Client:
401
423
return adapter
402
424
403
425
def sign_request (self , headers : dict ) -> None :
404
- headers ["AUTHORIZATION-APP-API" ] = b64encode (f"{ self .user } :{ self .cfg .app_secret } " .encode ("UTF=8" ))
426
+ headers ["AUTHORIZATION-APP-API" ] = b64encode (f"{ self ._user } :{ self .cfg .app_secret } " .encode ("UTF=8" ))
405
427
406
428
def sign_check (self , request : Request ) -> None :
407
429
headers = {
0 commit comments