@@ -319,11 +319,16 @@ def __init__(self, adwords_client, version=sorted(_SERVICE_MAP.keys())[-1],
319319 self ._end_point = self ._END_POINT_FORMAT % (server , version )
320320 self ._header_handler = _AdWordsHeaderHandler (adwords_client , version )
321321
322+ proxy_option = None
323+ if self ._adwords_client .https_proxy :
324+ proxy_option = {'https' : self ._adwords_client .https_proxy }
325+
322326 schema_url = self ._SCHEMA_FORMAT % (server , version )
323327 schema = suds .client .Client (
324328 schema_url ,
325329 doctor = suds .xsd .doctor .ImportDoctor (suds .xsd .doctor .Import (
326- self ._namespace , schema_url ))).wsdl .schema
330+ self ._namespace , schema_url )),
331+ proxy = proxy_option ).wsdl .schema
327332 self ._report_definition_type = schema .elements [
328333 (self ._REPORT_DEFINITION_NAME , self ._namespace )]
329334 self ._marshaller = suds .mx .literal .Literal (schema )
@@ -340,16 +345,25 @@ def DownloadReport(self, report_definition, output=sys.stdout,
340345 that will be downloaded.
341346 [optional]
342347 output: A writable object where the contents of the report will be written
343- to.
348+ to. If the report is gzip compressed, you need to specify an output
349+ that can write binary data.
344350 return_money_in_micros: A boolean indicating whether money should be
345351 represented as micros in reports. If None is supplied the AdWords
346352 server will use its default value, which is currently True.
347353
348354 Raises:
349- An AdWordsReportBadRequestError if the report download fails due to
350- improper input. If the request fails for any other reason (for example,
351- a network error), an AdWordsReportError will be raised instead.
355+ AdWordsReportBadRequestError: if the report download fails due to
356+ improper input.
357+ GoogleAdsValueError: if the user-specified report format is incompatible
358+ with the output.
359+ AdWordsReportError: if the request fails for any other reason; e.g. a
360+ network error.
352361 """
362+ if (report_definition ['downloadFormat' ].startswith ('GZIPPED_' )
363+ and getattr (output , 'mode' , 'w' ) != 'wb' ):
364+ raise googleads .errors .GoogleAdsValueError ('Need to specify a binary'
365+ ' output for GZIPPED formats.' )
366+
353367 self ._DownloadReport (self ._SerializeReportDefinition (report_definition ),
354368 output , return_money_in_micros )
355369
@@ -367,16 +381,25 @@ def DownloadReportWithAwql(self, query, file_format, output=sys.stdout,
367381 https://developers.google.com/adwords/api/docs/guides/reporting
368382 [optional]
369383 output: A writable object where the contents of the report will be written
370- to.
384+ to. If the report is gzip compressed, you need to specify an output
385+ that can write binary data.
371386 return_money_in_micros: A boolean indicating whether money should be
372387 represented as micros in reports. If None is supplied the AdWords
373388 server will use its default value, which is currently True.
374389
375390 Raises:
376- An AdWordsReportBadRequestError if the report download fails due to
377- improper input. If the request fails for any other reason (for example,
378- a network error), an AdWordsReportError will be raised instead.
391+ AdWordsReportBadRequestError: if the report download fails due to
392+ improper input.
393+ GoogleAdsValueError: if the user-specified report format is incompatible
394+ with the output.
395+ AdWordsReportError: if the request fails for any other reason; e.g. a
396+ network error.
379397 """
398+ if (file_format .startswith ('GZIPPED_' )
399+ and getattr (output , 'mode' , 'w' ) != 'wb' ):
400+ raise googleads .errors .GoogleAdsValueError ('Need to specify a binary'
401+ ' output for GZIPPED formats.' )
402+
380403 self ._DownloadReport (self ._SerializeAwql (query , file_format ), output ,
381404 return_money_in_micros )
382405
@@ -393,11 +416,12 @@ def _DownloadReport(self, post_body, output, return_money_in_micros):
393416 server will use its default value, which is currently True.
394417
395418 Raises:
396- An AdWordsReportBadRequestError if the report download fails due to
397- improper input. If the HTTP request fails with any other status code, an
398- AdWordsReportError will be raised instead. In the event of certain other
399- failures, a urllib2.URLError (Python 2) or urllib.error.URLError
400- (Python 3) will be raised.
419+ AdWordsReportBadRequestError: if the report download fails due to
420+ improper input. In the event of certain other failures, a
421+ urllib2.URLError (Python 2) or urllib.error.URLError (Python 3) will be
422+ raised.
423+ AdWordsReportError: if the request fails for any other reason; e.g. a
424+ network error.
401425 """
402426 if sys .version_info [0 ] == 3 :
403427 post_body = bytes (post_body , 'utf8' )
@@ -414,7 +438,8 @@ def _DownloadReport(self, post_body, output, return_money_in_micros):
414438 while True :
415439 chunk = response .read (_CHUNK_SIZE )
416440 if not chunk : break
417- output .write (chunk .decode () if sys .version_info [0 ] == 3 else chunk )
441+ output .write (chunk .decode () if sys .version_info [0 ] == 3
442+ and getattr (output , 'mode' , 'w' ) == 'w' else chunk )
418443
419444 def _SerializeAwql (self , query , file_format ):
420445 """Serializes an AWQL query and file format for transport.
0 commit comments