Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTX-6577(part): serverUrl bug fix and added prompt for specifying ser… #258

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion coretex/cli/modules/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,45 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from requests.exceptions import RequestException

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 RequestException:
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)

while not validateServerUrl(serverUrl):
serverUrl = ui.clickPrompt("You've entered invalid server url. Please try again.", type = str)
dule1322 marked this conversation as resolved.
Show resolved Hide resolved

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)

Expand Down
3 changes: 3 additions & 0 deletions coretex/configuration/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions coretex/networking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions coretex/networking/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import random
import logging

from urllib.parse import urlsplit, urlunsplit, SplitResult

from .network_response import NetworkResponse


Expand Down Expand Up @@ -73,6 +75,13 @@ def logRequestFailure(endpoint: str, response: NetworkResponse) -> None:
logger.debug(f"\tResponse: {response.getContent()!r}")


def baseUrl(url: str) -> str:
result = urlsplit(url)
parsed = SplitResult(result.scheme, result.netloc, "", "", "")

return urlunsplit(parsed)


def getDelayBeforeRetry(retry: int) -> int:
# retry starts from 0 so we add +1/+2 to start/end
# to make it have proper delay
Expand Down
Loading