|  | @@ -0,0 +1,61 @@
 | 
	
		
			
			|  | 1 | +import requests
 | 
	
		
			
			|  | 2 | +
 | 
	
		
			
			|  | 3 | +
 | 
	
		
			
			|  | 4 | +the_http_adapter = requests.adapters.HTTPAdapter(pool_connections=100)
 | 
	
		
			
			|  | 5 | +the_https_adapter = requests.adapters.HTTPAdapter(pool_connections=100)
 | 
	
		
			
			|  | 6 | +
 | 
	
		
			
			|  | 7 | +
 | 
	
		
			
			|  | 8 | +class SessionSinglePool(requests.Session):
 | 
	
		
			
			|  | 9 | +
 | 
	
		
			
			|  | 10 | +    def __init__(self):
 | 
	
		
			
			|  | 11 | +        global the_https_adapter, the_http_adapter
 | 
	
		
			
			|  | 12 | +        super(SessionSinglePool, self).__init__()
 | 
	
		
			
			|  | 13 | +
 | 
	
		
			
			|  | 14 | +        # reuse the same adapters
 | 
	
		
			
			|  | 15 | +        self.adapters.clear()
 | 
	
		
			
			|  | 16 | +        self.mount('https://', the_https_adapter)
 | 
	
		
			
			|  | 17 | +        self.mount('http://', the_http_adapter)
 | 
	
		
			
			|  | 18 | +
 | 
	
		
			
			|  | 19 | +    def close(self):
 | 
	
		
			
			|  | 20 | +        """Call super, but clear adapters since there are managed globaly"""
 | 
	
		
			
			|  | 21 | +        self.adapters.clear()
 | 
	
		
			
			|  | 22 | +        super(SessionSinglePool, self).close()
 | 
	
		
			
			|  | 23 | +
 | 
	
		
			
			|  | 24 | +
 | 
	
		
			
			|  | 25 | +def request(method, url, **kwargs):
 | 
	
		
			
			|  | 26 | +    """same as requests/requests/api.py request(...) except it use SessionSinglePool"""
 | 
	
		
			
			|  | 27 | +    session = SessionSinglePool()
 | 
	
		
			
			|  | 28 | +    response = session.request(method=method, url=url, **kwargs)
 | 
	
		
			
			|  | 29 | +    session.close()
 | 
	
		
			
			|  | 30 | +    return response
 | 
	
		
			
			|  | 31 | +
 | 
	
		
			
			|  | 32 | +
 | 
	
		
			
			|  | 33 | +def get(url, **kwargs):
 | 
	
		
			
			|  | 34 | +    kwargs.setdefault('allow_redirects', True)
 | 
	
		
			
			|  | 35 | +    return request('get', url, **kwargs)
 | 
	
		
			
			|  | 36 | +
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +def options(url, **kwargs):
 | 
	
		
			
			|  | 39 | +    kwargs.setdefault('allow_redirects', True)
 | 
	
		
			
			|  | 40 | +    return request('options', url, **kwargs)
 | 
	
		
			
			|  | 41 | +
 | 
	
		
			
			|  | 42 | +
 | 
	
		
			
			|  | 43 | +def head(url, **kwargs):
 | 
	
		
			
			|  | 44 | +    kwargs.setdefault('allow_redirects', False)
 | 
	
		
			
			|  | 45 | +    return request('head', url, **kwargs)
 | 
	
		
			
			|  | 46 | +
 | 
	
		
			
			|  | 47 | +
 | 
	
		
			
			|  | 48 | +def post(url, data=None, json=None, **kwargs):
 | 
	
		
			
			|  | 49 | +    return request('post', url, data=data, json=json, **kwargs)
 | 
	
		
			
			|  | 50 | +
 | 
	
		
			
			|  | 51 | +
 | 
	
		
			
			|  | 52 | +def put(url, data=None, **kwargs):
 | 
	
		
			
			|  | 53 | +    return request('put', url, data=data, **kwargs)
 | 
	
		
			
			|  | 54 | +
 | 
	
		
			
			|  | 55 | +
 | 
	
		
			
			|  | 56 | +def patch(url, data=None, **kwargs):
 | 
	
		
			
			|  | 57 | +    return request('patch', url, data=data, **kwargs)
 | 
	
		
			
			|  | 58 | +
 | 
	
		
			
			|  | 59 | +
 | 
	
		
			
			|  | 60 | +def delete(url, **kwargs):
 | 
	
		
			
			|  | 61 | +    return request('delete', url, **kwargs)
 |