7
7
8
8
from aiohttp import BasicAuth , ClientSession , ClientTimeout
9
9
from more_itertools import chunked
10
+ from yarl import URL
10
11
11
12
from bssclient .client .config import BssConfig
12
13
from bssclient .models .aufgabe import AufgabeStats
@@ -27,6 +28,25 @@ def __init__(self, config: BssConfig):
27
28
self ._session : Optional [ClientSession ] = None
28
29
_logger .info ("Instantiated BssClient with server_url %s" , str (self ._config .server_url ))
29
30
31
+ def get_top_level_domain (self ) -> URL | None :
32
+ """
33
+ Returns the top level domain of the server_url; this is useful to differentiate prod from test systems.
34
+ If the server_url is an IP address, None is returned.
35
+ """
36
+ # this method is unit tested; check the testcases to understand its branches
37
+ domain_parts = self ._config .server_url .host .split ("." ) # type:ignore[union-attr]
38
+ if all (x .isnumeric () for x in domain_parts ):
39
+ # seems like this is an IP address
40
+ return None
41
+ if not any (domain_parts ):
42
+ return self ._config .server_url
43
+ tld : str
44
+ if domain_parts [- 1 ] == "localhost" :
45
+ tld = "." .join (domain_parts [- 1 :])
46
+ else :
47
+ tld = "." .join (domain_parts [- 2 :])
48
+ return URL (self ._config .server_url .scheme + "://" + tld )
49
+
30
50
async def _get_session (self ) -> ClientSession :
31
51
"""
32
52
returns a client session (that may be reused or newly created)
0 commit comments