Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6b85dc3

Browse files
committed
Add IAM class method to Cloudant class
1 parent 59ca68f commit 6b85dc3

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/cloudant/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,29 @@ def cloudant(user, passwd, **kwargs):
6363
cloudant_session.disconnect()
6464

6565
@contextlib.contextmanager
66-
def cloudant_iam(api_key, account_name, **kwargs):
66+
def cloudant_iam(account_name, api_key, **kwargs):
6767
"""
68-
Provides a context manager to create a Cloudant session and provide access
69-
to databases, docs etc.
68+
Provides a context manager to create a Cloudant session using IAM
69+
authentication and provide access to databases, docs etc.
7070
71-
:param api_key: IAM authentication API key.
7271
:param account_name: Cloudant account name.
72+
:param api_key: IAM authentication API key.
73+
74+
For example:
75+
76+
.. code-block:: python
77+
78+
# cloudant context manager
79+
from cloudant import cloudant_iam
80+
81+
with cloudant_iam(ACCOUNT_NAME, API_KEY) as client:
82+
# Context handles connect() and disconnect() for you.
83+
# Perform library operations within this context. Such as:
84+
print client.all_dbs()
85+
# ...
86+
7387
"""
74-
cloudant_session = Cloudant(account_name, api_key, use_iam=True, **kwargs)
88+
cloudant_session = Cloudant.iam(account_name, api_key, **kwargs)
7589

7690
cloudant_session.connect()
7791
yield cloudant_session

src/cloudant/client.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from ._common_util import (
3030
USER_AGENT,
3131
append_response_error_content,
32-
InfiniteSession,
3332
ClientSession,
3433
CloudFoundryService,
3534
CookieSession,
@@ -68,6 +67,10 @@ class CouchDB(dict):
6867
`Requests library timeout argument
6968
<http://docs.python-requests.org/en/master/user/quickstart/#timeouts>`_.
7069
but will apply to every request made using this client.
70+
:param bool use_iam: Keyword argument, if set to True performs
71+
IAM authentication with server. Default is False.
72+
Use :func:`~cloudant.client.CouchDB.iam` to construct an IAM
73+
authenticated client.
7174
"""
7275
_DATABASE_CLASS = CouchDatabase
7376

@@ -147,16 +150,8 @@ def session(self):
147150
"""
148151
if self.admin_party:
149152
return None
150-
<<<<<<< HEAD
151-
sess_url = '/'.join((self.server_url, '_session'))
152-
resp = self.r_session.get(sess_url)
153-
resp.raise_for_status()
154-
sess_data = resp.json()
155-
return sess_data
156-
=======
157153

158154
return self.r_session.info()
159-
>>>>>>> Add IAM authentication support
160155

161156
def session_cookie(self):
162157
"""
@@ -175,21 +170,8 @@ def session_login(self):
175170
"""
176171
if self.admin_party:
177172
return
178-
<<<<<<< HEAD
179-
sess_url = '/'.join((self.server_url, '_session'))
180-
resp = self.r_session.post(
181-
sess_url,
182-
data={
183-
'name': user,
184-
'password': passwd
185-
},
186-
headers={'Content-Type': 'application/x-www-form-urlencoded'}
187-
)
188-
resp.raise_for_status()
189-
=======
190173

191174
self.r_session.login()
192-
>>>>>>> Add IAM authentication support
193175

194176
def session_logout(self):
195177
"""
@@ -198,14 +180,8 @@ def session_logout(self):
198180
"""
199181
if self.admin_party:
200182
return
201-
<<<<<<< HEAD
202-
sess_url = '/'.join((self.server_url, '_session'))
203-
resp = self.r_session.delete(sess_url)
204-
resp.raise_for_status()
205-
=======
206183

207184
self.r_session.logout()
208-
>>>>>>> Add IAM authentication support
209185

210186
def basic_auth_str(self):
211187
"""
@@ -805,3 +781,13 @@ def bluemix(cls, vcap_services, instance_name=None, **kwargs):
805781
service.password,
806782
url=service.url,
807783
**kwargs)
784+
785+
@classmethod
786+
def iam(cls, account_name, api_key, **kwargs):
787+
"""
788+
Create a Cloudant client that uses IAM authentication.
789+
790+
:param account_name: Cloudant account name.
791+
:param api_key: IAM authentication API key.
792+
"""
793+
return cls(None, api_key, account=account_name, use_iam=True, **kwargs)

0 commit comments

Comments
 (0)