Skip to content

Commit eea3769

Browse files
dhermeslukesneeringer
authored andcommitted
Adding GCCL header for HTTP APIs. (googleapis#3046)
1 parent 1580efe commit eea3769

File tree

38 files changed

+497
-8
lines changed

38 files changed

+497
-8
lines changed

bigquery/google/cloud/bigquery/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"""
2424

2525

26+
from pkg_resources import get_distribution
27+
__version__ = get_distribution('google-cloud-bigquery').version
28+
2629
from google.cloud.bigquery._helpers import ArrayQueryParameter
2730
from google.cloud.bigquery._helpers import ScalarQueryParameter
2831
from google.cloud.bigquery._helpers import StructQueryParameter

bigquery/google/cloud/bigquery/_http.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
from google.cloud import _http
1818

19+
from google.cloud.bigquery import __version__
20+
21+
22+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23+
1924

2025
class Connection(_http.JSONConnection):
2126
"""A connection to Google BigQuery via the JSON REST API.
@@ -32,3 +37,7 @@ class Connection(_http.JSONConnection):
3237

3338
API_URL_TEMPLATE = '{api_base_url}/bigquery/{api_version}{path}'
3439
"""A template for the URL of a particular API call."""
40+
41+
_EXTRA_HEADERS = {
42+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
43+
}

bigquery/unit_tests/test__http.py

+32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import unittest
1616

17+
import mock
18+
1719

1820
class TestConnection(unittest.TestCase):
1921

@@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self):
4850
'/'.join(['', 'bigquery', conn.API_VERSION, 'foo']))
4951
parms = dict(parse_qsl(qs))
5052
self.assertEqual(parms['bar'], 'baz')
53+
54+
def test_extra_headers(self):
55+
from google.cloud import _http as base_http
56+
from google.cloud.bigquery import _http as MUT
57+
58+
http = mock.Mock(spec=['request'])
59+
response = mock.Mock(status=200, spec=['status'])
60+
data = b'brent-spiner'
61+
http.request.return_value = response, data
62+
client = mock.Mock(_http=http, spec=['_http'])
63+
64+
conn = self._make_one(client)
65+
req_data = 'req-data-boring'
66+
result = conn.api_request(
67+
'GET', '/rainbow', data=req_data, expect_json=False)
68+
self.assertEqual(result, data)
69+
70+
expected_headers = {
71+
'Content-Length': str(len(req_data)),
72+
'Accept-Encoding': 'gzip',
73+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
74+
'User-Agent': conn.USER_AGENT,
75+
}
76+
expected_uri = conn.build_api_url('/rainbow')
77+
http.request.assert_called_once_with(
78+
body=req_data,
79+
headers=expected_headers,
80+
method='GET',
81+
uri=expected_uri,
82+
)

datastore/google/cloud/datastore/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
"""
5555

5656

57+
from pkg_resources import get_distribution
58+
__version__ = get_distribution('google-cloud-datastore').version
59+
5760
from google.cloud.datastore.batch import Batch
5861
from google.cloud.datastore.client import Client
5962
from google.cloud.datastore.entity import Entity

datastore/google/cloud/datastore/_http.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Connections to Google Cloud Datastore API servers."""
1616

1717
import os
18-
from pkg_resources import get_distribution
1918

2019
from google.rpc import status_pb2
2120

@@ -24,6 +23,8 @@
2423
from google.cloud.environment_vars import GCD_HOST
2524
from google.cloud import exceptions
2625
from google.cloud.grpc.datastore.v1 import datastore_pb2 as _datastore_pb2
26+
27+
from google.cloud.datastore import __version__
2728
try:
2829
from google.cloud.datastore._gax import _DatastoreAPIOverGRPC
2930
_HAVE_GRPC = True
@@ -37,9 +38,7 @@
3738

3839
_DISABLE_GRPC = os.getenv(DISABLE_GRPC, False)
3940
_USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC
40-
_DATASTORE_DIST = get_distribution('google-cloud-datastore')
41-
_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(
42-
_DATASTORE_DIST.version)
41+
_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(__version__)
4342

4443

4544
class _DatastoreAPIOverHttp(object):

dns/google/cloud/dns/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"""
2626

2727

28+
from pkg_resources import get_distribution
29+
__version__ = get_distribution('google-cloud-dns').version
30+
2831
from google.cloud.dns.zone import Changes
2932
from google.cloud.dns.client import Client
3033
from google.cloud.dns.zone import ManagedZone

dns/google/cloud/dns/_http.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
from google.cloud import _http
1818

19+
from google.cloud.dns import __version__
20+
21+
22+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23+
1924

2025
class Connection(_http.JSONConnection):
2126
"""A connection to Google Cloud DNS via the JSON REST API.
@@ -32,3 +37,7 @@ class Connection(_http.JSONConnection):
3237

3338
API_URL_TEMPLATE = '{api_base_url}/dns/{api_version}{path}'
3439
"""A template for the URL of a particular API call."""
40+
41+
_EXTRA_HEADERS = {
42+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
43+
}

dns/unit_tests/test__http.py

+32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import unittest
1616

17+
import mock
18+
1719

1820
class TestConnection(unittest.TestCase):
1921

@@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self):
4850
'/'.join(['', 'dns', conn.API_VERSION, 'foo']))
4951
parms = dict(parse_qsl(qs))
5052
self.assertEqual(parms['bar'], 'baz')
53+
54+
def test_extra_headers(self):
55+
from google.cloud import _http as base_http
56+
from google.cloud.dns import _http as MUT
57+
58+
http = mock.Mock(spec=['request'])
59+
response = mock.Mock(status=200, spec=['status'])
60+
data = b'brent-spiner'
61+
http.request.return_value = response, data
62+
client = mock.Mock(_http=http, spec=['_http'])
63+
64+
conn = self._make_one(client)
65+
req_data = 'req-data-boring'
66+
result = conn.api_request(
67+
'GET', '/rainbow', data=req_data, expect_json=False)
68+
self.assertEqual(result, data)
69+
70+
expected_headers = {
71+
'Content-Length': str(len(req_data)),
72+
'Accept-Encoding': 'gzip',
73+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
74+
'User-Agent': conn.USER_AGENT,
75+
}
76+
expected_uri = conn.build_api_url('/rainbow')
77+
http.request.assert_called_once_with(
78+
body=req_data,
79+
headers=expected_headers,
80+
method='GET',
81+
uri=expected_uri,
82+
)

error_reporting/google/cloud/error_reporting/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""Client library for Stackdriver Error Reporting"""
1616

1717

18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-error-reporting').version
20+
1821
from google.cloud.error_reporting.client import Client
1922
from google.cloud.error_reporting.client import HTTPContext
2023
from google.cloud.error_reporting.util import build_flask_context

language/google/cloud/language/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""Client library for Google Cloud Natural Language API."""
1616

1717

18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-language').version
20+
1821
from google.cloud.language.client import Client
1922
from google.cloud.language.document import Document
2023
from google.cloud.language.document import Encoding

language/google/cloud/language/_http.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
from google.cloud import _http
1818

19+
from google.cloud.language import __version__
20+
21+
22+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23+
1924

2025
class Connection(_http.JSONConnection):
2126
"""A connection to Google Cloud Natural Language JSON REST API.
@@ -32,3 +37,7 @@ class Connection(_http.JSONConnection):
3237

3338
API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}'
3439
"""A template for the URL of a particular API call."""
40+
41+
_EXTRA_HEADERS = {
42+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
43+
}

language/unit_tests/test__http.py

+32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import unittest
1616

17+
import mock
18+
1719

1820
class TestConnection(unittest.TestCase):
1921

@@ -36,3 +38,33 @@ def test_build_api_url(self):
3638
method = 'annotateText'
3739
uri += ':' + method
3840
self.assertEqual(conn.build_api_url(method), uri)
41+
42+
def test_extra_headers(self):
43+
from google.cloud import _http as base_http
44+
from google.cloud.language import _http as MUT
45+
46+
http = mock.Mock(spec=['request'])
47+
response = mock.Mock(status=200, spec=['status'])
48+
data = b'brent-spiner'
49+
http.request.return_value = response, data
50+
client = mock.Mock(_http=http, spec=['_http'])
51+
52+
conn = self._make_one(client)
53+
req_data = 'req-data-boring'
54+
result = conn.api_request(
55+
'GET', '/rainbow', data=req_data, expect_json=False)
56+
self.assertEqual(result, data)
57+
58+
expected_headers = {
59+
'Content-Length': str(len(req_data)),
60+
'Accept-Encoding': 'gzip',
61+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
62+
'User-Agent': conn.USER_AGENT,
63+
}
64+
expected_uri = conn.build_api_url('/rainbow')
65+
http.request.assert_called_once_with(
66+
body=req_data,
67+
headers=expected_headers,
68+
method='GET',
69+
uri=expected_uri,
70+
)

logging/google/cloud/logging/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""Google Stackdriver Logging API wrapper."""
1616

1717

18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-logging').version
20+
1821
from google.cloud.logging.client import Client
1922

2023

logging/google/cloud/logging/_http.py

+9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
from google.cloud import _http
2020
from google.cloud.iterator import HTTPIterator
21+
22+
from google.cloud.logging import __version__
2123
from google.cloud.logging._helpers import entry_from_resource
2224
from google.cloud.logging.sink import Sink
2325
from google.cloud.logging.metric import Metric
2426

2527

28+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
29+
30+
2631
class Connection(_http.JSONConnection):
2732
"""A connection to Google Stackdriver Logging via the JSON REST API.
2833
@@ -39,6 +44,10 @@ class Connection(_http.JSONConnection):
3944
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
4045
"""A template for the URL of a particular API call."""
4146

47+
_EXTRA_HEADERS = {
48+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
49+
}
50+
4251

4352
class _LoggingAPI(object):
4453
"""Helper mapping logging-related APIs.

logging/unit_tests/test__http.py

+30
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ def test_default_url(self):
4242
conn = self._make_one(client)
4343
self.assertIs(conn._client, client)
4444

45+
def test_extra_headers(self):
46+
from google.cloud import _http as base_http
47+
from google.cloud.logging import _http as MUT
48+
49+
http = mock.Mock(spec=['request'])
50+
response = mock.Mock(status=200, spec=['status'])
51+
data = b'brent-spiner'
52+
http.request.return_value = response, data
53+
client = mock.Mock(_http=http, spec=['_http'])
54+
55+
conn = self._make_one(client)
56+
req_data = 'req-data-boring'
57+
result = conn.api_request(
58+
'GET', '/rainbow', data=req_data, expect_json=False)
59+
self.assertEqual(result, data)
60+
61+
expected_headers = {
62+
'Content-Length': str(len(req_data)),
63+
'Accept-Encoding': 'gzip',
64+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
65+
'User-Agent': conn.USER_AGENT,
66+
}
67+
expected_uri = conn.build_api_url('/rainbow')
68+
http.request.assert_called_once_with(
69+
body=req_data,
70+
headers=expected_headers,
71+
method='GET',
72+
uri=expected_uri,
73+
)
74+
4575

4676
class Test_LoggingAPI(unittest.TestCase):
4777

monitoring/google/cloud/monitoring/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
"""Google Stackdriver Monitoring API wrapper."""
1616

17+
18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-monitoring').version
20+
1721
from google.cloud.monitoring.client import Client
1822
from google.cloud.monitoring.group import Group
1923
from google.cloud.monitoring.label import LabelDescriptor

monitoring/google/cloud/monitoring/_http.py

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
from google.cloud import _http
1818

19+
from google.cloud.monitoring import __version__
20+
21+
22+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23+
1924

2025
class Connection(_http.JSONConnection):
2126
"""A connection to Google Stackdriver Monitoring via the JSON REST API.
@@ -32,3 +37,7 @@ class Connection(_http.JSONConnection):
3237

3338
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
3439
"""A template for the URL of a particular API call."""
40+
41+
_EXTRA_HEADERS = {
42+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
43+
}

0 commit comments

Comments
 (0)