Skip to content

Commit

Permalink
Adding ability to send version info header on HTTP requests.
Browse files Browse the repository at this point in the history
Added an "extra headers" feature to enable this. I am not a fan
of changing `Connection()` so haphazardly, but I hope to
completely re-factor / destory `Connection()` in the near-term
so I am less worried.

This only adds the storage and datastore header info, for the
purposes of a simple review. Once we agree on the approach,
I can add support in the other subpackages.
  • Loading branch information
dhermes committed Feb 17, 2017
1 parent 0a311f7 commit 0721ea7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion core/google/cloud/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import json
from pkg_resources import get_distribution
import platform

import six
from six.moves.urllib.parse import urlencode

Expand All @@ -29,6 +31,10 @@
get_distribution('google-cloud-core').version)
"""The user agent for google-cloud-python requests."""

CLIENT_INFO_HEADER = 'X-Goog-API-Client'
CLIENT_INFO_TEMPLATE = (
'gl-python/' + platform.python_version() + ' gccl/{}')


class Connection(object):
"""A generic connection to Google Cloud Platform.
Expand All @@ -38,6 +44,11 @@ class Connection(object):
"""

USER_AGENT = DEFAULT_USER_AGENT
_EXTRA_HEADERS = {}
"""Headers to be sent with every request.
Intended to be over-ridden by subclasses.
"""

def __init__(self, client):
self._client = client
Expand Down Expand Up @@ -147,7 +158,9 @@ def _make_request(self, method, url, data=None, content_type=None,
:param content_type: The proper MIME type of the data provided.
:type headers: dict
:param headers: A dictionary of HTTP headers to send with the request.
:param headers: (Optional) A dictionary of HTTP headers to send with
the request. If passed, will be modified directly
here with added headers.
:type target_object: object
:param target_object:
Expand All @@ -161,6 +174,7 @@ def _make_request(self, method, url, data=None, content_type=None,
returned by :meth:`_do_request`.
"""
headers = headers or {}
headers.update(self._EXTRA_HEADERS)
headers['Accept-Encoding'] = 'gzip'

if data:
Expand Down
4 changes: 4 additions & 0 deletions datastore/google/cloud/datastore/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import contextlib
import os
from pkg_resources import get_distribution

from google.rpc import status_pb2

Expand Down Expand Up @@ -61,6 +62,8 @@

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


class _DatastoreAPIOverHttp(object):
Expand Down Expand Up @@ -102,6 +105,7 @@ def _request(self, project, method, data):
'Content-Type': 'application/x-protobuf',
'Content-Length': str(len(data)),
'User-Agent': self.connection.USER_AGENT,
connection_module.CLIENT_INFO_HEADER: _CLIENT_INFO,
}
headers, content = self.connection.http.request(
uri=self.connection.build_api_url(project=project, method=method),
Expand Down
10 changes: 10 additions & 0 deletions storage/google/cloud/storage/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@

"""Create / interact with Google Cloud Storage connections."""

from pkg_resources import get_distribution

from google.cloud import _http


_STORAGE_DIST = get_distribution('google-cloud-storage')
_CLIENT_INFO = CLIENT_INFO_TEMPLATE.format(_STORAGE_DIST.version)


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

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

_EXTRA_HEADERS = {
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
}

0 comments on commit 0721ea7

Please sign in to comment.