Source code for qiskit_quantuminspire.api.settings
"""Module containing the handler for the Quantum Inspire persistent configuration."""from__future__importannotationsimporttimefrompathlibimportPathfromtypingimportDict,OptionalfrompydanticimportBaseModel,BeforeValidator,Field,HttpUrlfromtyping_extensionsimportAnnotatedUrl=Annotated[str,BeforeValidator(lambdavalue:str(HttpUrl(value)).rstrip("/"))]API_SETTINGS_FILE=Path.joinpath(Path.home(),".quantuminspire","config.json")
[docs]classTokenInfo(BaseModel):"""A pydantic model for storing all information regarding oauth access and refresh tokens."""access_token:strexpires_in:int# [s]refresh_token:strrefresh_expires_in:Optional[int]=None# [s]generated_at:float=Field(default_factory=time.time)@propertydefaccess_expires_at(self)->float:"""Unix timestamp containing the time when the access token will expire."""returnself.generated_at+self.expires_in
[docs]classAuthSettings(BaseModel):"""Pydantic model for storing all auth related settings for a given host."""client_id:strcode_challenge_method:strcode_verifyer_length:intwell_known_endpoint:Urltokens:Optional[TokenInfo]team_member_id:Optional[int]
[docs]classApiSettings(BaseModel):"""The settings class for the Quantum Inspire persistent configuration."""auths:Dict[Url,AuthSettings]default_host:Url
[docs]defstore_tokens(self,host:Url,tokens:TokenInfo,path:Path=API_SETTINGS_FILE)->None:"""Stores the team_member_id, access and refresh tokens in the config.json file. Args: host: The hostname of the API for which the tokens are intended. tokens: OAuth access and refresh tokens. path: The path to the config.json file. Defaults to API_SETTINGS_FILE. Returns: None """self.auths[host].tokens=tokenspath.write_text(self.model_dump_json(indent=2))
[docs]@classmethoddeffrom_config_file(cls,path:Path=API_SETTINGS_FILE)->ApiSettings:"""Load the configuration from a file."""ifnotpath.is_file():raiseFileNotFoundError("No configuration file found. Please connect to Quantum Inspire using the CLI.")api_settings=path.read_text()returnApiSettings.model_validate_json(api_settings)