code: def add_version_to_url(url, version, replace=False):
      """Add a version ID to `url`.
  
      Parameters
      ----------
      url : datalad.support.network.URL
          A URL.
      version : str
          The value of 'versionId='.
      replace : boolean, optional
          If a versionID is already present in `url`, replace it.
  
      Returns
      -------
      A versioned URL (str)
      """
      version_id = "versionId={}".format(version)
      if not url.query:
          query = version_id
      else:
          ver_match = re.match(
              "(?P<pre>.*&)?"
              "(?P<vers>versionId=[^&]+)"
              "(?P<post>&.*)?",
              url.query,
          )
          if ver_match:
              if replace:
                  query = "".join(
                      [
                          ver_match.group("pre") or "",
                          version_id,
                          ver_match.group("post") or "",
                      ]
                  )
              else:
                  query = url.query
          else:
              query = url.query + "&" + version_id
      return URL(**dict(url.fields, query=query)).as_str()
  
imports: import mimetypes
  from os.path import splitext
  import re
  from datalad.support.network import urlquote, URL
  import logging
  import datalad.log
  from datalad.support.exceptions import CapturedException, DownloadError, AccessDeniedError, AccessPermissionExpiredError, AnonymousAccessDeniedError
  from datalad.utils import try_multiple_dec
  from urllib.request import urlopen, Request
label_desc: Use image node_id 16 for calling a global function with example usage: add_version_to_url(url, version, replace) and returns: URL