Skip to content

Commit 5324510

Browse files
committed
Adding extra __environ__ for user agent.
Fixes #566.
1 parent 3ed4324 commit 5324510

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

gcloud/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,32 @@
1616

1717
from pkg_resources import get_distribution
1818

19+
import httplib2
20+
import socket
21+
22+
__environ__ = ''
23+
try:
24+
from google import appengine
25+
__environ__ = '-GAE' # pragma: NO COVER
26+
except ImportError:
27+
pass
28+
29+
1930
__version__ = get_distribution('gcloud').version
31+
32+
33+
if __environ__ == '':
34+
RESPONSE = None
35+
try:
36+
RESPONSE, _ = httplib2.Http(timeout=0.1).request(
37+
uri='http://169.254.169.254/computeMetadata/v1/project/project-id',
38+
headers={'Metadata-Flavor': 'Google'})
39+
40+
if RESPONSE['status'] == '200': # pragma: NO COVER
41+
__environ__ = '-GCE' # pragma: NO COVER
42+
except socket.timeout:
43+
pass
44+
finally:
45+
del RESPONSE
46+
else: # pragma: NO COVER
47+
pass # pragma: NO COVER

gcloud/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Connection(object):
3232
_EMPTY = object()
3333
"""A pointer to represent an empty value for default arguments."""
3434

35-
USER_AGENT = "gcloud-python/{0}".format(gcloud.__version__)
35+
USER_AGENT = "gcloud-python/{0}{1}".format(gcloud.__version__,
36+
gcloud.__environ__)
3637
"""The user agent for gcloud-python requests."""
3738

3839
def __init__(self, credentials=None):

0 commit comments

Comments
 (0)