44"""
55import json
66import logging
7+ import socket
78
89import requests
10+ import requests .exceptions
911try :
1012 import requests_unixsocket
1113except ImportError : # pragma: no cover
1214 requests_unixsocket = None
1315
14- from consulate import api
15- from consulate import utils
16+ from consulate import api , exceptions , utils
1617
1718LOGGER = logging .getLogger (__name__ )
1819
@@ -71,23 +72,51 @@ def delete(self, uri):
7172 return self ._process_response (
7273 self .session .delete (uri , timeout = self .timeout ))
7374
74- def get (self , uri ):
75+ def get (self , uri , timeout = None ):
7576 """Perform a HTTP get
7677
7778 :param src uri: The URL to send the DELETE to
79+ :param timeout: How long to wait on the response
80+ :type timeout: int or float or None
7881 :rtype: consulate.api.Response
7982
8083 """
8184 LOGGER .debug ("GET %s" , uri )
82- return self ._process_response (
83- self .session .get (uri , timeout = self .timeout ))
85+ try :
86+ return self ._process_response (
87+ self .session .get (uri , timeout = timeout or self .timeout ))
88+ except (requests .exceptions .ConnectionError ,
89+ OSError , socket .error ) as err :
90+ raise exceptions .RequestError (str (err ))
91+
92+ def get_stream (self , uri ):
93+ """Perform a HTTP get that returns the response as a stream.
94+
95+ :param src uri: The URL to send the DELETE to
96+ :rtype: iterator
97+
98+ """
99+ LOGGER .debug ("GET Stream from %s" , uri )
100+ try :
101+ response = self .session .get (uri , stream = True )
102+ except (requests .exceptions .ConnectionError ,
103+ OSError , socket .error ) as err :
104+ raise exceptions .RequestError (str (err ))
105+ if response .encoding is None :
106+ response .encoding = 'utf-8'
107+ if utils .response_ok (response ):
108+ for line in response .iter_lines ():
109+ if line :
110+ yield line .decode ('utf-8' )
84111
85112 @prepare_data
86- def put (self , uri , data = None ):
113+ def put (self , uri , data = None , timeout = None ):
87114 """Perform a HTTP put
88115
89116 :param src uri: The URL to send the DELETE to
90117 :param str data: The PUT data
118+ :param timeout: How long to wait on the response
119+ :type timeout: int or float or None
91120 :rtype: consulate.api.Response
92121
93122 """
@@ -96,9 +125,14 @@ def put(self, uri, data=None):
96125 'Content-Type' : CONTENT_FORM
97126 if utils .is_string (data ) else CONTENT_JSON
98127 }
99- return self ._process_response (
100- self .session .put (
101- uri , data = data , headers = headers , timeout = self .timeout ))
128+ try :
129+ return self ._process_response (
130+ self .session .put (
131+ uri , data = data , headers = headers ,
132+ timeout = timeout or self .timeout ))
133+ except (requests .exceptions .ConnectionError ,
134+ OSError , socket .error ) as err :
135+ raise exceptions .RequestError (str (err ))
102136
103137 @staticmethod
104138 def _process_response (response ):
@@ -109,8 +143,12 @@ def _process_response(response):
109143 :rtype: consulate.api.Response
110144
111145 """
112- return api .Response (
113- response .status_code , response .content , response .headers )
146+ try :
147+ return api .Response (
148+ response .status_code , response .content , response .headers )
149+ except (requests .exceptions .ConnectionError ,
150+ OSError , socket .error ) as err :
151+ raise exceptions .RequestError (str (err ))
114152
115153
116154class UnixSocketRequest (Request ):
0 commit comments