1818import platform
1919
2020from pkg_resources import get_distribution
21- import six
2221from six .moves .urllib .parse import urlencode
2322
24- from google .cloud . exceptions import make_exception
23+ from google .cloud import exceptions
2524
2625
2726API_BASE_URL = 'https://www.googleapis.com'
@@ -67,8 +66,9 @@ def credentials(self):
6766 def http (self ):
6867 """A getter for the HTTP transport used in talking to the API.
6968
70- :rtype: :class:`httplib2.Http`
71- :returns: A Http object used to transport data.
69+ Returns:
70+ google.auth.transport.requests.AuthorizedSession:
71+ A :class:`requests.Session` instance.
7272 """
7373 return self ._client ._http
7474
@@ -168,23 +168,13 @@ def _make_request(self, method, url, data=None, content_type=None,
168168 custom behavior, for example, to defer an HTTP request and complete
169169 initialization of the object at a later time.
170170
171- :rtype: tuple of ``response`` (a dictionary of sorts)
172- and ``content`` (a string).
173- :returns: The HTTP response object and the content of the response,
174- returned by :meth:`_do_request`.
171+ :rtype: :class:`requests.Response`
172+ :returns: The HTTP response.
175173 """
176174 headers = headers or {}
177175 headers .update (self ._EXTRA_HEADERS )
178176 headers ['Accept-Encoding' ] = 'gzip'
179177
180- if data :
181- content_length = len (str (data ))
182- else :
183- content_length = 0
184-
185- # NOTE: str is intended, bytes are sufficient for headers.
186- headers ['Content-Length' ] = str (content_length )
187-
188178 if content_type :
189179 headers ['Content-Type' ] = content_type
190180
@@ -215,12 +205,11 @@ def _do_request(self, method, url, headers, data,
215205 (Optional) Unused ``target_object`` here but may be used by a
216206 superclass.
217207
218- :rtype: tuple of ``response`` (a dictionary of sorts)
219- and ``content`` (a string).
220- :returns: The HTTP response object and the content of the response.
208+ :rtype: :class:`requests.Response`
209+ :returns: The HTTP response.
221210 """
222- return self .http .request (uri = url , method = method , headers = headers ,
223- body = data )
211+ return self .http .request (
212+ url = url , method = method , headers = headers , data = data )
224213
225214 def api_request (self , method , path , query_params = None ,
226215 data = None , content_type = None , headers = None ,
@@ -281,7 +270,7 @@ def api_request(self, method, path, query_params=None,
281270
282271 :raises ~google.cloud.exceptions.GoogleCloudError: if the response code
283272 is not 200 OK.
284- :raises TypeError : if the response content type is not JSON.
273+ :raises ValueError : if the response content type is not JSON.
285274 :rtype: dict or str
286275 :returns: The API response payload, either as a raw string or
287276 a dictionary if the response is valid JSON.
@@ -296,21 +285,14 @@ def api_request(self, method, path, query_params=None,
296285 data = json .dumps (data )
297286 content_type = 'application/json'
298287
299- response , content = self ._make_request (
288+ response = self ._make_request (
300289 method = method , url = url , data = data , content_type = content_type ,
301290 headers = headers , target_object = _target_object )
302291
303- if not 200 <= response .status < 300 :
304- raise make_exception (response , content ,
305- error_info = method + ' ' + url )
292+ if not 200 <= response .status_code < 300 :
293+ raise exceptions .from_http_response (response )
306294
307- string_or_bytes = (six .binary_type , six .text_type )
308- if content and expect_json and isinstance (content , string_or_bytes ):
309- content_type = response .get ('content-type' , '' )
310- if not content_type .startswith ('application/json' ):
311- raise TypeError ('Expected JSON, got %s' % content_type )
312- if isinstance (content , six .binary_type ):
313- content = content .decode ('utf-8' )
314- return json .loads (content )
315-
316- return content
295+ if expect_json and response .content :
296+ return response .json ()
297+ else :
298+ return response .content
0 commit comments