11#!/usr/bin/env python
2- # Copyright (c) 2015, 2016 IBM. All rights reserved.
2+ # Copyright (c) 2015, 2016, 2017 IBM Corp . All rights reserved.
33#
44# Licensed under the Apache License, Version 2.0 (the "License");
55# you may not use this file except in compliance with the License.
@@ -292,19 +292,21 @@ class InfiniteSession(Session):
292292 information in the event of expired session authentication.
293293 """
294294
295- def __init__ (self , username , password , server_url ):
295+ def __init__ (self , username , password , server_url , ** kwargs ):
296296 super (InfiniteSession , self ).__init__ ()
297297 self ._username = username
298298 self ._password = password
299299 self ._server_url = server_url
300+ self ._timeout = kwargs .get ('timeout' , None )
300301
301302 def request (self , method , url , ** kwargs ):
302303 """
303304 Overrides ``requests.Session.request`` to perform a POST to the
304305 _session endpoint to renew Session cookie authentication settings and
305306 then retry the original request, if necessary.
306307 """
307- resp = super (InfiniteSession , self ).request (method , url , ** kwargs )
308+ resp = super (InfiniteSession , self ).request (
309+ method , url , timeout = self ._timeout , ** kwargs )
308310 path = url_parse (url ).path .lower ()
309311 post_to_session = method .upper () == 'POST' and path == '/_session'
310312 is_expired = any ((
@@ -319,10 +321,30 @@ def request(self, method, url, **kwargs):
319321 data = {'name' : self ._username , 'password' : self ._password },
320322 headers = {'Content-Type' : 'application/x-www-form-urlencoded' }
321323 )
322- resp = super (InfiniteSession , self ).request (method , url , ** kwargs )
324+ resp = super (InfiniteSession , self ).request (
325+ method , url , timeout = self ._timeout , ** kwargs )
323326
324327 return resp
325328
329+ class ClientSession (Session ):
330+ """
331+ This class extends Session and provides a default timeout.
332+ """
333+
334+ def __init__ (self , username , password , server_url , ** kwargs ):
335+ super (ClientSession , self ).__init__ ()
336+ self ._username = username
337+ self ._password = password
338+ self ._server_url = server_url
339+ self ._timeout = kwargs .get ('timeout' , None )
340+
341+ def request (self , method , url , ** kwargs ):
342+ """
343+ Overrides ``requests.Session.request`` to set the timeout.
344+ """
345+ resp = super (ClientSession , self ).request (
346+ method , url , timeout = self ._timeout , ** kwargs )
347+ return resp
326348
327349class CloudFoundryService (object ):
328350 """ Manages Cloud Foundry service configuration. """
0 commit comments