erioon 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: erioon
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Erioon SDF for Python
5
5
  Author: Zyber Pireci
6
6
  Author-email: zyber.pireci@erioon.com
@@ -0,0 +1,194 @@
1
+ import os
2
+ import json
3
+ import requests
4
+ from erioon.database import Database
5
+
6
+ class ErioonClient:
7
+ """
8
+ Client SDK for interacting with the Erioon API.
9
+
10
+ Handles user authentication, token caching, and accessing user databases.
11
+
12
+ Attributes:
13
+ email (str): User email for login.
14
+ password (str): User password for login.
15
+ base_url (str): Base URL of the Erioon API.
16
+ user_id (str | None): Authenticated user ID.
17
+ error (str | None): Stores error messages if login fails.
18
+ token_path (str): Local path to cached authentication token.
19
+ """
20
+
21
+ def __init__(self, email, password, base_url="https://sdk.erioon.com"):
22
+ """
23
+ Initialize ErioonClient instance, attempts to load cached token or perform login.
24
+
25
+ Args:
26
+ email (str): User email for authentication.
27
+ password (str): User password for authentication.
28
+ base_url (str, optional): Base API URL. Defaults to "https://sdk.erioon.com".
29
+ """
30
+ self.email = email
31
+ self.password = password
32
+ self.base_url = base_url
33
+ self.user_id = None
34
+ self.error = None
35
+ self.token_path = os.path.expanduser(f"~/.erioon_token_{self._safe_filename(email)}")
36
+
37
+ try:
38
+ self.user_id = self._load_or_login()
39
+ except Exception as e:
40
+ self.error = str(e)
41
+
42
+ def _safe_filename(self, text):
43
+ """
44
+ Converts a string into a safe filename by replacing non-alphanumeric chars with underscores.
45
+
46
+ Args:
47
+ text (str): Input string to convert.
48
+
49
+ Returns:
50
+ str: Sanitized filename-safe string.
51
+ """
52
+ return "".join(c if c.isalnum() else "_" for c in text)
53
+
54
+ def _load_or_login(self):
55
+ """
56
+ Load cached user_id token from local storage or perform login if not cached.
57
+
58
+ Returns:
59
+ str: User ID from token or fresh login.
60
+
61
+ Raises:
62
+ Exception: If login fails.
63
+ """
64
+ if os.path.exists(self.token_path):
65
+ with open(self.token_path, "r") as f:
66
+ token_data = json.load(f)
67
+ user_id = token_data.get("user_id")
68
+ if user_id:
69
+ return user_id
70
+
71
+ return self._do_login_and_cache()
72
+
73
+ def _do_login_and_cache(self):
74
+ """
75
+ Perform login to API and cache the user_id token locally.
76
+
77
+ Returns:
78
+ str: User ID from successful login.
79
+
80
+ Raises:
81
+ Exception: If login fails.
82
+ """
83
+ user_id = self._login()
84
+ with open(self.token_path, "w") as f:
85
+ json.dump({"user_id": user_id}, f)
86
+ return user_id
87
+
88
+ def _login(self):
89
+ """
90
+ Authenticate with Erioon API using email and password.
91
+
92
+ Returns:
93
+ str: User ID on successful authentication.
94
+
95
+ Raises:
96
+ Exception: If credentials are invalid.
97
+ """
98
+ url = f"{self.base_url}/login_with_credentials"
99
+ payload = {"email": self.email, "password": self.password}
100
+ headers = {"Content-Type": "application/json"}
101
+
102
+ response = requests.post(url, json=payload, headers=headers)
103
+ if response.status_code == 200:
104
+ return response.text.strip()
105
+ else:
106
+ raise Exception("Invalid account")
107
+
108
+ def _clear_cached_token(self):
109
+ """
110
+ Remove cached token file and reset user_id to None.
111
+ """
112
+ if os.path.exists(self.token_path):
113
+ os.remove(self.token_path)
114
+ self.user_id = None
115
+
116
+ def __getitem__(self, db_id):
117
+ """
118
+ Access a Database object by database ID.
119
+
120
+ Args:
121
+ db_id (str): The ID of the database to access.
122
+
123
+ Returns:
124
+ Database: An instance representing the database.
125
+
126
+ Raises:
127
+ ValueError: If client is not authenticated.
128
+ Exception: For other API errors not related to database existence.
129
+
130
+ Handles:
131
+ On database-related errors, tries to relogin once. If relogin fails, returns "Login error".
132
+ If database still not found after relogin, returns a formatted error message.
133
+ """
134
+ if not self.user_id:
135
+ raise ValueError("Client not authenticated. Cannot access database.")
136
+
137
+ try:
138
+ return self._get_database_info(db_id)
139
+ except Exception as e:
140
+ err_msg = str(e).lower()
141
+ if f"database with {db_id.lower()}" in err_msg or "database" in err_msg:
142
+ self._clear_cached_token()
143
+ try:
144
+ self.user_id = self._do_login_and_cache()
145
+ except Exception:
146
+ return "Login error"
147
+
148
+ try:
149
+ return self._get_database_info(db_id)
150
+ except Exception:
151
+ return f"❌ Database with _id {db_id} ..."
152
+ else:
153
+ raise e
154
+
155
+ def _get_database_info(self, db_id):
156
+ """
157
+ Helper method to fetch database info from API and instantiate a Database object.
158
+
159
+ Args:
160
+ db_id (str): The database ID to fetch.
161
+
162
+ Returns:
163
+ Database: Database instance with the fetched info.
164
+
165
+ Raises:
166
+ Exception: If API returns an error.
167
+ """
168
+ payload = {"user_id": self.user_id, "db_id": db_id}
169
+ headers = {"Content-Type": "application/json"}
170
+
171
+ response = requests.post(f"{self.base_url}/db_info", json=payload, headers=headers)
172
+
173
+ if response.status_code == 200:
174
+ db_info = response.json()
175
+ return Database(self.user_id, db_info)
176
+ else:
177
+ try:
178
+ error_json = response.json()
179
+ error_msg = error_json.get("error", response.text)
180
+ except Exception:
181
+ error_msg = response.text
182
+ raise Exception(error_msg)
183
+
184
+ def __str__(self):
185
+ """
186
+ String representation: returns user_id if authenticated, else the error message.
187
+ """
188
+ return self.user_id if self.user_id else self.error
189
+
190
+ def __repr__(self):
191
+ """
192
+ Developer-friendly string representation of the client instance.
193
+ """
194
+ return f"<ErioonClient user_id={self.user_id}>" if self.user_id else f"<ErioonClient error='{self.error}'>"
@@ -11,7 +11,7 @@ class Collection:
11
11
  db_id,
12
12
  coll_id,
13
13
  metadata,
14
- base_url: str = "http://127.0.0.1:5000",
14
+ base_url: str = "https://sdk.erioon.com",
15
15
  ):
16
16
  """
17
17
  Initialize a Collection instance.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: erioon
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Erioon SDF for Python
5
5
  Author: Zyber Pireci
6
6
  Author-email: zyber.pireci@erioon.com
@@ -2,7 +2,7 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name='erioon',
5
- version='0.0.2',
5
+ version='0.0.3',
6
6
  author='Zyber Pireci',
7
7
  author_email='zyber.pireci@erioon.com',
8
8
  description='Erioon SDF for Python',
@@ -1,108 +0,0 @@
1
- import os
2
- import json
3
- import requests
4
- from erioon.database import Database
5
-
6
- class ErioonClient:
7
- def __init__(self, email, password, base_url="http://127.0.0.1:5000"):
8
- self.email = email
9
- self.password = password
10
- self.base_url = base_url
11
- self.user_id = None
12
- self.error = None
13
- self.token_path = os.path.expanduser(f"~/.erioon_token_{self._safe_filename(email)}")
14
-
15
- try:
16
- self.user_id = self._load_or_login()
17
- except Exception as e:
18
- self.error = str(e)
19
-
20
- def _safe_filename(self, text):
21
- return "".join(c if c.isalnum() else "_" for c in text)
22
-
23
- def _load_or_login(self):
24
- # Try to load from local cache
25
- if os.path.exists(self.token_path):
26
- with open(self.token_path, "r") as f:
27
- token_data = json.load(f)
28
- user_id = token_data.get("user_id")
29
- if user_id:
30
- return user_id # ✅ Use cached value without API call
31
-
32
- # Fallback: login and cache
33
- return self._do_login_and_cache()
34
-
35
- def _do_login_and_cache(self):
36
- user_id = self._login()
37
- with open(self.token_path, "w") as f:
38
- json.dump({"user_id": user_id}, f)
39
- return user_id
40
-
41
- def _login(self):
42
- url = f"{self.base_url}/login_with_credentials"
43
- payload = {"email": self.email, "password": self.password}
44
- headers = {"Content-Type": "application/json"}
45
-
46
- response = requests.post(url, json=payload, headers=headers)
47
- if response.status_code == 200:
48
- return response.text.strip()
49
- else:
50
- raise Exception("Invalid account")
51
-
52
- def _clear_cached_token(self):
53
- if os.path.exists(self.token_path):
54
- os.remove(self.token_path)
55
- self.user_id = None
56
-
57
- def __getitem__(self, db_id):
58
- if not self.user_id:
59
- raise ValueError("Client not authenticated. Cannot access database.")
60
-
61
- try:
62
- return self._get_database_info(db_id)
63
- except Exception as e:
64
- err_msg = str(e).lower()
65
- # Check if error is a database error mentioning the db_id
66
- if f"database with {db_id.lower()}" in err_msg or "database" in err_msg:
67
- # Try relogin once
68
- self._clear_cached_token()
69
- try:
70
- self.user_id = self._do_login_and_cache()
71
- except Exception:
72
- return "Login error"
73
-
74
- # Retry fetching database info
75
- try:
76
- return self._get_database_info(db_id)
77
- except Exception as e2:
78
- print(f"❌ Database with _id {db_id} ...")
79
- # Optionally you could also return or raise the error here
80
- return f"❌ Database with _id {db_id} ..."
81
- else:
82
- # Not a DB-related error, just propagate or raise
83
- raise e
84
-
85
-
86
- def _get_database_info(self, db_id):
87
- payload = {"user_id": self.user_id, "db_id": db_id}
88
- headers = {"Content-Type": "application/json"}
89
-
90
- response = requests.post(f"{self.base_url}/db_info", json=payload, headers=headers)
91
-
92
- if response.status_code == 200:
93
- db_info = response.json()
94
- return Database(self.user_id, db_info)
95
- else:
96
- # Try parse error json
97
- try:
98
- error_json = response.json()
99
- error_msg = error_json.get("error", response.text)
100
- except Exception:
101
- error_msg = response.text
102
- raise Exception(error_msg)
103
-
104
- def __str__(self):
105
- return self.user_id if self.user_id else self.error
106
-
107
- def __repr__(self):
108
- return f"<ErioonClient user_id={self.user_id}>" if self.user_id else f"<ErioonClient error='{self.error}'>"
File without changes
File without changes
File without changes
File without changes
File without changes