Skip to content

Commit

Permalink
Merge c91cc78 into 373a929
Browse files Browse the repository at this point in the history
  • Loading branch information
qdii committed Feb 25, 2019
2 parents 373a929 + c91cc78 commit ea9b36c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion apitools/gen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import collections
import contextlib
import gzip
import json
import keyword
import logging
import os
import re
import tempfile

import six
from six.moves import urllib_parse
Expand Down Expand Up @@ -365,6 +367,19 @@ def _NormalizeDiscoveryUrls(discovery_url):
]


def _Gunzip(gzipped_content):
"""Returns gunzipped content from gzipped contents."""
try:
f = tempfile.NamedTemporaryFile(suffix='gz', mode='w+b', delete=False)
f.write(gzipped_content)
f.close() # force file synchronization
with gzip.open(f.name, 'rb') as h:
decompressed_content = h.read()
return decompressed_content
finally:
os.unlink(f.name)


def FetchDiscoveryDoc(discovery_url, retries=5):
"""Fetch the discovery document at the given url."""
discovery_urls = _NormalizeDiscoveryUrls(discovery_url)
Expand All @@ -373,7 +388,16 @@ def FetchDiscoveryDoc(discovery_url, retries=5):
for url in discovery_urls:
for _ in range(retries):
try:
content = urllib_request.urlopen(url).read()
response = urllib_request.urlopen(url)
encoding = response.info().get('Content-Encoding')
if encoding == 'gzip':
content = _Gunzip(response.read())
elif encoding == 'deflate':
content = response.read()
else:
logging.warning('Unknown encoding')
content = response.read()

if isinstance(content, bytes):
content = content.decode('utf8')
discovery_doc = json.loads(content)
Expand Down

0 comments on commit ea9b36c

Please sign in to comment.