<commit_before># Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Connection to Google Cloud BigTable API servers."""

import httplib2


DATA_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.data'
READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.data.readonly'
ADMIN_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.admin'

DATA_API_BASE_URL = 'https://bigtable.googleapis.com'
TABLE_ADMIN_API_BASE_URL = 'https://bigtabletableadmin.googleapis.com'
CLUSTER_ADMIN_API_BASE_URL = 'https://bigtableclusteradmin.googleapis.com'


class Connection(object):
    """HTTP-RPC Connection base class for Google Cloud BigTable.

    If no value is passed in for ``http``, a :class:`httplib2.Http` object
    will be created and authorized with the ``credentials``. If not, the
    ``credentials`` and ``http`` need not be related.

    A custom (non-``httplib2``) HTTP object must have a ``request`` method
    which accepts the following arguments:

    * ``uri``
    * ``method``
    * ``body``
    * ``headers``

    In addition, ``redirections`` and ``connection_type`` may be used.

    Without the use of ``credentials.authorize(http)``, a custom ``http``
    object will also need to be able to add a bearer token to API
    requests and handle token refresh on 401 errors.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for this connection.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests.
    """

    USER_AGENT = 'gcloud-bigtable-python'

    def __init__(self, credentials=None, http=None):
        self._http = http
        self._credentials = credentials

    @property
    def credentials(self):
        """Getter for current credentials.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: The credentials object associated with this connection.
        """
        return self._credentials

    @property
    def http(self):
        """A getter for the HTTP transport used in talking to the API.

        :rtype: :class:`httplib2.Http`
        :returns: A Http object used to transport data.
        """
        if self._http is None:
            self._http = httplib2.Http()
            if self._credentials:
                self._http = self._credentials.authorize(self._http)
        return self._http

    @staticmethod
    def _create_scoped_credentials(credentials, scope):
        """Create a scoped set of credentials if it is required.

        :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                           :class:`NoneType`
        :param credentials: The OAuth2 Credentials to add a scope to.

        :type scope: list of URLs
        :param scope: the effective service auth scopes for the connection.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: A new credentials object that has a scope added (if needed).
        """
        if credentials and credentials.create_scoped_required():
            credentials = credentials.create_scoped(scope)
        return credentials


class DataConnection(Connection):
    """Connection to Google Cloud BigTable Data API.
<commit_msg>Adding basic protobuf _request method.<commit_after># Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Connection to Google Cloud BigTable API servers."""

import httplib2


DATA_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.data'
READ_ONLY_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.data.readonly'
ADMIN_SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.admin'

DATA_API_BASE_URL = 'https://bigtable.googleapis.com'
TABLE_ADMIN_API_BASE_URL = 'https://bigtabletableadmin.googleapis.com'
CLUSTER_ADMIN_API_BASE_URL = 'https://bigtableclusteradmin.googleapis.com'


class Connection(object):
    """HTTP-RPC Connection base class for Google Cloud BigTable.

    If no value is passed in for ``http``, a :class:`httplib2.Http` object
    will be created and authorized with the ``credentials``. If not, the
    ``credentials`` and ``http`` need not be related.

    A custom (non-``httplib2``) HTTP object must have a ``request`` method
    which accepts the following arguments:

    * ``uri``
    * ``method``
    * ``body``
    * ``headers``

    In addition, ``redirections`` and ``connection_type`` may be used.

    Without the use of ``credentials.authorize(http)``, a custom ``http``
    object will also need to be able to add a bearer token to API
    requests and handle token refresh on 401 errors.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for this connection.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests.
    """

    USER_AGENT = 'gcloud-bigtable-python'

    def __init__(self, credentials=None, http=None):
        self._http = http
        self._credentials = credentials

    @property
    def credentials(self):
        """Getter for current credentials.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: The credentials object associated with this connection.
        """
        return self._credentials

    @property
    def http(self):
        """A getter for the HTTP transport used in talking to the API.

        :rtype: :class:`httplib2.Http`
        :returns: A Http object used to transport data.
        """
        if self._http is None:
            self._http = httplib2.Http()
            if self._credentials:
                self._http = self._credentials.authorize(self._http)
        return self._http

    @staticmethod
    def _create_scoped_credentials(credentials, scope):
        """Create a scoped set of credentials if it is required.

        :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                           :class:`NoneType`
        :param credentials: The OAuth2 Credentials to add a scope to.

        :type scope: list of URLs
        :param scope: the effective service auth scopes for the connection.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: A new credentials object that has a scope added (if needed).
        """
        if credentials and credentials.create_scoped_required():
            credentials = credentials.create_scoped(scope)
        return credentials

    def _request(self, request_uri, data, request_method='POST'):
        """Make a request over the Http transport to the Cloud Datastore API.

        :type request_uri: string
        :param request_uri: The URI to send the request to.

        :type data: string
        :param data: The data to send with the API call.
                     Typically this is a serialized Protobuf string.

        :type request_method: string
        :param request_method: The HTTP method to send the request. Defaults to
                               'POST'.

        :rtype: string
        :returns: The string response content from the API call.
        :raises: :class:`ValueError` if the response code is not 200 OK.
        """
        headers = {
            'Content-Type': 'application/x-protobuf',
            'Content-Length': str(len(data)),
            'User-Agent': self.USER_AGENT,
        }
        headers, content = self.http.request(
            uri=request_uri, method=request_method,
            headers=headers, body=data)

        status = headers['status']
        if status != '200':
            raise ValueError(headers, content)

        return content

    def _rpc(self, request_uri, request_pb, response_pb_cls,
             request_method='POST'):
        """Make a protobuf RPC request.

        :type request_uri: string
        :param request_uri: The URI to send the request to.

        :type request_pb: :class:`google.protobuf.message.Message` instance
        :param request_pb: the protobuf instance representing the request.

        :type response_pb_cls: A :class:`google.protobuf.message.Message'
                               subclass.
        :param response_pb_cls: The class used to unmarshall the response
                                protobuf.

        :type request_method: string
        :param request_method: The HTTP method to send the request. Defaults to
                               'POST'.
        """
        response = self._request(request_uri=request_uri,
                                 data=request_pb.SerializeToString(),
                                 request_method=request_method)
        return response_pb_cls.FromString(response)


class DataConnection(Connection):
    """Connection to Google Cloud BigTable Data API.

{'fraction_non_alphanumeric': 0.09144717372003551, 'fraction_numerical': 0.005918910920390648, 'mean_word_length': 2.7299484915378955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7032311', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2666, 'n_words': 944}