88[https://webknossos.org/auth/token](https://webknossos.org/auth/token)
99
1010Using the same methods, you can also specify the webknossos-server if you
11- are not using the default [webknossos.org](https://webknossos.org) instance.
11+ are not using the default [webknossos.org](https://webknossos.org) instance,
12+ as well as a timeout for network requests (default is 30 minutes).
1213
1314There are the following four options to specify which server context to use:
1415
3637 # content of .env
3738 WK_TOKEN="my_webknossos_token"
3839 WK_URL="…"
40+ WK_TIMEOUT="3600" # in seconds
3941 ```
4042
41434. If nothing else is specified and authentication is needed,
5456from dotenv import load_dotenv
5557from rich .prompt import Prompt
5658
57- from webknossos .client ._defaults import DEFAULT_WEBKNOSSOS_URL
59+ from webknossos .client ._defaults import DEFAULT_HTTP_TIMEOUT , DEFAULT_WEBKNOSSOS_URL
5860from webknossos .client ._generated import Client as GeneratedClient
5961
6062load_dotenv ()
@@ -101,13 +103,14 @@ def _cached_get_datastore_token(context: "_WebknossosContext") -> str:
101103def _cached__get_generated_client (
102104 webknossos_url : str ,
103105 token : Optional [str ],
106+ timeout : int ,
104107) -> GeneratedClient :
105108 """Generates a client which might contain an x-auth-token header."""
106109 if token is None :
107- return GeneratedClient (base_url = webknossos_url , timeout = 30 )
110+ return GeneratedClient (base_url = webknossos_url , timeout = timeout )
108111 else :
109112 return GeneratedClient (
110- base_url = webknossos_url , headers = {"X-Auth-Token" : token }, timeout = 30
113+ base_url = webknossos_url , headers = {"X-Auth-Token" : token }, timeout = timeout
111114 )
112115
113116
@@ -122,6 +125,7 @@ def _clear_all_context_caches() -> None:
122125class _WebknossosContext :
123126 url : str = os .environ .get ("WK_URL" , default = DEFAULT_WEBKNOSSOS_URL )
124127 token : Optional [str ] = os .environ .get ("WK_TOKEN" , default = None )
128+ timeout : int = int (os .environ .get ("WK_TIMEOUT" , default = DEFAULT_HTTP_TIMEOUT ))
125129
126130 # all properties are cached outside to allow re-usability
127131 # if same context is instantiated twice
@@ -149,14 +153,16 @@ def datastore_required_token(self) -> str:
149153
150154 @property
151155 def generated_client (self ) -> GeneratedClient :
152- return _cached__get_generated_client (self .url , self .token )
156+ return _cached__get_generated_client (self .url , self .token , self . timeout )
153157
154158 @property
155159 def generated_auth_client (self ) -> GeneratedClient :
156- return _cached__get_generated_client (self .url , self .required_token )
160+ return _cached__get_generated_client (
161+ self .url , self .required_token , self .timeout
162+ )
157163
158164 def get_generated_datastore_client (self , datastore_url : str ) -> GeneratedClient :
159- return GeneratedClient (base_url = datastore_url , timeout = 120 )
165+ return GeneratedClient (base_url = datastore_url , timeout = self . timeout )
160166
161167
162168_webknossos_context_var : ContextVar [_WebknossosContext ] = ContextVar (
@@ -166,10 +172,32 @@ def get_generated_datastore_client(self, datastore_url: str) -> GeneratedClient:
166172
167173@contextmanager
168174def webknossos_context (
169- url : str = DEFAULT_WEBKNOSSOS_URL ,
175+ url : Optional [ str ] = None ,
170176 token : Optional [str ] = None ,
177+ timeout : Optional [int ] = None ,
171178) -> Iterator [None ]:
172- context_var_token = _webknossos_context_var .set (_WebknossosContext (url , token ))
179+ """Returns a new webKnossos server contextmanager. Use with the `with` statement:
180+ ```python
181+ with webknossos_context(token="my_webknossos_token"):
182+ # code that interacts with webknossos
183+ ```
184+
185+ You can specify the following arguments:
186+ * `url`, by default [https://webknossos.org](https://www.webknossos.org),
187+ * `token`, as displayed on [https://webknossos.org/auth/token](https://webknossos.org/auth/token),
188+ * `timeout` to specify a custom network request timeout in seconds, `1800` (30min) by default.
189+
190+ `url` and `timeout` are taken from the previous context (e.g. environment variables) if not specified.
191+ `token` must be set explicitly, it is not available when not specified.
192+ """
193+
194+ if url is None :
195+ url = _get_context ().url
196+ if timeout is None :
197+ timeout = _get_context ().timeout
198+ context_var_token = _webknossos_context_var .set (
199+ _WebknossosContext (url , token , timeout )
200+ )
173201 try :
174202 yield
175203 finally :
0 commit comments