Skip to content

Commit 039ae5e

Browse files
committed
Adding full test coverage.
Also fixing a few broken docs.
1 parent 6e566c6 commit 039ae5e

6 files changed

Lines changed: 396 additions & 47 deletions

File tree

gcloud/connection.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ def build_api_url(cls, path, query_params=None,
162162
:type path: string
163163
:param path: The path to the resource (ie, ``'/b/bucket-name'``).
164164
165-
:type query_params: dict
166-
:param query_params: A dictionary of keys and values to insert into
167-
the query string of the URL.
165+
:type query_params: dict or list
166+
:param query_params: A dictionary of keys and values (or list of
167+
key-value pairs) to insert into the query
168+
string of the URL.
168169
169170
:type api_base_url: string
170171
:param api_base_url: The base URL for the API endpoint.
@@ -286,10 +287,10 @@ def api_request(self, method, path, query_params=None,
286287
:param path: The path to the resource (ie, ``'/b/bucket-name'``).
287288
Required.
288289
289-
:type query_params: dict
290-
:param query_params: A dictionary of keys and values to insert into
291-
the query string of the URL. Default is
292-
empty dict.
290+
:type query_params: dict or list
291+
:param query_params: A dictionary of keys and values (or list of
292+
key-value pairs) to insert into the query
293+
string of the URL.
293294
294295
:type data: string
295296
:param data: The data to send as the body of the request. Default is

gcloud/pubsub/connection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ def build_api_url(self, path, query_params=None,
6767
:type path: string
6868
:param path: The path to the resource.
6969
70-
:type query_params: dict
71-
:param query_params: A dictionary of keys and values to insert into
72-
the query string of the URL.
70+
:type query_params: dict or list
71+
:param query_params: A dictionary of keys and values (or list of
72+
key-value pairs) to insert into the query
73+
string of the URL.
7374
7475
:type api_base_url: string
7576
:param api_base_url: The base URL for the API endpoint.

gcloud/translate/client.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _zip_assert(values1, values2):
5050
class Client(object):
5151
"""Client to bundle configuration needed for API requests.
5252
53-
:type key: string
53+
:type key: str
5454
:param key: The key used to send with requests as a query
5555
parameter.
5656
@@ -91,19 +91,18 @@ def get_languages(self, target_language=None):
9191
method='GET', path='/languages', query_params=query_params)
9292
return response.get('data', {}).get('languages', ())
9393

94-
def detect_language(self, values):
94+
def detect_language(self, *values):
9595
"""Detect the language of a string or list of strings.
9696
9797
See: https://cloud.google.com/translate/v2/\
9898
detecting-language-with-rest
9999
100-
:type values: str or list
101-
:param values: String or list of strings that will have
102-
language detected.
100+
:type values: tuple
101+
:param values: Tuple of strings that will have language detected.
103102
104103
:rtype: list
105104
:returns: A list of dictionaries for each queried value. Each
106-
dictionary typically contains four keys (though not
105+
dictionary typically contains three keys (though not
107106
all will be present in all cases)
108107
109108
* ``confidence``: The confidence in language detection, a
@@ -118,8 +117,6 @@ def detect_language(self, values):
118117
:raises: :class:`ValueError <exceptions.ValueError>` if the number of
119118
detections is not equal to the number of values.
120119
"""
121-
if isinstance(values, six.string_types):
122-
values = [values]
123120
query_params = [('key', self.key)]
124121
query_params.extend(('q', _to_bytes(value, 'utf-8'))
125122
for value in values)
@@ -145,49 +142,53 @@ def detect_language(self, values):
145142

146143
return detections
147144

148-
def translate(self, values, target_language=ENGLISH_ISO_639,
149-
format_=None, source_language=None,
150-
customization_ids=()):
145+
def translate(self, *values, **kwargs):
151146
"""Translate a string or list of strings.
152147
153148
See: https://cloud.google.com/translate/v2/\
154149
translating-text-with-rest
155150
156-
:type values: str or list
157-
:param values: String or list of strings that will have
158-
language detected.
151+
Accepted keyword arguments are:
159152
160-
:type target_language: str
161-
:param target_language: The language to translate results into. This
162-
is required by the API and defaults to
163-
:data:`ENGLISH_ISO_639`.
164-
165-
:type format_: str
166-
:param format_: (Optional) One of ``text`` or ``html``, to specify
167-
if the input text is plain text or HTML.
168-
169-
:type source_language: str
170-
:param source_language: (Optional) The language of the text to
171-
be translated.
172-
173-
:type customization_ids: list
174-
:param customization_ids: (Optional) List of customization IDs for
175-
translation. Sets the ``cid`` parameter
176-
in the query.
153+
* ``target_language`` (str): The language to translate results into.
154+
This is required by the API and defaults to :data:`ENGLISH_ISO_639`.
155+
* ``format`` (str): (Optional) One of ``text`` or ``html``, to specify
156+
if the input text is plain text or HTML.
157+
* ``source_language`` (str): (Optional) The language of the text to
158+
be translated.
159+
* ``customization_ids`` (list): (Optional) List of customization IDs
160+
for translation. Sets the ``cid`` parameter in the query.
161+
162+
:type values: tuple
163+
:param values: Tuple of strings to translate.
164+
165+
:type kwargs: dict
166+
:param kwargs: Keyword arguments to be passed in.
167+
168+
:rtype: list
169+
:returns: A list of dictionaries for each queried value. Each
170+
dictionary typically contains three keys (though not
171+
all will be present in all cases)
172+
173+
* ``detectedSourceLanguage``: The detected language (as an
174+
ISO 639-1 language code) of the text.
175+
* ``translatedText``: The translation of the text into the
176+
target language.
177+
* ``input``: The corresponding input value.
177178
"""
178-
if isinstance(values, six.string_types):
179-
values = [values]
179+
target_language = kwargs.get('target_language', ENGLISH_ISO_639)
180+
customization_ids = kwargs.get('customization_ids', ())
180181
if isinstance(customization_ids, six.string_types):
181182
customization_ids = [customization_ids]
182183

183184
query_params = [('key', self.key), ('target', target_language)]
184185
query_params.extend(('q', _to_bytes(value, 'utf-8'))
185186
for value in values)
186187
query_params.extend(('cid', cid) for cid in customization_ids)
187-
if format_ is not None:
188-
query_params.append(('format', format_))
189-
if source_language is not None:
190-
query_params.append(('source', source_language))
188+
if 'format' in kwargs:
189+
query_params.append(('format', kwargs['format']))
190+
if 'source_language' in kwargs:
191+
query_params.append(('source', kwargs['source_language']))
191192

192193
response = self.connection.api_request(
193194
method='GET', path='', query_params=query_params)

0 commit comments

Comments
 (0)