Skip to content

Commit

Permalink
Merge pull request #258 from bogdant36/CTX-6577-v1
Browse files Browse the repository at this point in the history
CTX-6577(part): serverUrl bug fix and added prompt for specifying ser…
  • Loading branch information
dule1322 authored Aug 27, 2024
2 parents d4afb05 + 7c3d1f7 commit a2ae22d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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)

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

0 comments on commit a2ae22d

Please sign in to comment.