Merge "Add parsing the endpoint URL"
This commit is contained in:
@@ -20,6 +20,7 @@ import hashlib
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import six.moves.urllib.parse as urlparse
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import uuid
|
import uuid
|
||||||
@@ -396,11 +397,13 @@ def strip_version(endpoint):
|
|||||||
version = None
|
version = None
|
||||||
# Get rid of trailing '/' if present
|
# Get rid of trailing '/' if present
|
||||||
endpoint = endpoint.rstrip('/')
|
endpoint = endpoint.rstrip('/')
|
||||||
url_bits = endpoint.split('/')
|
url_parts = urlparse.urlparse(endpoint)
|
||||||
|
(scheme, netloc, path, __, __, __) = url_parts
|
||||||
|
path = path.lstrip('/')
|
||||||
# regex to match 'v1' or 'v2.0' etc
|
# regex to match 'v1' or 'v2.0' etc
|
||||||
if re.match('v\d+\.?\d*', url_bits[-1]):
|
if re.match('v\d+\.?\d*', path):
|
||||||
version = float(url_bits[-1].lstrip('v'))
|
version = float(path.lstrip('v'))
|
||||||
endpoint = '/'.join(url_bits[:-1])
|
endpoint = scheme + '://' + netloc
|
||||||
return endpoint, version
|
return endpoint, version
|
||||||
|
|
||||||
|
|
||||||
|
@@ -44,3 +44,23 @@ class ClientTest(testtools.TestCase):
|
|||||||
gc = client.Client(2.2, "http://example.com/v2.1")
|
gc = client.Client(2.2, "http://example.com/v2.1")
|
||||||
self.assertEqual("http://example.com", gc.http_client.endpoint)
|
self.assertEqual("http://example.com", gc.http_client.endpoint)
|
||||||
self.assertIsInstance(gc, v2.client.Client)
|
self.assertIsInstance(gc, v2.client.Client)
|
||||||
|
|
||||||
|
def test_endpoint_with_version_hostname(self):
|
||||||
|
gc = client.Client(2, "http://v1.example.com")
|
||||||
|
self.assertEqual("http://v1.example.com", gc.http_client.endpoint)
|
||||||
|
self.assertIsInstance(gc, v2.client.Client)
|
||||||
|
|
||||||
|
def test_versioned_endpoint_with_version_hostname_v2(self):
|
||||||
|
gc = client.Client(endpoint="http://v1.example.com/v2")
|
||||||
|
self.assertEqual("http://v1.example.com", gc.http_client.endpoint)
|
||||||
|
self.assertIsInstance(gc, v2.client.Client)
|
||||||
|
|
||||||
|
def test_versioned_endpoint_with_version_hostname_v1(self):
|
||||||
|
gc = client.Client(endpoint="http://v2.example.com/v1")
|
||||||
|
self.assertEqual("http://v2.example.com", gc.http_client.endpoint)
|
||||||
|
self.assertIsInstance(gc, v1.client.Client)
|
||||||
|
|
||||||
|
def test_versioned_endpoint_with_minor_revision_and_version_hostname(self):
|
||||||
|
gc = client.Client(endpoint="http://v1.example.com/v2.1")
|
||||||
|
self.assertEqual("http://v1.example.com", gc.http_client.endpoint)
|
||||||
|
self.assertIsInstance(gc, v2.client.Client)
|
||||||
|
Reference in New Issue
Block a user