2525from .database import CloudantDatabase , CouchDatabase
2626from .feed import Feed , InfiniteFeed
2727from .error import CloudantException , CloudantArgumentError
28- from ._common_util import USER_AGENT , append_response_error_content
28+ from ._common_util import (
29+ USER_AGENT ,
30+ append_response_error_content ,
31+ InfiniteSession
32+ )
2933
3034
3135class CouchDB (dict ):
@@ -49,6 +53,9 @@ class CouchDB(dict):
4953 configuring requests.
5054 :param bool connect: Keyword argument, if set to True performs the call to
5155 connect as part of client construction. Default is False.
56+ :param bool auto_renew: Keyword argument, if set to True performs
57+ automatic renewal of expired session authentication settings.
58+ Default is False.
5259 """
5360 _DATABASE_CLASS = CouchDatabase
5461
@@ -63,7 +70,9 @@ def __init__(self, user, auth_token, admin_party=False, **kwargs):
6370 self .encoder = kwargs .get ('encoder' ) or json .JSONEncoder
6471 self .adapter = kwargs .get ('adapter' )
6572 self .r_session = None
66- if kwargs .get ('connect' , False ):
73+ self ._auto_renew = kwargs .get ('auto_renew' , False )
74+ connect_to_couch = kwargs .get ('connect' , False )
75+ if connect_to_couch and self ._DATABASE_CLASS == CouchDatabase :
6776 self .connect ()
6877
6978 def connect (self ):
@@ -74,7 +83,14 @@ def connect(self):
7483 if self .r_session :
7584 return
7685
77- self .r_session = requests .Session ()
86+ if self ._auto_renew and not self .admin_party :
87+ self .r_session = InfiniteSession (
88+ self ._user ,
89+ self ._auth_token ,
90+ self .server_url
91+ )
92+ else :
93+ self .r_session = requests .Session ()
7894 # If a Transport Adapter was supplied add it to the session
7995 if self .adapter is not None :
8096 self .r_session .mount (self .server_url , self .adapter )
@@ -398,7 +414,6 @@ class Cloudant(CouchDB):
398414
399415 def __init__ (self , cloudant_user , auth_token , ** kwargs ):
400416 super (Cloudant , self ).__init__ (cloudant_user , auth_token , ** kwargs )
401-
402417 self ._client_user_header = {'User-Agent' : USER_AGENT }
403418 account = kwargs .get ('account' )
404419 url = kwargs .get ('url' )
@@ -413,6 +428,9 @@ def __init__(self, cloudant_user, auth_token, **kwargs):
413428 if self .server_url is None :
414429 raise CloudantException ('You must provide a url or an account.' )
415430
431+ if kwargs .get ('connect' , False ):
432+ self .connect ()
433+
416434 def db_updates (self , raw_data = False , ** kwargs ):
417435 """
418436 Returns the ``_db_updates`` feed iterator. The ``_db_updates`` feed can
0 commit comments