Skip to content

Commit 0ad4eac

Browse files
authored
Merge pull request #516 from watson-developer-cloud/develop
Release python 4.2.0
2 parents c753aa0 + e842f5a commit 0ad4eac

14 files changed

+1661
-274
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Watson services are migrating to token-based Identity and Access Management (IAM
8383
- In other instances, you authenticate by providing the **[username and password](#username-and-password)** for the service instance.
8484
- Visual Recognition uses a form of [API key](#api-key) only with instances created before May 23, 2018. Newer instances of Visual Recognition use IAM.
8585

86+
**Note:** Authenticating with the X-Watson-Authorization-Token header is deprecated. The token continues to work with Cloud Foundry services, but is not supported for services that use Identity and Access Management (IAM) authentication. See [here](#iam) for details.
87+
8688
### Getting credentials
8789
To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
8890

examples/language_translator_v3.py

-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,3 @@
5050
## Get model details
5151
# model = language_translator.get_model(model_id='fdadfc3b-0b96-4276-a6e5-f5c4a29711fc')
5252
# print(json.dumps(model, indent=2))
53-
54-
55-
56-

examples/tone_analyzer_v3.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from os.path import join, dirname
44
from watson_developer_cloud import ToneAnalyzerV3
5+
from watson_developer_cloud.tone_analyzer_v3 import ToneInput
56

67
tone_analyzer = ToneAnalyzerV3(
78
username='YOUR SERVICE USERNAME',
@@ -55,3 +56,11 @@
5556
print(tone)
5657
print(tone.get_headers())
5758
print(tone.get_result())
59+
tone_analyzer.set_detailed_response(False)
60+
61+
print("\ntone() example 7:\n")
62+
tone_input = ToneInput('I am very happy. It is a good day.')
63+
tone = tone_analyzer.tone(
64+
tone_input=tone_input,
65+
content_type="application/json")
66+
print(json.dumps(tone, indent=2))

test/integration/test_discovery_v1.py

+34
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,37 @@ def test_queries(self):
116116
filter='extracted_metadata.sha1::9181d244*',
117117
return_fields='extracted_metadata.sha1')
118118
assert query_results is not None
119+
120+
def test_credentials(self):
121+
credential_details = {
122+
'credential_type': 'username_password',
123+
'url': 'https://login.salesforce.com',
124+
'username': 'user@email.com',
125+
'password': 'xxx'
126+
}
127+
credentials = self.discovery.create_credentials(self.environment_id,
128+
'salesforce',
129+
credential_details)
130+
assert credentials['credential_id'] is not None
131+
credential_id = credentials['credential_id']
132+
133+
get_credentials = self.discovery.get_credentials(self.environment_id, credential_id)
134+
assert get_credentials['credential_id'] == credential_id
135+
136+
list_credentials = self.discovery.list_credentials(self.environment_id)
137+
assert list_credentials is not None
138+
139+
new_credential_details = {
140+
'credential_type': 'username_password',
141+
'url': 'https://logo.salesforce.com',
142+
'username': 'user@email.com',
143+
'password': 'xxx'
144+
}
145+
updated_credentials = self.discovery.update_credentials(self.environment_id, credential_id, 'salesforce', new_credential_details)
146+
assert updated_credentials is not None
147+
148+
get_credentials = self.discovery.get_credentials(self.environment_id, credentials['credential_id'])
149+
assert get_credentials['credential_details']['url'] == new_credential_details['url']
150+
151+
delete_credentials = self.discovery.delete_credentials(self.environment_id, credential_id)
152+
assert delete_credentials['credential_id'] is not None

test/unit/test_discovery_v1.py

+138-2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,45 @@ def test_collection():
214214
collection_id='collid')
215215
assert len(responses.calls) == 5
216216

217+
@responses.activate
218+
def test_federated_query():
219+
discovery_url = urljoin(base_discovery_url,
220+
'environments/envid/query')
221+
222+
responses.add(responses.GET, discovery_url,
223+
body="{\"body\": \"hello\"}", status=200,
224+
content_type='application/json')
225+
discovery = watson_developer_cloud.DiscoveryV1('2016-11-07',
226+
username='username',
227+
password='password')
228+
discovery.federated_query('envid', ['collid1', 'collid2'], filter='colls.sha1::9181d244*')
229+
230+
called_url = urlparse(responses.calls[0].request.url)
231+
test_url = urlparse(discovery_url)
232+
233+
assert called_url.netloc == test_url.netloc
234+
assert called_url.path == test_url.path
235+
assert len(responses.calls) == 1
236+
237+
@responses.activate
238+
def test_federated_query_notices():
239+
discovery_url = urljoin(base_discovery_url,
240+
'environments/envid/notices')
241+
242+
responses.add(responses.GET, discovery_url,
243+
body="{\"body\": \"hello\"}", status=200,
244+
content_type='application/json')
245+
discovery = watson_developer_cloud.DiscoveryV1('2016-11-07',
246+
username='username',
247+
password='password')
248+
discovery.federated_query_notices('envid', ['collid1', 'collid2'], filter='notices.sha1::9181d244*')
249+
250+
called_url = urlparse(responses.calls[0].request.url)
251+
test_url = urlparse(discovery_url)
252+
253+
assert called_url.netloc == test_url.netloc
254+
assert called_url.path == test_url.path
255+
assert len(responses.calls) == 1
217256

218257
@responses.activate
219258
def test_query():
@@ -226,7 +265,12 @@ def test_query():
226265
discovery = watson_developer_cloud.DiscoveryV1('2016-11-07',
227266
username='username',
228267
password='password')
229-
discovery.query('envid', 'collid', {'count': 10})
268+
discovery.query('envid', 'collid',
269+
filter='extracted_metadata.sha1::9181d244*',
270+
count=1,
271+
passages=True,
272+
passages_fields=['x', 'y'],
273+
passages_count=2)
230274

231275
called_url = urlparse(responses.calls[0].request.url)
232276
test_url = urlparse(discovery_url)
@@ -282,6 +326,29 @@ def test_query_entities():
282326
assert called_url.path == test_url.path
283327
assert len(responses.calls) == 1
284328

329+
@responses.activate
330+
def test_query_notices():
331+
discovery_url = urljoin(
332+
base_discovery_url,
333+
'environments/envid/collections/collid/notices')
334+
335+
responses.add(
336+
responses.GET,
337+
discovery_url,
338+
body="{\"body\": \"hello\"}",
339+
status=200,
340+
content_type='application/json')
341+
342+
discovery = watson_developer_cloud.DiscoveryV1(
343+
'2016-11-07', username='username', password='password')
344+
345+
discovery.query_notices('envid', 'collid', filter='notices.sha1::*')
346+
called_url = urlparse(responses.calls[0].request.url)
347+
test_url = urlparse(discovery_url)
348+
assert called_url.netloc == test_url.netloc
349+
assert called_url.path == test_url.path
350+
assert len(responses.calls) == 1
351+
285352
@responses.activate
286353
def test_configs():
287354
discovery_url = urljoin(base_discovery_url,
@@ -327,13 +394,20 @@ def test_configs():
327394

328395
discovery.create_configuration(environment_id='envid',
329396
name='my name')
397+
discovery.create_configuration(environment_id='envid',
398+
name='my name',
399+
source={'type': 'salesforce', 'credential_id': 'xxx'})
330400
discovery.update_configuration(environment_id='envid',
331401
configuration_id='confid',
332402
name='my new name')
403+
discovery.update_configuration(environment_id='envid',
404+
configuration_id='confid',
405+
name='my new name',
406+
source={'type': 'salesforce', 'credential_id': 'xxx'})
333407
discovery.delete_configuration(environment_id='envid',
334408
configuration_id='confid')
335409

336-
assert len(responses.calls) == 5
410+
assert len(responses.calls) == 7
337411

338412

339413
@responses.activate
@@ -815,3 +889,65 @@ def test_delete_user_data():
815889
response = discovery.delete_user_data('id')
816890
assert response is None
817891
assert len(responses.calls) == 1
892+
893+
@responses.activate
894+
def test_credentials():
895+
discovery_credentials_url = urljoin(base_discovery_url, 'environments/envid/credentials')
896+
897+
results = {'credential_id': 'e68305ce-29f3-48ea-b829-06653ca0fdef',
898+
'source_type': 'salesforce',
899+
'credential_details': {
900+
'url': 'https://login.salesforce.com',
901+
'credential_type': 'username_password',
902+
'username':'user@email.com'}
903+
}
904+
905+
iam_url = "https://iam.bluemix.net/identity/token"
906+
iam_token_response = """{
907+
"access_token": "oAeisG8yqPY7sFR_x66Z15",
908+
"token_type": "Bearer",
909+
"expires_in": 3600,
910+
"expiration": 1524167011,
911+
"refresh_token": "jy4gl91BQ"
912+
}"""
913+
responses.add(responses.POST, url=iam_url, body=iam_token_response, status=200)
914+
responses.add(responses.GET, "{0}/{1}?version=2016-11-07".format(discovery_credentials_url, 'credential_id'),
915+
body=json.dumps(results),
916+
status=200,
917+
content_type='application/json')
918+
responses.add(responses.GET, "{0}?version=2016-11-07".format(discovery_credentials_url),
919+
body=json.dumps([results]),
920+
status=200,
921+
content_type='application/json')
922+
923+
responses.add(responses.POST, "{0}?version=2016-11-07".format(discovery_credentials_url),
924+
body=json.dumps(results),
925+
status=200,
926+
content_type='application/json')
927+
results['source_type'] = 'ibm'
928+
responses.add(responses.PUT, "{0}/{1}?version=2016-11-07".format(discovery_credentials_url, 'credential_id'),
929+
body=json.dumps(results),
930+
status=200,
931+
content_type='application/json')
932+
responses.add(responses.DELETE, "{0}/{1}?version=2016-11-07".format(discovery_credentials_url, 'credential_id'),
933+
body=json.dumps({'deleted': 'bogus -- ok'}),
934+
status=200,
935+
content_type='application/json')
936+
937+
discovery = watson_developer_cloud.DiscoveryV1('2016-11-07',
938+
iam_api_key='iam_api_key')
939+
discovery.create_credentials('envid', 'salesforce', {
940+
'url': 'https://login.salesforce.com',
941+
'credential_type': 'username_password',
942+
'username':'user@email.com'
943+
})
944+
945+
discovery.get_credentials('envid', 'credential_id')
946+
947+
discovery.update_credentials(environment_id='envid',
948+
credential_id='credential_id',
949+
source_type='salesforce',
950+
credential_details=results['credential_details'])
951+
discovery.list_credentials('envid')
952+
discovery.delete_credentials(environment_id='envid', credential_id='credential_id')
953+
assert len(responses.calls) == 10

test/unit/test_watson_service.py

+16
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,19 @@ def test_iam():
131131
content_type='application/json')
132132
service.any_service_call()
133133
assert "grant_type=refresh_token" in responses.calls[0].request.body
134+
135+
@responses.activate
136+
def test_when_apikey_is_username():
137+
service1 = AnyServiceV1('2017-07-07', username='apikey', password='xxxxx')
138+
assert service1.token_manager is not None
139+
assert service1.iam_api_key is 'xxxxx'
140+
assert service1.username is None
141+
assert service1.password is None
142+
assert service1.token_manager.iam_url == 'https://iam.bluemix.net/identity/token'
143+
144+
service2 = AnyServiceV1('2017-07-07', username='apikey', password='xxxxx', iam_url='https://iam.stage1.bluemix.net/identity/token')
145+
assert service2.token_manager is not None
146+
assert service2.iam_api_key is 'xxxxx'
147+
assert service2.username is None
148+
assert service2.password is None
149+
assert service2.token_manager.iam_url == 'https://iam.stage1.bluemix.net/identity/token'

watson_developer_cloud/assistant_v1.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ def create_value(self,
14691469
- It must be no longer than 64 characters.
14701470
:param list[str] patterns: An array of patterns for the entity value. You can
14711471
provide either synonyms or patterns (as indicated by **type**), but not both. A
1472-
pattern is a regular expression no longer than 128 characters. For more
1472+
pattern is a regular expression no longer than 512 characters. For more
14731473
information about how to specify a pattern, see the
14741474
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
14751475
:param str value_type: Specifies the type of value.
@@ -1688,7 +1688,7 @@ def update_value(self,
16881688
- It must be no longer than 64 characters.
16891689
:param list[str] new_patterns: An array of patterns for the entity value. You can
16901690
provide either synonyms or patterns (as indicated by **type**), but not both. A
1691-
pattern is a regular expression no longer than 128 characters. For more
1691+
pattern is a regular expression no longer than 512 characters. For more
16921692
information about how to specify a pattern, see the
16931693
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
16941694
:param dict headers: A `dict` containing the request headers
@@ -3257,7 +3257,7 @@ class CreateValue(object):
32573257
- It must be no longer than 64 characters.
32583258
:attr list[str] patterns: (optional) An array of patterns for the entity value. You
32593259
can provide either synonyms or patterns (as indicated by **type**), but not both. A
3260-
pattern is a regular expression no longer than 128 characters. For more information
3260+
pattern is a regular expression no longer than 512 characters. For more information
32613261
about how to specify a pattern, see the
32623262
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
32633263
:attr str value_type: (optional) Specifies the type of value.
@@ -3286,7 +3286,7 @@ def __init__(self,
32863286
- It must be no longer than 64 characters.
32873287
:param list[str] patterns: (optional) An array of patterns for the entity value.
32883288
You can provide either synonyms or patterns (as indicated by **type**), but not
3289-
both. A pattern is a regular expression no longer than 128 characters. For more
3289+
both. A pattern is a regular expression no longer than 512 characters. For more
32903290
information about how to specify a pattern, see the
32913291
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
32923292
:param str value_type: (optional) Specifies the type of value.

watson_developer_cloud/conversation_v1.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ def create_value(self,
14691469
- It must be no longer than 64 characters.
14701470
:param list[str] patterns: An array of patterns for the entity value. You can
14711471
provide either synonyms or patterns (as indicated by **type**), but not both. A
1472-
pattern is a regular expression no longer than 128 characters. For more
1472+
pattern is a regular expression no longer than 512 characters. For more
14731473
information about how to specify a pattern, see the
14741474
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
14751475
:param str value_type: Specifies the type of value.
@@ -1688,7 +1688,7 @@ def update_value(self,
16881688
- It must be no longer than 64 characters.
16891689
:param list[str] new_patterns: An array of patterns for the entity value. You can
16901690
provide either synonyms or patterns (as indicated by **type**), but not both. A
1691-
pattern is a regular expression no longer than 128 characters. For more
1691+
pattern is a regular expression no longer than 512 characters. For more
16921692
information about how to specify a pattern, see the
16931693
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
16941694
:param dict headers: A `dict` containing the request headers
@@ -3257,7 +3257,7 @@ class CreateValue(object):
32573257
- It must be no longer than 64 characters.
32583258
:attr list[str] patterns: (optional) An array of patterns for the entity value. You
32593259
can provide either synonyms or patterns (as indicated by **type**), but not both. A
3260-
pattern is a regular expression no longer than 128 characters. For more information
3260+
pattern is a regular expression no longer than 512 characters. For more information
32613261
about how to specify a pattern, see the
32623262
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
32633263
:attr str value_type: (optional) Specifies the type of value.
@@ -3286,7 +3286,7 @@ def __init__(self,
32863286
- It must be no longer than 64 characters.
32873287
:param list[str] patterns: (optional) An array of patterns for the entity value.
32883288
You can provide either synonyms or patterns (as indicated by **type**), but not
3289-
both. A pattern is a regular expression no longer than 128 characters. For more
3289+
both. A pattern is a regular expression no longer than 512 characters. For more
32903290
information about how to specify a pattern, see the
32913291
[documentation](https://console.bluemix.net/docs/services/conversation/entities.html#creating-entities).
32923292
:param str value_type: (optional) Specifies the type of value.

0 commit comments

Comments
 (0)