From 847ce008d0c4a2721bb9ebe54d7e94f918f5a7f1 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Fri, 23 Aug 2024 11:00:56 +0200 Subject: [PATCH 1/4] CTX-6577(part): serverUrl bug fix and added prompt for specifying server url rather than having to override env var manually. --- coretex/cli/modules/user.py | 29 ++++++++++++++++++++++++++++- coretex/configuration/user.py | 3 +++ coretex/networking/__init__.py | 1 + coretex/networking/utils.py | 9 +++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/coretex/cli/modules/user.py b/coretex/cli/modules/user.py index 825a80df..4401d091 100644 --- a/coretex/cli/modules/user.py +++ b/coretex/cli/modules/user.py @@ -15,16 +15,43 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import requests + from . import ui -from ...networking import networkManager, NetworkRequestError +from ...networking import networkManager, NetworkRequestError, baseUrl from ...configuration import UserConfiguration, InvalidConfiguration, ConfigurationNotFound +def validateServerUrl(serverUrl: str) -> bool: + try: + endpoint = baseUrl(serverUrl) + "/info.json" + response = requests.get(endpoint, timeout = 5) + return response.ok + except BaseException as ex: + return False + + +def configureServerUrl() -> str: + availableChoices = ["Official Coretex Server URL", "Custom Server URL"] + selectedChoice = ui.arrowPrompt(availableChoices, "Please select server url that you want to use (use arrow keys to select an option):") + + if not "Official" in selectedChoice: + serverUrl: str = ui.clickPrompt("Enter server url that you wish to use", type = str) + + if not validateServerUrl(serverUrl): + ui.warningEcho("You've entered invalid server url. Please try again.") + serverUrl = configureServerUrl() + + return serverUrl + + return "https://api.coretex.ai/" + def configUser(retryCount: int = 0) -> UserConfiguration: if retryCount >= 3: raise RuntimeError("Failed to authenticate. Terminating...") userConfig = UserConfiguration({}) # create new empty user config + userConfig.serverUrl = configureServerUrl() username = ui.clickPrompt("Email", type = str) password = ui.clickPrompt("Password", type = str, hide_input = True) diff --git a/coretex/configuration/user.py b/coretex/configuration/user.py index c81a82ec..c07ee6a9 100644 --- a/coretex/configuration/user.py +++ b/coretex/configuration/user.py @@ -19,6 +19,8 @@ from typing import List, Optional, Tuple from datetime import datetime, timezone +import os + from .base import BaseConfiguration, CONFIG_DIR from ..utils import decodeDate @@ -83,6 +85,7 @@ def serverUrl(self) -> str: @serverUrl.setter def serverUrl(self, value: str) -> None: + os.environ["CTX_API_URL"] = value self._raw["serverUrl"] = value @property diff --git a/coretex/networking/__init__.py b/coretex/networking/__init__.py index a6beedc5..4e1e08c9 100644 --- a/coretex/networking/__init__.py +++ b/coretex/networking/__init__.py @@ -22,3 +22,4 @@ from .request_type import RequestType from .chunk_upload_session import ChunkUploadSession, MAX_CHUNK_SIZE, fileChunkUpload from .file_data import FileData +from .utils import baseUrl diff --git a/coretex/networking/utils.py b/coretex/networking/utils.py index fb6778ec..d2550eda 100644 --- a/coretex/networking/utils.py +++ b/coretex/networking/utils.py @@ -20,6 +20,8 @@ import io import logging +from urllib.parse import urlsplit, urlunsplit, SplitResult + from .network_response import NetworkResponse @@ -66,3 +68,10 @@ def logRequestFailure(endpoint: str, response: NetworkResponse) -> None: logging.getLogger("coretexpylib").debug(f"\tResponse: {responseJson}") except (ValueError, TypeError): logging.getLogger("coretexpylib").debug(f"\tResponse: {response.getContent()!r}") + + +def baseUrl(url: str) -> str: + result = urlsplit(url) + parsed = SplitResult(result.scheme, result.netloc, "", "", "") + + return urlunsplit(parsed) From 986ea770df646e783119f2f5b04b167814dfb0c8 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Fri, 23 Aug 2024 11:01:54 +0200 Subject: [PATCH 2/4] CTX-6577: newline --- coretex/cli/modules/user.py | 1 + 1 file changed, 1 insertion(+) diff --git a/coretex/cli/modules/user.py b/coretex/cli/modules/user.py index 4401d091..dcfe9426 100644 --- a/coretex/cli/modules/user.py +++ b/coretex/cli/modules/user.py @@ -46,6 +46,7 @@ def configureServerUrl() -> str: return "https://api.coretex.ai/" + def configUser(retryCount: int = 0) -> UserConfiguration: if retryCount >= 3: raise RuntimeError("Failed to authenticate. Terminating...") From 782eb343288bd86f9f3be231b9cc4d6fd5e55a01 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Mon, 26 Aug 2024 16:32:50 +0200 Subject: [PATCH 3/4] CTX-6577: Discussion changes. --- coretex/cli/modules/user.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/coretex/cli/modules/user.py b/coretex/cli/modules/user.py index dcfe9426..93fe8d81 100644 --- a/coretex/cli/modules/user.py +++ b/coretex/cli/modules/user.py @@ -39,8 +39,7 @@ def configureServerUrl() -> str: serverUrl: str = ui.clickPrompt("Enter server url that you wish to use", type = str) if not validateServerUrl(serverUrl): - ui.warningEcho("You've entered invalid server url. Please try again.") - serverUrl = configureServerUrl() + serverUrl = ui.clickPrompt("You've entered invalid server url. Please try again.", type = str) return serverUrl From 7c3d1f71308e4829bc8df832b111ba0efacc7be9 Mon Sep 17 00:00:00 2001 From: Bogdan Tintor Date: Tue, 27 Aug 2024 10:31:02 +0200 Subject: [PATCH 4/4] CTX-6577: Fix of accidentally changed code. --- coretex/networking/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coretex/networking/utils.py b/coretex/networking/utils.py index bc85bdd4..850ba725 100644 --- a/coretex/networking/utils.py +++ b/coretex/networking/utils.py @@ -72,7 +72,7 @@ def logRequestFailure(endpoint: str, response: NetworkResponse) -> None: else: logger.debug(f"\tResponse: {responseJson}") except (ValueError, TypeError): - logging.getLogger("coretexpylib").debug(f"\tResponse: {response.getContent()!r}") + logger.debug(f"\tResponse: {response.getContent()!r}") def baseUrl(url: str) -> str: