changeset 3829:67fc066ed2cd

tools (common/uri): accept URIs without type: URIs without type are used to link JIDs. rel 369
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2022 16:30:37 +0200
parents 432aaba0d7fe
children 68a11b95a7d3
files sat/tools/common/uri.py
diffstat 1 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/uri.py	Sat Jul 09 16:29:36 2022 +0200
+++ b/sat/tools/common/uri.py	Sat Jul 09 16:30:37 2022 +0200
@@ -19,6 +19,7 @@
 
 """ XMPP uri parsing tools """
 
+from typing import Optional
 import sys
 import urllib.parse
 import urllib.request, urllib.parse, urllib.error
@@ -50,9 +51,13 @@
     data = {"path": urllib.parse.unquote(uri_split.path)}
 
     query_end = uri_split.query.find(";")
-    query_type = uri_split.query[:query_end]
-    if query_end == -1 or "=" in query_type:
-        raise ValueError("no query type, invalid XMPP URI")
+    if query_end == -1:
+        # we just have a JID
+        query_type = None
+    else:
+        query_type = uri_split.query[:query_end]
+        if "=" in query_type:
+            raise ValueError("no query type, invalid XMPP URI")
 
     if sys.version_info >= (3, 9):
         # parse_qs behaviour has been modified in Python 3.9, ";" is not understood as a
@@ -92,13 +97,19 @@
         )
 
 
-def buildXMPPUri(type_, **kwargs):
+def buildXMPPUri(type_: Optional[str] = None, **kwargs: str) -> str:
     uri = ["xmpp:"]
     subtype = kwargs.pop("subtype", None)
     path = kwargs.pop("path")
     uri.append(urllib.parse.quote_plus(path.encode("utf-8")).replace("%40", "@"))
 
-    if type_ == "pubsub":
+    if type_ is None:
+        # we have an URI to a JID
+        if kwargs:
+            raise NotImplementedError(
+                "keyword arguments are not supported for URI without type"
+            )
+    elif type_ == "pubsub":
         if subtype == "microblog" and not kwargs.get("node"):
             kwargs["node"] = "urn:xmpp:microblog:0"
         if kwargs: