18 from pathlib
import Path
19 from typing
import List, Optional, Tuple
20 from datetime
import datetime, timezone
24 from .base
import BaseConfiguration, CONFIG_DIR
25 from ..utils
import decodeDate
28 class UserConfiguration(BaseConfiguration):
31 def getConfigPath(cls) -> Path:
32 return CONFIG_DIR /
"user_config.json"
35 def username(self) -> str:
36 return self.getValue(
"username", str,
"CTX_USERNAME")
39 def username(self, value: str) ->
None:
40 self._raw[
"username"] = value
43 def password(self) -> str:
44 return self.getValue(
"password", str,
"CTX_PASSWORD")
47 def password(self, value: str) ->
None:
48 self._raw[
"password"] = value
51 def token(self) -> Optional[str]:
52 return self.getOptValue(
"token", str)
55 def token(self, value: Optional[str]) ->
None:
56 self._raw[
"token"] = value
59 def refreshToken(self) -> Optional[str]:
60 return self.getOptValue(
"refreshToken", str)
63 def refreshToken(self, value: Optional[str]) ->
None:
64 self._raw[
"refreshToken"] = value
67 def tokenExpirationDate(self) -> Optional[str]:
68 return self.getOptValue(
"tokenExpirationDate", str)
70 @tokenExpirationDate.setter
71 def tokenExpirationDate(self, value: Optional[str]) ->
None:
72 self._raw[
"tokenExpirationDate"] = value
75 def refreshTokenExpirationDate(self) -> Optional[str]:
76 return self.getOptValue(
"refreshTokenExpirationDate", str)
78 @refreshTokenExpirationDate.setter
79 def refreshTokenExpirationDate(self, value: Optional[str]) ->
None:
80 self._raw[
"refreshTokenExpirationDate"] = value
83 def serverUrl(self) -> str:
84 return self.getValue(
"serverUrl", str,
"CTX_API_URL",
"https://api.coretex.ai/")
87 def serverUrl(self, value: str) ->
None:
88 os.environ[
"CTX_API_URL"] = value
89 self._raw[
"serverUrl"] = value
92 def projectId(self) -> Optional[int]:
93 return self.getOptValue(
"projectId", int,
"CTX_PROJECT_ID")
96 def projectId(self, value: Optional[int]) ->
None:
97 self._raw[
"projectId"] = value
100 def frontendUrl(self) -> str:
101 return self.getValue(
"frontendUrl", str, default =
"app.coretex.ai")
104 def frontendUrl(self, value: Optional[str]) ->
None:
105 self._raw[
"frontendUrl"] = value
107 def _isConfigValid(self) -> Tuple[bool, List[str]]:
111 if self._raw.get(
"username")
is None or not isinstance(self._raw.get(
"username"), str):
113 errorMessages.append(
"Missing required field \"username\" in user configuration.")
115 if self._raw.get(
"password")
is None or not isinstance(self._raw.get(
"password"), str):
117 errorMessages.append(
"Missing required field \"password\" in user configuration.")
119 return isValid, errorMessages
121 def isTokenValid(self, tokenName: str) -> bool:
122 tokenValue = self._raw.get(tokenName)
123 if not isinstance(tokenValue, str)
or len(tokenValue) == 0:
126 tokenExpirationDate = self._raw.get(f
"{tokenName}ExpirationDate")
127 if not isinstance(tokenExpirationDate, str)
or len(tokenExpirationDate) == 0:
131 return datetime.now(timezone.utc) < decodeDate(tokenExpirationDate)
135 def selectProject(self, id: int) ->
None: