Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Support use of UI URLs for API.get method
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmello committed Aug 18, 2022
1 parent 1c3eab8 commit 4681d0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cript/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
from cript.nodes.primary.user import User
from cript.nodes.primary.file import File
from cript.nodes.secondary.base_secondary import BaseSecondary
from cript.utils import get_api_url, convert_file_size, display_errors
from cript.utils import (
get_api_url,
convert_to_api_url,
convert_file_size,
display_errors,
)
from cript.exceptions import (
APIAuthError,
APIRefreshError,
Expand Down Expand Up @@ -615,6 +620,7 @@ def get(
"""
# Get node with a URL
if isinstance(obj, str):
obj = convert_to_api_url(obj)
if self.api_url not in obj:
raise APIGetError("Please enter a valid node URL.")
response = self.session.get(obj)
Expand Down
17 changes: 17 additions & 0 deletions src/cript/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import math
import json
from urllib.parse import urlparse


def get_api_url(host: str, tls: bool = True):
Expand All @@ -21,6 +22,22 @@ def get_api_url(host: str, tls: bool = True):
return f"{protocol}://{host}/api"


def convert_to_api_url(url: str):
"""
Convert a UI URL to an API URL.
e.g., https://criptapp.org/material/ --> https://criptapp.org/api/material/
:param url: The original UI URL that will be converted.
:return: The converted API URL.
:rtype: str
"""
parsed_url = urlparse(url)
scheme = parsed_url.scheme
netloc = parsed_url.netloc
path = parsed_url.path
return f"{scheme}://{netloc}/api{path}"


def auto_assign_group(group, parent):
"""
Decide whether to inherit the group from a node's parent.
Expand Down

0 comments on commit 4681d0e

Please sign in to comment.