Skip to content

Commit 446f3f0

Browse files
authored
Merge pull request #99 from digital-land/wrap-commas-url
Wrapper for URL's with commas
2 parents 09c2bb8 + eb1be1d commit 446f3f0

File tree

1 file changed

+16
-3
lines changed
  • request-processor/src/application/core

1 file changed

+16
-3
lines changed

request-processor/src/application/core/utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
logger = get_logger(__name__)
1111

1212

13+
def _quote_url_if_comma(url):
14+
"""Wrap url in double quotes if it contains a comma, to prevent CSV parsing errors."""
15+
if url and "," in url:
16+
return f'"{url}"'
17+
return url
18+
19+
1320
def get_request(url, verify_ssl=True):
1421
# log["ssl-verify"] = verify_ssl
1522
log = {"status": "", "message": ""}
@@ -315,14 +322,16 @@ def validate_endpoint(url, config_dir, plugin, start_date=None):
315322
]
316323
)
317324

325+
safe_url = _quote_url_if_comma(url)
326+
318327
endpoint_exists = False
319328
existing_entry = None
320329

321330
try:
322331
with open(endpoint_csv_path, "r", encoding="utf-8") as f:
323332
reader = csv.DictReader(f)
324333
for row in reader:
325-
if row.get("endpoint-url", "").strip() == url.strip():
334+
if row.get("endpoint-url", "").strip() == safe_url.strip():
326335
endpoint_exists = True
327336
existing_entry = {
328337
"endpoint": row.get("endpoint", ""),
@@ -350,7 +359,7 @@ def validate_endpoint(url, config_dir, plugin, start_date=None):
350359

351360
endpoint_key, new_endpoint_row = append_endpoint(
352361
endpoint_csv_path=endpoint_csv_path,
353-
endpoint_url=url,
362+
endpoint_url=safe_url,
354363
entry_date=entry_date,
355364
start_date=start_date,
356365
end_date="",
@@ -387,6 +396,10 @@ def validate_source(
387396
if not documentation_url:
388397
logger.warning("No documentation URL provided")
389398

399+
safe_documentation_url = (
400+
_quote_url_if_comma(documentation_url) if documentation_url else ""
401+
)
402+
390403
if not start_date:
391404
start_date = datetime.now().strftime("%Y-%m-%d")
392405
entry_date = datetime.now().isoformat()
@@ -397,7 +410,7 @@ def validate_source(
397410
organisation=organisation,
398411
endpoint_key=endpoint_key,
399412
attribution="",
400-
documentation_url=documentation_url or "",
413+
documentation_url=safe_documentation_url,
401414
licence=licence or "",
402415
pipelines=dataset,
403416
entry_date=entry_date,

0 commit comments

Comments
 (0)