return items[start_index:range_end]
-def get_id_from_href(href):
- """Return the id or uuid portion of a url.
-
- Given: 'http://www.foo.com/bar/123?q=4'
- Returns: '123'
-
- Given: 'http://www.foo.com/bar/abc123?q=4'
- Returns: 'abc123'
-
- """
- return urlparse.urlsplit("%s" % href).path.split('/')[-1]
-
-
def remove_version_from_href(href):
"""Removes the first api version from the href.
return urlparse.urlunsplit(parsed_url)
-def get_version_from_href(href):
- """Returns the api version in the href.
-
- Returns the api version in the href.
- If no version is found, '2' is returned
-
- Given: 'http://www.cinder.com/123'
- Returns: '2'
-
- Given: 'http://www.cinder.com/v1.1'
- Returns: '1.1'
-
- """
- try:
- expression = r'/v([0-9]+|[0-9]+\.[0-9]+)(/|$)'
- return re.findall(expression, href)[0][0]
- except IndexError:
- return '2'
-
-
def dict_to_query_str(params):
# TODO(throughnothing): we should just use urllib.urlencode instead of this
# But currently we don't work with urlencoded url's
self.assertRaises(ValueError,
common.remove_version_from_href,
fixture)
-
- def test_get_id_from_href_with_int_url(self):
- fixture = 'http://www.testsite.com/dir/45'
- actual = common.get_id_from_href(fixture)
- expected = '45'
- self.assertEqual(actual, expected)
-
- def test_get_id_from_href_with_int(self):
- fixture = '45'
- actual = common.get_id_from_href(fixture)
- expected = '45'
- self.assertEqual(actual, expected)
-
- def test_get_id_from_href_with_int_url_query(self):
- fixture = 'http://www.testsite.com/dir/45?asdf=jkl'
- actual = common.get_id_from_href(fixture)
- expected = '45'
- self.assertEqual(actual, expected)
-
- def test_get_id_from_href_with_uuid_url(self):
- fixture = 'http://www.testsite.com/dir/abc123'
- actual = common.get_id_from_href(fixture)
- expected = "abc123"
- self.assertEqual(actual, expected)
-
- def test_get_id_from_href_with_uuid_url_query(self):
- fixture = 'http://www.testsite.com/dir/abc123?asdf=jkl'
- actual = common.get_id_from_href(fixture)
- expected = "abc123"
- self.assertEqual(actual, expected)
-
- def test_get_id_from_href_with_uuid(self):
- fixture = 'abc123'
- actual = common.get_id_from_href(fixture)
- expected = 'abc123'
- self.assertEqual(actual, expected)
-
- def test_get_version_from_href(self):
- fixture = 'http://www.testsite.com/v1.1/images'
- expected = '1.1'
- actual = common.get_version_from_href(fixture)
- self.assertEqual(actual, expected)
-
- def test_get_version_from_href_2(self):
- fixture = 'http://www.testsite.com/v1.1'
- expected = '1.1'
- actual = common.get_version_from_href(fixture)
- self.assertEqual(actual, expected)
-
- def test_get_version_from_href_default(self):
- fixture = 'http://www.testsite.com/images'
- expected = '2'
- actual = common.get_version_from_href(fixture)
- self.assertEqual(actual, expected)