@@ -43,29 +43,51 @@ class ServerVersion(TypedDict):
4343 """Indicates if the subscription has extended support"""
4444
4545
46+ @dataclass
47+ class RuntimeOptions :
48+ xdebug_session : str
49+ timeout : Optional [int ]
50+ timeout_dav : Optional [int ]
51+ _nc_cert : Union [str , bool ]
52+
53+ def __init__ (self , ** kwargs ):
54+ self .xdebug_session = kwargs .get ("xdebug_session" , options .XDEBUG_SESSION )
55+ self .timeout = kwargs .get ("npa_timeout" , options .NPA_TIMEOUT )
56+ self .timeout_dav = kwargs .get ("npa_timeout_dav" , options .NPA_TIMEOUT_DAV )
57+ self ._nc_cert = kwargs .get ("npa_nc_cert" , options .NPA_NC_CERT )
58+
59+ @property
60+ def nc_cert (self ) -> Union [str , bool ]:
61+ return self ._nc_cert
62+
63+
4664@dataclass
4765class BasicConfig :
4866 endpoint : str
4967 dav_endpoint : str
5068 dav_url_suffix : str
69+ options : RuntimeOptions
5170
5271 def __init__ (self , ** kwargs ):
53- full_nc_url = self ._get_value ("nextcloud_url" , ** kwargs )
72+ full_nc_url = self ._get_config_value ("nextcloud_url" , ** kwargs )
5473 self .endpoint = full_nc_url .removesuffix ("/index.php" ).removesuffix ("/" )
55- self .dav_url_suffix = self ._get_value ("dav_url_suffix" , raise_not_found = False , ** kwargs )
74+ self .dav_url_suffix = self ._get_config_value ("dav_url_suffix" , raise_not_found = False , ** kwargs )
5675 if not self .dav_url_suffix :
5776 self .dav_url_suffix = "remote.php/dav"
5877 self .dav_url_suffix = "/" + self .dav_url_suffix .strip ("/" )
5978 self .dav_endpoint = self .endpoint + self .dav_url_suffix
79+ self .options = RuntimeOptions (** kwargs )
6080
6181 @staticmethod
62- def _get_value (value_name : str , raise_not_found = True , ** kwargs ):
63- value = kwargs .get (value_name , None )
64- if not value :
65- value = environ .get (value_name .upper (), None )
66- if not value and raise_not_found :
82+ def _get_config_value (value_name : str , raise_not_found = True , ** kwargs ):
83+ if value_name in kwargs :
84+ return kwargs [value_name ]
85+ value_name_upper = value_name .upper ()
86+ if value_name_upper in environ :
87+ return environ [value_name_upper ]
88+ if raise_not_found :
6789 raise ValueError (f"`{ value_name } ` is not found." )
68- return value
90+ return None
6991
7092
7193@dataclass
@@ -74,7 +96,7 @@ class Config(BasicConfig):
7496
7597 def __init__ (self , ** kwargs ):
7698 super ().__init__ (** kwargs )
77- self .auth = (self ._get_value ("nc_auth_user" , ** kwargs ), self ._get_value ("nc_auth_pass" , ** kwargs ))
99+ self .auth = (self ._get_config_value ("nc_auth_user" , ** kwargs ), self ._get_config_value ("nc_auth_pass" , ** kwargs ))
78100
79101
80102@dataclass
@@ -92,12 +114,12 @@ class AppConfig(BasicConfig):
92114
93115 def __init__ (self , ** kwargs ):
94116 super ().__init__ (** kwargs )
95- self .ae_version = self ._get_value ("ae_version" , raise_not_found = False , ** kwargs )
117+ self .ae_version = self ._get_config_value ("ae_version" , raise_not_found = False , ** kwargs )
96118 if not self .ae_version :
97119 self .ae_version = "1.0.0"
98- self .app_name = self ._get_value ("app_id" , ** kwargs )
99- self .app_version = self ._get_value ("app_version" , ** kwargs )
100- self .app_secret = self ._get_value ("app_secret" , ** kwargs ).encode ("UTF-8" )
120+ self .app_name = self ._get_config_value ("app_id" , ** kwargs )
121+ self .app_version = self ._get_config_value ("app_version" , ** kwargs )
122+ self .app_secret = self ._get_config_value ("app_secret" , ** kwargs ).encode ("UTF-8" )
101123
102124
103125class NcSessionBasic (ABC ):
@@ -147,7 +169,7 @@ def _ocs(self, method: str, path_params: str, headers: dict, data: Optional[byte
147169 info = f"request: method={ method } , url={ url_params } "
148170 nested_req = kwargs .pop ("nested_req" , False )
149171 try :
150- timeout = kwargs .pop ("timeout" , options .TIMEOUT )
172+ timeout = kwargs .pop ("timeout" , self . cfg . options .timeout )
151173 if method == "GET" :
152174 response = self .adapter .get (url_params , headers = headers , timeout = timeout , ** kwargs )
153175 else :
@@ -190,14 +212,15 @@ def dav_stream(
190212
191213 def _dav (self , method : str , path : str , headers : dict , data : Optional [bytes ], ** kwargs ) -> Response :
192214 self .init_adapter ()
193- timeout = kwargs .pop ("timeout" , options .TIMEOUT_DAV )
215+ # self.cfg.
216+ timeout = kwargs .pop ("timeout" , self .cfg .options .timeout_dav )
194217 return self .adapter .request (
195218 method , self .cfg .endpoint + path , headers = headers , content = data , timeout = timeout , ** kwargs
196219 )
197220
198221 def _dav_stream (self , method : str , path : str , headers : dict , data : Optional [bytes ], ** kwargs ) -> Iterator [Response ]:
199222 self .init_adapter ()
200- timeout = kwargs .pop ("timeout" , options .TIMEOUT_DAV )
223+ timeout = kwargs .pop ("timeout" , self . cfg . options .timeout_dav )
201224 return self .adapter .stream (
202225 method , self .cfg .endpoint + path , headers = headers , content = data , timeout = timeout , ** kwargs
203226 )
@@ -249,9 +272,7 @@ def __init__(self, **kwargs):
249272 super ().__init__ (user = self .cfg .auth [0 ])
250273
251274 def _create_adapter (self ) -> Client :
252- return Client (
253- auth = self .cfg .auth , follow_redirects = True , limits = self .limits , verify = options .VERIFY_NC_CERTIFICATE
254- )
275+ return Client (auth = self .cfg .auth , follow_redirects = True , limits = self .limits , verify = self .cfg .options .nc_cert )
255276
256277
257278class NcSessionApp (NcSessionBasic ):
@@ -274,7 +295,7 @@ def _dav_stream(self, method: str, path: str, headers: dict, data: Optional[byte
274295 return super ()._dav_stream (method , path , headers , data , ** kwargs )
275296
276297 def _create_adapter (self ) -> Client :
277- adapter = Client (follow_redirects = True , limits = self .limits , verify = options .VERIFY_NC_CERTIFICATE )
298+ adapter = Client (follow_redirects = True , limits = self .limits , verify = self . cfg . options .nc_cert )
278299 adapter .headers .update (
279300 {
280301 "AE-VERSION" : self .cfg .ae_version ,
0 commit comments