66from pathlib import Path
77
88import httpx
9- import ixmp4
109import jwt
1110import numpy as np
1211import pandas as pd
1312import requests
1413import yaml
1514from ixmp4 .cli .platforms import tabulate_manager_platforms
16- from ixmp4 .conf import settings
17- from ixmp4 .conf .auth import ManagerAuth
15+ from ixmp4 .conf .settings import Settings
1816from requests .auth import AuthBase
17+ from toolkit .client .auth import ManagerAuth , SelfSignedAuth
1918
2019from pyam .core import IamDataFrame
2120from pyam .exceptions import deprecation_warning
@@ -49,16 +48,18 @@ def platforms() -> None:
4948
5049 See Also
5150 --------
52- ixmp4.conf.settings.manager .list_platforms
51+ ixmp4.conf.platforms.ManagerPlatforms .list_platforms
5352 """
54- tabulate_manager_platforms (ixmp4 .conf .settings .manager .list_platforms ())
53+ settings = Settings ()
54+ manager_platforms = settings .get_manager_platforms ()
55+ tabulate_manager_platforms (manager_platforms .list_platforms ())
5556
5657
5758def _read_config (file ):
5859 """Read username and password for IIASA API connection from file"""
5960 with open (file ) as stream :
6061 creds = yaml .safe_load (stream )
61-
62+ settings = Settings ()
6263 return ManagerAuth (** creds , url = str (settings .manager_url ))
6364
6465
@@ -68,6 +69,8 @@ def _check_response(r, msg="Error connecting to IIASA database", error=RuntimeEr
6869
6970
7071class SceSeAuth (AuthBase ):
72+ auth : ManagerAuth | SelfSignedAuth | None = None
73+
7174 def __init__ (self , creds : str = None , auth_url : str = _AUTH_URL ):
7275 """Connection to the Scenario Services manager service for authentication.
7376
@@ -89,7 +92,9 @@ def __init__(self, creds: str = None, auth_url: str = _AUTH_URL):
8992 )
9093 self .auth = _read_config (DEFAULT_IIASA_CREDS )
9194 else :
92- self .auth = ixmp4 .conf .settings .default_auth
95+ settings = Settings ()
96+ cred_dict = settings .get_credentials ().get ("default" )
97+ self .auth = settings .get_client_auth (cred_dict )
9398 elif isinstance (creds , Path ) or is_str (creds ):
9499 deprecation_warning (f"{ IXMP4_LOGIN } ." , "Using a pyam-credentials file" )
95100 self .auth = _read_config (creds )
@@ -98,19 +103,20 @@ def __init__(self, creds: str = None, auth_url: str = _AUTH_URL):
98103 "Passing credentials as clear-text is not allowed. "
99104 f"{ IXMP4_LOGIN } instead."
100105 )
101-
102- # self.auth is None if connection to manager service cannot be established
103- if self .auth is None :
104- raise httpx .ConnectError ("No connection to IIASA manager service." )
105-
106106 # explicit token for anonymous login is not necessary for ixmp4 platforms
107107 # but is required for legacy Scenario Explorer databases
108- if self .auth . user . username == "@anonymous" :
108+ if self .auth is None :
109109 self ._get_anonymous_token ()
110110
111111 else :
112- self .user = self .auth .user .username
113- self .access_token = self .auth .access_token
112+ token_username = getattr (
113+ self .auth .access_token .user , "username" , "@unknown"
114+ )
115+ if token_username == "@anonymous" :
116+ self ._get_anonymous_token ()
117+ else :
118+ self .user = token_username
119+ self .access_token = self .auth .access_token
114120
115121 def _get_anonymous_token (self ):
116122 r = self .client .get ("/legacy/anonym/" )
@@ -611,7 +617,9 @@ def read_iiasa(name, default_only=True, meta=True, creds=None, **kwargs):
611617 Credentials (username & password) are not required to access any public |ixmp4|
612618 or Scenario Explorer database (i.e., with Guest login).
613619 """
614- if name in [i .name for i in ixmp4 .conf .settings .manager .list_platforms ()]:
620+ settings = Settings ()
621+ manager_platforms = settings .get_manager_platforms ()
622+ if name in [i .name for i in manager_platforms .list_platforms ()]:
615623 if meta is not True :
616624 raise NotImplementedError (
617625 "Reading from ixmp4 platforms requires `meta=True`"
@@ -660,9 +668,9 @@ def lazy_read_iiasa(file, name, default_only=True, meta=True, creds=None, **kwar
660668 Credentials (username & password) are not required to access any public |ixmp4|
661669 or Scenario Explorer database (i.e., with Guest login).
662670 """
663- if name in [
664- platform . name for platform in ixmp4 . conf . settings .manager . list_platforms ()
665- ]:
671+ settings = Settings ()
672+ manager_platforms = settings .get_manager_platforms ()
673+ if name in [ platform . name for platform in manager_platforms . list_platforms () ]:
666674 raise NotImplementedError (
667675 "The function `lazy_read_iiasa()` does not support ixmp4 platforms."
668676 )
0 commit comments