@@ -24,7 +24,8 @@ class Client(object):
2424
2525 def __init__ (self , username , api_key , server = DEFAULT_SERVER ,
2626 version = DEFAULT_VERSION , http_proxy = None , https_proxy = None ,
27- verify = True , headers = None , debug = False , exception_class = Exception ):
27+ verify = True , headers = None , debug = False , exception_class = Exception ,
28+ session = None ):
2829 """Initial loading of the client.
2930
3031 :param str username: API username in email address format
@@ -63,18 +64,25 @@ def __init__(self, username, api_key, server=DEFAULT_SERVER,
6364 self .verify = False
6465 self .exception_class = exception_class
6566 self .set_context ('python' ,'passivetotal' ,VERSION )
67+ self .session = session or requests .Session ()
6668
6769 @classmethod
68- def from_config (cls ):
69- """Method to return back a loaded instance."""
70+ def from_config (cls , ** kwargs ):
71+ """Method to return back a loaded instance.
72+
73+ kwargs override configuration file variables if provided and are passed to the object constructor.
74+ """
75+ arg_keys = ['username' ,'api_key' ,'server' ,'version' ,'http_proxy' ,'https_proxy' ]
76+ args = { k : kwargs .pop (k ) if k in kwargs else None for k in arg_keys }
7077 config = Config ()
7178 client = cls (
72- username = config .get ('username' ),
73- api_key = config .get ('api_key' ),
74- server = config .get ('api_server' ),
75- version = config .get ('api_version' ),
76- http_proxy = config .get ('http_proxy' ),
77- https_proxy = config .get ('https_proxy' ),
79+ username = args .get ('username' ) or config .get ('username' ),
80+ api_key = args .get ('api_key' ) or config .get ('api_key' ),
81+ server = args .get ('server' ) or config .get ('api_server' ),
82+ version = args .get ('version' ) or config .get ('api_version' ),
83+ http_proxy = args .get ('http_proxy' ) or config .get ('http_proxy' ),
84+ https_proxy = args .get ('https_proxy' ) or config .get ('https_proxy' ),
85+ ** kwargs
7886 )
7987 return client
8088
@@ -155,7 +163,7 @@ def _get(self, endpoint, action, *url_args, **url_params):
155163 if self .proxies :
156164 kwargs ['proxies' ] = self .proxies
157165 self .logger .debug ("Requesting: %s, %s" % (api_url , str (kwargs )))
158- response = requests .get (api_url , ** kwargs )
166+ response = self . session .get (api_url , ** kwargs )
159167 return self ._json (response )
160168
161169 def _get_special (self , endpoint , action , trail , data , * url_args , ** url_params ):
@@ -175,7 +183,7 @@ def _get_special(self, endpoint, action, trail, data, *url_args, **url_params):
175183 'auth' : (self .username , self .api_key )}
176184 if self .proxies :
177185 kwargs ['proxies' ] = self .proxies
178- response = requests .get (api_url , ** kwargs )
186+ response = self . session .get (api_url , ** kwargs )
179187 return self ._json (response )
180188
181189 def _send_data (self , method , endpoint , action ,
@@ -196,7 +204,7 @@ def _send_data(self, method, endpoint, action,
196204 'auth' : (self .username , self .api_key )}
197205 if self .proxies :
198206 kwargs ['proxies' ] = self .proxies
199- response = requests .request (method , api_url , ** kwargs )
207+ response = self . session .request (method , api_url , ** kwargs )
200208 return self ._json (response )
201209
202210
0 commit comments