Skip to content

Commit

Permalink
Adding GCCL header for HTTP APIs. (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes authored and lukesneeringer committed Feb 22, 2017
1 parent 0cb9e53 commit 08785e4
Show file tree
Hide file tree
Showing 38 changed files with 497 additions and 8 deletions.
3 changes: 3 additions & 0 deletions bigquery/google/cloud/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-bigquery').version

from google.cloud.bigquery._helpers import ArrayQueryParameter
from google.cloud.bigquery._helpers import ScalarQueryParameter
from google.cloud.bigquery._helpers import StructQueryParameter
Expand Down
9 changes: 9 additions & 0 deletions bigquery/google/cloud/bigquery/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

from google.cloud import _http

from google.cloud.bigquery import __version__


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


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

API_URL_TEMPLATE = '{api_base_url}/bigquery/{api_version}{path}'
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}
32 changes: 32 additions & 0 deletions bigquery/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class TestConnection(unittest.TestCase):

Expand Down Expand Up @@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self):
'/'.join(['', 'bigquery', conn.API_VERSION, 'foo']))
parms = dict(parse_qsl(qs))
self.assertEqual(parms['bar'], 'baz')

def test_extra_headers(self):
from google.cloud import _http as base_http
from google.cloud.bigquery import _http as MUT

http = mock.Mock(spec=['request'])
response = mock.Mock(status=200, spec=['status'])
data = b'brent-spiner'
http.request.return_value = response, data
client = mock.Mock(_http=http, spec=['_http'])

conn = self._make_one(client)
req_data = 'req-data-boring'
result = conn.api_request(
'GET', '/rainbow', data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
'Content-Length': str(len(req_data)),
'Accept-Encoding': 'gzip',
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
'User-Agent': conn.USER_AGENT,
}
expected_uri = conn.build_api_url('/rainbow')
http.request.assert_called_once_with(
body=req_data,
headers=expected_headers,
method='GET',
uri=expected_uri,
)
3 changes: 3 additions & 0 deletions datastore/google/cloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-datastore').version

from google.cloud.datastore.batch import Batch
from google.cloud.datastore.client import Client
from google.cloud.datastore.entity import Entity
Expand Down
7 changes: 3 additions & 4 deletions datastore/google/cloud/datastore/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""Connections to Google Cloud Datastore API servers."""

import os
from pkg_resources import get_distribution

from google.rpc import status_pb2

Expand All @@ -24,6 +23,8 @@
from google.cloud.environment_vars import GCD_HOST
from google.cloud import exceptions
from google.cloud.grpc.datastore.v1 import datastore_pb2 as _datastore_pb2

from google.cloud.datastore import __version__
try:
from google.cloud.datastore._gax import _DatastoreAPIOverGRPC
_HAVE_GRPC = True
Expand All @@ -37,9 +38,7 @@

_DISABLE_GRPC = os.getenv(DISABLE_GRPC, False)
_USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC
_DATASTORE_DIST = get_distribution('google-cloud-datastore')
_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(
_DATASTORE_DIST.version)
_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(__version__)


class _DatastoreAPIOverHttp(object):
Expand Down
3 changes: 3 additions & 0 deletions dns/google/cloud/dns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-dns').version

from google.cloud.dns.zone import Changes
from google.cloud.dns.client import Client
from google.cloud.dns.zone import ManagedZone
Expand Down
9 changes: 9 additions & 0 deletions dns/google/cloud/dns/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

from google.cloud import _http

from google.cloud.dns import __version__


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


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

API_URL_TEMPLATE = '{api_base_url}/dns/{api_version}{path}'
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}
32 changes: 32 additions & 0 deletions dns/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class TestConnection(unittest.TestCase):

Expand Down Expand Up @@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self):
'/'.join(['', 'dns', conn.API_VERSION, 'foo']))
parms = dict(parse_qsl(qs))
self.assertEqual(parms['bar'], 'baz')

def test_extra_headers(self):
from google.cloud import _http as base_http
from google.cloud.dns import _http as MUT

http = mock.Mock(spec=['request'])
response = mock.Mock(status=200, spec=['status'])
data = b'brent-spiner'
http.request.return_value = response, data
client = mock.Mock(_http=http, spec=['_http'])

conn = self._make_one(client)
req_data = 'req-data-boring'
result = conn.api_request(
'GET', '/rainbow', data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
'Content-Length': str(len(req_data)),
'Accept-Encoding': 'gzip',
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
'User-Agent': conn.USER_AGENT,
}
expected_uri = conn.build_api_url('/rainbow')
http.request.assert_called_once_with(
body=req_data,
headers=expected_headers,
method='GET',
uri=expected_uri,
)
3 changes: 3 additions & 0 deletions error_reporting/google/cloud/error_reporting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"""Client library for Stackdriver Error Reporting"""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-error-reporting').version

from google.cloud.error_reporting.client import Client
from google.cloud.error_reporting.client import HTTPContext
from google.cloud.error_reporting.util import build_flask_context
3 changes: 3 additions & 0 deletions language/google/cloud/language/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"""Client library for Google Cloud Natural Language API."""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-language').version

from google.cloud.language.client import Client
from google.cloud.language.document import Document
from google.cloud.language.document import Encoding
9 changes: 9 additions & 0 deletions language/google/cloud/language/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

from google.cloud import _http

from google.cloud.language import __version__


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


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

API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}'
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}
32 changes: 32 additions & 0 deletions language/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class TestConnection(unittest.TestCase):

Expand All @@ -36,3 +38,33 @@ def test_build_api_url(self):
method = 'annotateText'
uri += ':' + method
self.assertEqual(conn.build_api_url(method), uri)

def test_extra_headers(self):
from google.cloud import _http as base_http
from google.cloud.language import _http as MUT

http = mock.Mock(spec=['request'])
response = mock.Mock(status=200, spec=['status'])
data = b'brent-spiner'
http.request.return_value = response, data
client = mock.Mock(_http=http, spec=['_http'])

conn = self._make_one(client)
req_data = 'req-data-boring'
result = conn.api_request(
'GET', '/rainbow', data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
'Content-Length': str(len(req_data)),
'Accept-Encoding': 'gzip',
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
'User-Agent': conn.USER_AGENT,
}
expected_uri = conn.build_api_url('/rainbow')
http.request.assert_called_once_with(
body=req_data,
headers=expected_headers,
method='GET',
uri=expected_uri,
)
3 changes: 3 additions & 0 deletions logging/google/cloud/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"""Google Stackdriver Logging API wrapper."""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-logging').version

from google.cloud.logging.client import Client


Expand Down
9 changes: 9 additions & 0 deletions logging/google/cloud/logging/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

from google.cloud import _http
from google.cloud.iterator import HTTPIterator

from google.cloud.logging import __version__
from google.cloud.logging._helpers import entry_from_resource
from google.cloud.logging.sink import Sink
from google.cloud.logging.metric import Metric


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


class Connection(_http.JSONConnection):
"""A connection to Google Stackdriver Logging via the JSON REST API.
Expand All @@ -39,6 +44,10 @@ class Connection(_http.JSONConnection):
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}


class _LoggingAPI(object):
"""Helper mapping logging-related APIs.
Expand Down
30 changes: 30 additions & 0 deletions logging/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ def test_default_url(self):
conn = self._make_one(client)
self.assertIs(conn._client, client)

def test_extra_headers(self):
from google.cloud import _http as base_http
from google.cloud.logging import _http as MUT

http = mock.Mock(spec=['request'])
response = mock.Mock(status=200, spec=['status'])
data = b'brent-spiner'
http.request.return_value = response, data
client = mock.Mock(_http=http, spec=['_http'])

conn = self._make_one(client)
req_data = 'req-data-boring'
result = conn.api_request(
'GET', '/rainbow', data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
'Content-Length': str(len(req_data)),
'Accept-Encoding': 'gzip',
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
'User-Agent': conn.USER_AGENT,
}
expected_uri = conn.build_api_url('/rainbow')
http.request.assert_called_once_with(
body=req_data,
headers=expected_headers,
method='GET',
uri=expected_uri,
)


class Test_LoggingAPI(unittest.TestCase):

Expand Down
4 changes: 4 additions & 0 deletions monitoring/google/cloud/monitoring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

"""Google Stackdriver Monitoring API wrapper."""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-monitoring').version

from google.cloud.monitoring.client import Client
from google.cloud.monitoring.group import Group
from google.cloud.monitoring.label import LabelDescriptor
Expand Down
9 changes: 9 additions & 0 deletions monitoring/google/cloud/monitoring/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

from google.cloud import _http

from google.cloud.monitoring import __version__


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


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

API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
"""A template for the URL of a particular API call."""

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}
Loading

0 comments on commit 08785e4

Please sign in to comment.