Skip to content
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
7 changes: 3 additions & 4 deletions qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from requests.adapters import HTTPAdapter, Retry

from .interfaces import QfcException, QfcRequest, QfcRequestException
from .utils import calc_etag, log
from .utils import calc_etag, log, add_trailing_slash_to_url
from pathvalidate import is_valid_filepath


Expand Down Expand Up @@ -1727,11 +1727,10 @@ def _request(
if path.startswith("/"):
path = path[1:]

if not path.endswith("/"):
path += "/"

path = self.url + path

path = add_trailing_slash_to_url(path)

if pagination:
limit = pagination.limit or DEFAULT_PAGINATION_LIMIT
offset = pagination.offset or 0
Expand Down
36 changes: 36 additions & 0 deletions qfieldcloud_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from typing import List
from urllib.parse import urlparse, urlunparse


def print_json(data):
Expand Down Expand Up @@ -114,3 +115,38 @@ def format_project_table(projects: List) -> str:
headers=["ID", "OWNER/NAME", "IS PUBLIC", "DESCRIPTION"],
data=data,
)


def add_trailing_slash_to_url(url: str) -> str:
"""
Add a trailing slash to a URL if it doesn't already have one.

Args:
url (str): The URL to process

Returns:
str: URL with trailing slash added

Examples:
>>> add_trailing_slash("https://example.com")
'https://example.com/'
>>> add_trailing_slash("https://example.com/")
'https://example.com/'
>>> add_trailing_slash("https://example.com/path")
'https://example.com/path/'
>>> add_trailing_slash("https://example.com/file.txt")
'https://example.com/file.txt/'
"""
if not url:
return url

parsed = urlparse(url)

# Add trailing slash to path if it doesn't end with one
path = parsed.path
if not path.endswith("/"):
path += "/"

# Reconstruct the URL with the modified path
modified_parsed = parsed._replace(path=path)
return urlunparse(modified_parsed)