def remove_version_from_href(href):
"""Removes the first api version from the href.
Given: 'http://www.meteos.com/v1.1/123'
Returns: 'http://www.meteos.com/123'
Given: 'http://www.meteos.com/v1.1'
Returns: 'http://www.meteos.com'
"""
parsed_url = parse.urlsplit(href)
url_parts = parsed_url.path.split('/', 2)
# NOTE: this should match vX.X or vX
expression = re.compile(r'^v([0-9]+|[0-9]+\.[0-9]+)(/.*|$)')
if expression.match(url_parts[1]):
del url_parts[1]
new_path = '/'.join(url_parts)
if new_path == parsed_url.path:
msg = 'href %s does not contain version' % href
LOG.debug(msg)
raise ValueError(msg)
parsed_url = list(parsed_url)
parsed_url[2] = new_path
return parse.urlunsplit(parsed_url)
评论列表
文章目录