Keyston Session can be used to instantiate client
The session is used to fetch the almanach url from keystone Change-Id: I7122c5870624985dc122a9b78ffebdb4a925235d
This commit is contained in:
parent
2107e7bfc1
commit
6424eb419a
@ -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):
|
||||
|
@ -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 = {
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user