diff --git a/almanachclient/http_client.py b/almanachclient/http_client.py index 973a5f9..4495df1 100644 --- a/almanachclient/http_client.py +++ b/almanachclient/http_client.py @@ -26,8 +26,27 @@ logger = logging.getLogger(__name__) class HttpClient(metaclass=abc.ABCMeta): - def __init__(self, url, token=None): - self.url = url + def __init__(self, url=None, token=None, + session=None, region_name=None, + service_type='cloudmetrics', endpoint_type=None): + """Initialization of Client object. + + :param string url: The endpoint of the Almanach service. Overrides the session url. + :param string token: The Almanach X-Auth-Token. + :param session: A session object that can be used for communication. + :type session: keystonauth.session.Session + :param string region_name: + :param string service_type: + :param string endpoint_type: + """ + if not url and not session: + raise ValueError('A session or an endpoint must be provided') + + if session: + self.url = session.get_endpoint(service_type=service_type, region_name=region_name, interface=endpoint_type) + if url: + self.url = url + self.token = token def _get(self, url, params=None): diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index 3cad915..7995554 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -37,6 +37,26 @@ class TestClient(base.TestCase): self.client = Client(self.url, self.token) + self.session = mock.Mock() + self.session.get_endpoint.return_value = 'http://almanach_url/from/session' + + def test_instantiate_client_with_a_session(self): + client = Client(session=self.session) + + self.assertEqual('http://almanach_url/from/session', client.get_url()) + + self.session.get_endpoint.called_once_with(service_type='cloudmetrics', region_name=None, interface=None) + + def test_instantiate_client_endpoint_url_override_session_url(self): + client = Client(url='http://almanach_url', session=self.session) + + self.assertEqual('http://almanach_url', client.get_url()) + + self.session.get_endpoint.called_once_with(service_type='cloudmetrics', region_name=None, interface=None) + + def test_instantiate_client_no_url_and_no_session(self): + self.assertRaises(ValueError, Client) + @mock.patch('requests.get') def test_get_info(self, requests): expected = { diff --git a/doc/source/usage_api.rst b/doc/source/usage_api.rst index 71c93d3..9d675d6 100644 --- a/doc/source/usage_api.rst +++ b/doc/source/usage_api.rst @@ -9,10 +9,22 @@ Almanach Python API Usage ----- -First, create an Almanach Client instance with your credentials:: +First, create an Almanach Client instance by providing a Keystone session and your AUTH_TOKEN:: + >>> from keystoneauth1 import loading + >>> from keystoneauth1 import session >>> from almanachclient.v1.client import Client - >>> almanach = Client(ALMANACH_URL, AUTH_TOKEN) + + >>> loader = loading.get_plugin_loader('password') + >>> auth = loader.load_from_options(auth_url=AUTH_URL, + ... username=USERNAME, + ... password=PASSWORD, + ... project_name=PROJECT_NAME, + ... project_domain_name="Default", + ... user_domain_name="Default") + >>> sess = session.Session(auth=auth) + + >>> almanach = Client(session=sess, token=AUTH_TOKEN) Here ``ALMANACH_URL`` will be a string that represents the url of Almanach API. ``AUTH_TOKEN`` will be the authorization token you use to access the API.