|
10 | 10 | import random |
11 | 11 | from urllib.parse import urlparse |
12 | 12 | from aiblock.interfaces import IResult, IErrorInternal |
| 13 | +from importlib import metadata as _importlib_metadata |
13 | 14 | import json |
14 | 15 |
|
15 | 16 | # Set up logging |
@@ -91,7 +92,9 @@ def handle_response(response) -> IResult[APIResponse]: |
91 | 92 | success_messages = { |
92 | 93 | 'latest_block': 'Latest block retrieved successfully', |
93 | 94 | 'block': 'Block retrieved successfully', |
| 95 | + 'block_by_num': 'Block retrieved successfully', |
94 | 96 | 'blockchain': 'Blockchain entry retrieved successfully', |
| 97 | + 'blockchain_entry': 'Blockchain entry retrieved successfully', |
95 | 98 | 'total_supply': 'Total supply retrieved successfully', |
96 | 99 | 'issued_supply': 'Issued supply retrieved successfully' |
97 | 100 | } |
@@ -174,48 +177,30 @@ def _make_request(self, endpoint: str, method: str = 'GET', data: Any = None) -> |
174 | 177 | # Determine which host to use based on endpoint |
175 | 178 | if endpoint.startswith(('total_supply', 'issued_supply', 'fetch_balance', 'create_item_asset', 'create_transactions')): |
176 | 179 | if not self.mempool_host: |
177 | | - raise ValueError("Mempool host is required for this endpoint") |
| 180 | + return IResult.err(IErrorInternal.NetworkNotInitialized, "Mempool host is required for this endpoint") |
178 | 181 | url = f"{self.mempool_host}/{endpoint}" |
179 | 182 | else: |
180 | 183 | if not self.storage_host: |
181 | | - raise ValueError("Storage host is required for this endpoint") |
| 184 | + return IResult.err(IErrorInternal.NetworkNotInitialized, "Storage host is required for this endpoint") |
182 | 185 | url = f"{self.storage_host}/{endpoint}" |
183 | | - |
184 | | - # Prepare headers |
185 | | - headers = { |
186 | | - 'Content-Type': 'application/json', |
187 | | - 'Accept': 'application/json', |
188 | | - 'User-Agent': f'AIBlock-Python-SDK/{self._get_version()}', |
189 | | - 'Request-ID': str(uuid.uuid4()), |
190 | | - 'Nonce': self._get_random_string(32) |
191 | | - } |
192 | | - |
| 186 | + |
| 187 | + # Prepare headers using shared generator |
| 188 | + headers = get_headers() |
| 189 | + headers['User-Agent'] = f"AIBlock-Python-SDK/{self._get_version()}" |
| 190 | + |
193 | 191 | # Make the request |
194 | 192 | if method.upper() == 'POST': |
195 | 193 | if data is not None: |
196 | | - # Use the same format as the working example: requests.request with data parameter |
197 | 194 | payload = json.dumps(data) |
198 | 195 | response = requests.request('POST', url, headers=headers, data=payload, timeout=30) |
199 | 196 | else: |
200 | 197 | response = requests.post(url, headers=headers, timeout=30) |
201 | 198 | else: |
202 | 199 | response = requests.get(url, headers=headers, timeout=30) |
203 | | - |
204 | | - # Handle response |
205 | | - if response.status_code == 200: |
206 | | - try: |
207 | | - json_response = response.json() |
208 | | - return IResult.ok(APIResponse(**json_response)) |
209 | | - except (ValueError, json.JSONDecodeError): |
210 | | - return IResult.err(IErrorInternal.InvalidNetworkResponse, f"Invalid JSON response: {response.text}") |
211 | | - else: |
212 | | - try: |
213 | | - error_response = response.json() |
214 | | - reason = error_response.get('reason', f'HTTP {response.status_code}') |
215 | | - return IResult.err(IErrorInternal.InvalidNetworkResponse, reason) |
216 | | - except (ValueError, json.JSONDecodeError): |
217 | | - return IResult.err(IErrorInternal.InvalidNetworkResponse, f"HTTP {response.status_code}: {response.text}") |
218 | | - |
| 200 | + |
| 201 | + # Delegate response handling to shared handler |
| 202 | + return handle_response(response) |
| 203 | + |
219 | 204 | except requests.exceptions.Timeout: |
220 | 205 | return IResult.err(IErrorInternal.NetworkError, "Request timeout") |
221 | 206 | except requests.exceptions.ConnectionError: |
@@ -327,8 +312,11 @@ def fetch_transactions(self, transaction_hashes: list[str]) -> IResult[APIRespon |
327 | 312 | return self._make_request('blockchain_entry', method='POST', data=transaction_hashes) |
328 | 313 |
|
329 | 314 | def _get_version(self) -> str: |
330 | | - """Get the SDK version.""" |
331 | | - return "0.2.7" # Current version from pyproject.toml |
| 315 | + """Get the SDK version from installed metadata, fallback to project version.""" |
| 316 | + try: |
| 317 | + return _importlib_metadata.version('aiblock') |
| 318 | + except Exception: |
| 319 | + return "0.2.8" |
332 | 320 |
|
333 | 321 | def _get_random_string(self, length: int) -> str: |
334 | 322 | """Generate a random string of specified length.""" |
|
0 commit comments