def darwin_client(darwin_config_path: Path, darwin_datasets_path: Path, team_slug: str) -> Client: config = Config(darwin_config_path) config.put(["global", "api_endpoint"], "http://localhost/api") config.put(["global", "base_url"], "http://localhost") config.put(["teams", team_slug, "api_key"], "mock_api_key") config.put(["teams", team_slug, "datasets_dir"], str(darwin_datasets_path)) return Client(config=config)
def from_guest(cls, datasets_dir: Optional[Path] = None): if datasets_dir is None: datasets_dir = Path.home() / ".darwin" / "datasets" config = Config(path=None) config.set_global(api_endpoint=Client.default_api_url(), base_url=Client.default_base_url()) return cls(config=config)
def __init__(self, config: Config, default_team: Optional[str] = None, log: Optional[Logger] = None): self.config: Config = config self.url: str = config.get("global/api_endpoint") self.base_url: str = config.get("global/base_url") self.default_team: str = default_team or config.get( "global/default_team") self.features: Dict[str, List[Feature]] = {} self._newer_version: Optional[DarwinVersionNumber] = None if log is None: self.log: Logger = logging.getLogger("darwin") else: self.log = log
def persist_client_configuration(client: "Client", default_team: Optional[str] = None, config_path: Optional[Path] = None) -> Config: """Authenticate user against the server and creates a configuration file for it Parameters ---------- client : Client Client to take the configurations from config_path : Path Optional path to specify where to save the configuration file Returns ------- Config A configuration object to handle YAML files """ if not config_path: config_path = Path.home() / ".darwin" / "config.yaml" config_path.parent.mkdir(exist_ok=True) team_config: Optional[dt.Team] = client.config.get_default_team() if not team_config: raise ValueError("Unable to get default team.") config: Config = Config(config_path) config.set_team(team=team_config.slug, api_key=team_config.api_key, datasets_dir=team_config.datasets_dir) config.set_global(api_endpoint=client.url, base_url=client.base_url, default_team=default_team) return config
def local_config_file(team_slug: str, darwin_datasets_path: Path): darwin_path = Path.home() / ".darwin" backup_darwin_path = Path.home() / ".darwin_backup" config_path = darwin_path / "config.yaml" # Executed before the test if darwin_path.exists(): shutil.move(str(darwin_path), str(backup_darwin_path)) darwin_path.mkdir() config = Config(config_path) config.put(["global", "api_endpoint"], "http://localhost/api") config.put(["global", "base_url"], "http://localhost") config.put(["teams", team_slug, "api_key"], "mock_api_key") config.put(["teams", team_slug, "datasets_dir"], str(darwin_datasets_path)) # Useful if the test needs to reuse attrs yield config # Executed after the test shutil.rmtree(darwin_path) if backup_darwin_path.exists(): shutil.move(backup_darwin_path, darwin_path)
def from_config(cls, config_path: Path, team_slug: Optional[str] = None): """Factory method to create a client from the configuration file passed as parameter Parameters ---------- config_path : str Path to a configuration file to use to create the client Returns ------- Client The inited client """ if not config_path.exists(): raise MissingConfig() config = Config(config_path) return cls(config=config, default_team=team_slug)
def from_api_key(cls, api_key: str, datasets_dir: Optional[Path] = None) -> "Client": """ Factory method to create a client given an API key. Parameters ---------- api_key: str API key to use to authenticate the client datasets_dir : Optional[Path] String where the client should be initialized from (aka the root path). Defaults to None. Returns ------- Client The initialized client. """ if not datasets_dir: datasets_dir = Path.home() / ".darwin" / "datasets" headers: Dict[str, str] = { "Content-Type": "application/json", "Authorization": f"ApiKey {api_key}" } api_url: str = Client.default_api_url() response: requests.Response = requests.get(urljoin( api_url, "/users/token_info"), headers=headers) if not response.ok: raise InvalidLogin() data: Dict[str, Any] = response.json() team: str = data["selected_team"]["slug"] config: Config = Config(path=None) config.set_team(team=team, api_key=api_key, datasets_dir=str(datasets_dir)) config.set_global(api_endpoint=api_url, base_url=Client.default_base_url()) return cls(config=config, default_team=team)
def from_guest(cls, datasets_dir: Optional[Path] = None) -> "Client": """ Factory method to create a client and access datasets as a guest. Parameters ---------- datasets_dir : Optional[Path] String where the client should be initialized from (aka the root path). Defaults to None. Returns ------- Client The initialized client. """ if datasets_dir is None: datasets_dir = Path.home() / ".darwin" / "datasets" config: Config = Config(path=None) config.set_global(api_endpoint=Client.default_api_url(), base_url=Client.default_base_url()) return cls(config=config)
def from_config(cls, config_path: Path, team_slug: Optional[str] = None) -> "Client": """ Factory method to create a client from the configuration file passed as parameter Parameters ---------- config_path : str Path to a configuration file to use to create the client team_slug: Optional[str] Team slug of the team the dataset will belong to. Defaults to None. Returns ------- Client The initialized client. """ if not config_path.exists(): raise MissingConfig() config = Config(config_path) return cls(config=config, default_team=team_slug)
def from_api_key(cls, api_key: str, datasets_dir: Optional[Path] = None): """Factory method to create a client given an API key Parameters ---------- api_key: str API key to use to authenticate the client datasets_dir : str String where the client should be initialized from (aka the root path) Returns ------- Client The inited client """ if datasets_dir is None: datasets_dir = Path.home() / ".darwin" / "datasets" headers = { "Content-Type": "application/json", "Authorization": f"ApiKey {api_key}" } api_url = Client.default_api_url() response = requests.get(urljoin(api_url, "/users/token_info"), headers=headers) if response.status_code != 200: raise InvalidLogin() data = response.json() team_id = data["selected_team"]["id"] team = [ team["slug"] for team in data["teams"] if team["id"] == team_id ][0] config = Config(path=None) config.set_team(team=team, api_key=api_key, datasets_dir=str(datasets_dir)) config.set_global(api_endpoint=api_url, base_url=Client.default_base_url()) return cls(config=config, default_team=team)
def __init__(self, config: Config, default_team: Optional[str] = None): self.config = config self.url = config.get("global/api_endpoint") self.base_url = config.get("global/base_url") self.default_team = default_team or config.get("global/default_team")
def _config() -> Config: return Config(Path.home() / ".darwin" / "config.yaml")