@@ -62,6 +62,101 @@ Connecting with a client
6262 # Disconnect from the server
6363 client.disconnect()
6464
65+ **************
66+ Authentication
67+ **************
68+
69+ When constructing a ``Cloudant `` client, you can authenticate using the
70+ [cookie authentication](http://guide.couchdb.org/editions/1/en/security.html#cookies) functionality.
71+ The server will always attempt to automatically renew the cookie
72+ shortly before its expiry. However, if the client does not send a
73+ request to the server during this renewal window and
74+ ``auto_renew=False `` then the cookie is not renewed.
75+
76+ Using ``auto_renew=True `` will attempt to renew the cookie at
77+ any point during the lifetime of the session when either of the
78+ following statements hold true:
79+
80+ - The server returns a ``credentials_expired `` error message.
81+ - The server returns a ``401 Unauthorized `` status code.
82+ - The server returns a ``403 Forbidden `` status code.
83+
84+ .. code-block :: python
85+
86+ # Create client using auto_renew to automatically renew expired cookie auth
87+ client = Cloudant(USERNAME , PASSWORD , url = ' https://acct.cloudant.com' ,
88+ connect = True ,
89+ auto_renew = True )
90+
91+ ****************
92+ Resource sharing
93+ ****************
94+
95+ The ``Cloudant `` or ``CouchDB `` client objects make HTTP calls using the ``requests `` library.
96+ ``requests `` uses the [urllib3](https://pypi.python.org/pypi/urllib3) library which features
97+ connection pooling and thread safety.
98+
99+ Connection pools can be managed by using the ``requests `` library's
100+ [HTTPAdapter](https://github.com/kennethreitz/requests/blob/master/requests/adapters.py#L78)
101+ when constructing a ``Cloudant `` or ``ClouchDB `` client instance.
102+ The default number set by the ``urllib3 `` library for cached connection pools is 10.
103+ Use the ``HTTPAdapter `` argument ``pool_connections `` to set the number of
104+ urllib3 connection pools to cache, and the ``pool_maxsize `` argument to set the
105+ maximum number of connections to save in the pool.
106+
107+ Although the ``client `` session is documented as thread safe and it's possible for a
108+ static ``client `` to be accessible by multiple threads, there are still cases that do not
109+ guarantee thread safe execution. It's recommended to use one ``client `` object per thread.
110+
111+ .. code-block :: python
112+
113+ # Create client with 15 cached pool connections and a max pool size of 100
114+ httpAdapter = HTTPAdapter(pool_connections = 15 , pool_maxsize = 100 )
115+ client = Cloudant(USERNAME , PASSWORD , url = ' https://acct.cloudant.com'
116+ connect = True ,
117+ adapter = httpAdapter)
118+
119+ Note: Idle connections within the pool may be terminated by the server, so will not remain open
120+ indefinitely meaning that this will not completely remove the overhead of creating new connections.
121+
122+ Using library in app server environment
123+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
124+
125+ This library can be used in an app server, and the example
126+ below shows how to use ``client `` in a ``flask `` app server.
127+
128+ .. code-block :: python
129+
130+ from flask import Flask
131+ import atexit
132+
133+ app = Flask(__name__ )
134+
135+ @app.route (' /' )
136+ def hello_world ():
137+ # Cookie authentication can be renewed automatically using ``auto_renew=True``
138+ # which is typically what you would require when running in an application
139+ # server where the connection may stay open for a long period of time
140+
141+ # Note: Each time you instantiate an instance of the Cloudant client, an
142+ # authentication request will be made to Cloudant to retrieve the session cookie.
143+ # If the performance overhead of this call is a concern for you, consider
144+ # using vanilla python requests with a custom subclass of HTTPAdapter that
145+ # performs the authentication call to Cloudant when it establishes the http
146+ # connection during the creation of the connection pool.
147+ client = Cloudant(USERNAME , PASSWORD , url = ' https://acct.cloudant.com' ,
148+ connect = True ,
149+ auto_renew = True )
150+
151+ # do something with client
152+ return ' Hello World!'
153+
154+ # When shutting down the app server, use ``client.disconnect()`` to properly
155+ # logout and end the ``client`` session
156+ @atexit.register
157+ def shutdown ():
158+ client.disconnect()
159+
65160*********
66161Databases
67162*********
0 commit comments