|
40 | 40 | this_dir = os.path.dirname(os.path.realpath(__file__)) |
41 | 41 | json_headers = {"Content-Type": "application/json"} |
42 | 42 |
|
| 43 | +MERGIN_DEFAULT_LOGS_URL = "https://g4pfq226j0.execute-api.eu-west-1.amazonaws.com/mergin_client_log_submit" |
| 44 | + |
43 | 45 |
|
44 | 46 | class TokenError(Exception): |
45 | 47 | pass |
@@ -1360,3 +1362,72 @@ def remove_project_collaborator(self, project_id: str, user_id: int): |
1360 | 1362 | Remove a user from project collaborators |
1361 | 1363 | """ |
1362 | 1364 | self.delete(f"v2/projects/{project_id}/collaborators/{user_id}") |
| 1365 | + |
| 1366 | + def server_config(self) -> dict: |
| 1367 | + """Get server configuration as dictionary.""" |
| 1368 | + response = self.get("/config") |
| 1369 | + return json.load(response) |
| 1370 | + |
| 1371 | + def server_version_newer_or_equal_than(self, version: str) -> bool: |
| 1372 | + """Check if the server version is newer or equal to the specified version.""" |
| 1373 | + required_major, required_minor, required_fix = version.split(".") |
| 1374 | + server_version = self.server_version() |
| 1375 | + if server_version: |
| 1376 | + server_major, server_minor, server_fix = server_version.split(".") |
| 1377 | + return ( |
| 1378 | + int(server_major) > int(required_major) |
| 1379 | + or (int(server_major) == int(required_major) and int(server_minor) > int(required_minor)) |
| 1380 | + or ( |
| 1381 | + int(server_major) == int(required_major) |
| 1382 | + and int(server_minor) == int(required_minor) |
| 1383 | + and int(server_fix) >= int(required_fix) |
| 1384 | + ) |
| 1385 | + ) |
| 1386 | + return False |
| 1387 | + |
| 1388 | + def send_logs( |
| 1389 | + self, |
| 1390 | + logfile: str, |
| 1391 | + global_log_file: typing.Optional[str] = None, |
| 1392 | + application: typing.Optional[str] = None, |
| 1393 | + meta: typing.Optional[str] = None, |
| 1394 | + ): |
| 1395 | + """Send logs to configured server or the default Mergin server.""" |
| 1396 | + |
| 1397 | + if application is None: |
| 1398 | + application = "mergin-client-{}".format(__version__) |
| 1399 | + |
| 1400 | + params = {"app": application, "username": self.username()} |
| 1401 | + |
| 1402 | + config = self.server_config() |
| 1403 | + diagnostic_logs_url = config.get("diagnostic_logs_url", None) |
| 1404 | + |
| 1405 | + if self.server_version_newer_or_equal_than("2023.4.1") and ( |
| 1406 | + diagnostic_logs_url is None or diagnostic_logs_url == "" |
| 1407 | + ): |
| 1408 | + url = self.url() + "?" + urllib.parse.urlencode(params) |
| 1409 | + else: |
| 1410 | + url = MERGIN_DEFAULT_LOGS_URL + "?" + urllib.parse.urlencode(params) |
| 1411 | + |
| 1412 | + if meta is None: |
| 1413 | + meta = "Python API Client\nSystem: {} \nMergin Maps URL: {} \nMergin Maps user: {} \n--------------------------------\n\n".format( |
| 1414 | + platform.system(), self.url, self.username() |
| 1415 | + ) |
| 1416 | + |
| 1417 | + global_logs = b"" |
| 1418 | + if global_log_file and os.path.exists(global_log_file): |
| 1419 | + with open(global_log_file, "rb") as f: |
| 1420 | + if os.path.getsize(global_log_file) > 100 * 1024: |
| 1421 | + f.seek(-100 * 1024, os.SEEK_END) |
| 1422 | + global_logs = f.read() + b"\n--------------------------------\n\n" |
| 1423 | + |
| 1424 | + with open(logfile, "rb") as f: |
| 1425 | + if os.path.getsize(logfile) > 512 * 1024: |
| 1426 | + f.seek(-512 * 1024, os.SEEK_END) |
| 1427 | + logs = f.read() |
| 1428 | + |
| 1429 | + payload = meta.encode() + global_logs + logs |
| 1430 | + header = {"content-type": "text/plain"} |
| 1431 | + |
| 1432 | + request = urllib.request.Request(url, data=payload, headers=header) |
| 1433 | + return self._do_request(request) |
0 commit comments