diff --git a/docs/index.rst b/docs/index.rst index ec1ea380c7e8..fc81fbfdee35 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -154,6 +154,7 @@ :caption: Natural Language language-usage + Client .. toctree:: :maxdepth: 0 diff --git a/docs/language-client.rst b/docs/language-client.rst new file mode 100644 index 000000000000..00f3e813727d --- /dev/null +++ b/docs/language-client.rst @@ -0,0 +1,13 @@ +Natural Language Client +======================= + +.. automodule:: gcloud.language.client + :members: + :show-inheritance: + +Connection +~~~~~~~~~~ + +.. automodule:: gcloud.language.connection + :members: + :show-inheritance: diff --git a/gcloud/language/__init__.py b/gcloud/language/__init__.py new file mode 100644 index 000000000000..180c4993a5b2 --- /dev/null +++ b/gcloud/language/__init__.py @@ -0,0 +1,17 @@ +# Copyright 2016 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. + +"""Client library for Google Cloud Natural Language API.""" + +from gcloud.language.client import Client diff --git a/gcloud/language/client.py b/gcloud/language/client.py new file mode 100644 index 000000000000..08695e358fff --- /dev/null +++ b/gcloud/language/client.py @@ -0,0 +1,42 @@ +# Copyright 2016 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. + +"""Basic client for Google Cloud Natural Language API.""" + + +from gcloud.client import JSONClient +from gcloud.language.connection import Connection + + +class Client(JSONClient): + """Client to bundle configuration needed for API requests. + + :type project: str + :param project: the project which the client acts on behalf of. If not + passed, falls back to the default inferred from the + environment. + + :type credentials: :class:`~oauth2client.client.OAuth2Credentials` + :param credentials: (Optional) The OAuth2 Credentials to use for the + connection owned by this client. If not passed (and + if no ``http`` object is passed), falls back to the + default inferred from the environment. + + :type http: :class:`httplib2.Http` or class that defines ``request()``. + :param http: An optional HTTP object to make requests. If not passed, an + ``http`` object is created that is bound to the + ``credentials`` for the current object. + """ + + _connection_class = Connection diff --git a/gcloud/language/connection.py b/gcloud/language/connection.py new file mode 100644 index 000000000000..ac9ceaba6098 --- /dev/null +++ b/gcloud/language/connection.py @@ -0,0 +1,33 @@ +# Copyright 2016 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. + +"""Basic connection for Google Cloud Natural Language API.""" + +from gcloud import connection as base_connection + + +class Connection(base_connection.JSONConnection): + """A connection to Google Cloud Natural Language JSON REST API.""" + + API_BASE_URL = 'https://language.googleapis.com' + """The base of the API call URL.""" + + API_VERSION = 'v1beta1' + """The version of the API, used in building the API call's URL.""" + + API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}' + """A template for the URL of a particular API call.""" + + SCOPE = ('https://www.googleapis.com/auth/cloud-platform',) + """The scopes required for authenticating as an API consumer.""" diff --git a/gcloud/language/test_client.py b/gcloud/language/test_client.py new file mode 100644 index 000000000000..5b25a5d083ee --- /dev/null +++ b/gcloud/language/test_client.py @@ -0,0 +1,48 @@ +# Copyright 2016 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. + +import unittest + + +class TestClient(unittest.TestCase): + + def _getTargetClass(self): + from gcloud.language.client import Client + return Client + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_ctor(self): + from gcloud.language.connection import Connection + project = 'PROJECT' + creds = _Credentials() + http = object() + client = self._makeOne(project=project, credentials=creds, http=http) + self.assertTrue(isinstance(client.connection, Connection)) + self.assertTrue(client.connection.credentials is creds) + self.assertTrue(client.connection.http is http) + + +class _Credentials(object): + + _scopes = None + + @staticmethod + def create_scoped_required(): + return True + + def create_scoped(self, scope): + self._scopes = scope + return self diff --git a/gcloud/language/test_connection.py b/gcloud/language/test_connection.py new file mode 100644 index 000000000000..5ba49c2bd849 --- /dev/null +++ b/gcloud/language/test_connection.py @@ -0,0 +1,36 @@ +# Copyright 2016 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. + +import unittest + + +class TestConnection(unittest.TestCase): + + def _getTargetClass(self): + from gcloud.language.connection import Connection + return Connection + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_build_api_url(self): + conn = self._makeOne() + uri = '/'.join([ + conn.API_BASE_URL, + conn.API_VERSION, + 'documents', + ]) + method = 'annotateText' + uri += ':' + method + self.assertEqual(conn.build_api_url(method), uri) diff --git a/scripts/generate_json_docs.py b/scripts/generate_json_docs.py index 130c7eccee7a..ea96773c8838 100644 --- a/scripts/generate_json_docs.py +++ b/scripts/generate_json_docs.py @@ -623,13 +623,14 @@ def main(): 'error_reporting': [], 'exceptions': [], 'iterator': [], + 'language': [], 'logging': [], 'monitoring': [], 'pubsub': [], 'resource_manager': [], 'storage': [], 'streaming': [], - 'translate': [] + 'translate': [], } } diff --git a/scripts/verify_included_modules.py b/scripts/verify_included_modules.py index d351592ad9cf..84d6d1df1179 100644 --- a/scripts/verify_included_modules.py +++ b/scripts/verify_included_modules.py @@ -37,6 +37,7 @@ 'gcloud.dns.__init__', 'gcloud.error_reporting.__init__', 'gcloud.iterator', + 'gcloud.language.__init__', 'gcloud.logging.__init__', 'gcloud.logging.handlers.__init__', 'gcloud.logging.handlers.transports.__init__',