From 42146945230146f7f96b57a2d988fe9a4a53c0b0 Mon Sep 17 00:00:00 2001 From: Erika Dsouza Date: Thu, 8 Mar 2018 15:52:23 -0500 Subject: [PATCH 1/2] fix(python): handling support for python 3.6.4 --- watson_developer_cloud/speech_to_text_v1.py | 12 ++++++++---- .../websocket/speech_to_text_websocket_listener.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/watson_developer_cloud/speech_to_text_v1.py b/watson_developer_cloud/speech_to_text_v1.py index 9c75da3fd..277b067a3 100644 --- a/watson_developer_cloud/speech_to_text_v1.py +++ b/watson_developer_cloud/speech_to_text_v1.py @@ -29,7 +29,10 @@ from .utils import deprecated from watson_developer_cloud.websocket import RecognizeCallback, RecognizeListener import base64 -import urllib +try: + from urllib.parse import urlencode +except ImportError: + from urllib import urlencode ############################################################################## # Service @@ -241,8 +244,9 @@ def recognize_with_websocket(self, headers = {} if self.default_headers is not None: headers = self.default_headers.copy() - base64_authorization = base64.b64encode( - self.username + ':' + self.password) + + authstring = "{0}:{1}".format(self.username, self.password) + base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8') headers['Authorization'] = 'Basic {0}'.format(base64_authorization) url = self.url.replace('https:', 'wss:') @@ -254,7 +258,7 @@ def recognize_with_websocket(self, 'version': version } params = _remove_null_values(params) - url = url + '/v1/recognize?{0}'.format(urllib.urlencode(params)) + url = url + '/v1/recognize?{0}'.format(urlencode(params)) options = { 'content_type': content_type, diff --git a/watson_developer_cloud/websocket/speech_to_text_websocket_listener.py b/watson_developer_cloud/websocket/speech_to_text_websocket_listener.py index c1b11adad..a4fa2bb59 100644 --- a/watson_developer_cloud/websocket/speech_to_text_websocket_listener.py +++ b/watson_developer_cloud/websocket/speech_to_text_websocket_listener.py @@ -62,7 +62,7 @@ def build_start_message(self, options): return options def build_close_message(self): - return json.dumps({'action': 'close'}) + return json.dumps({'action': 'close'}).encode('utf8') # helper method that sends a chunk of audio if needed (as required what the specified pacing is) def send_audio(self, data): From b09e8a3ac8baf1e5e9a574fc35b93b3d1a644456 Mon Sep 17 00:00:00 2001 From: Erika Dsouza Date: Thu, 8 Mar 2018 16:35:26 -0500 Subject: [PATCH 2/2] For travis build fail --- test/integration/test_discovery_v1.py | 9 ++++++--- test/integration/test_speech_to_text_v1.py | 4 ++-- test/integration/test_text_to_speech_v1.py | 3 ++- watson_developer_cloud/speech_to_text_v1.py | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/integration/test_discovery_v1.py b/test/integration/test_discovery_v1.py index fbd7c214a..a5435c14f 100644 --- a/test/integration/test_discovery_v1.py +++ b/test/integration/test_discovery_v1.py @@ -9,8 +9,8 @@ class Discoveryv1(TestCase): def setUp(self): self.discovery = watson_developer_cloud.DiscoveryV1( version='2017-10-16', - username=os.getenv('DISCOVERY_TO_TEXT_USERNAME'), - password=os.getenv('DISCOVERY_TO_TEXT_PASSWORD')) + username="YOUR SERVICE USERNAME", + password="YOUR SERVICE PASSWORD") self.discovery.set_default_headers({ 'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1' @@ -84,7 +84,10 @@ def test_documents(self): self.environment_id, self.collection_id, add_doc['document_id']) assert doc_status is not None - with open(os.path.join(os.path.dirname(__file__), '../../resources/simple.html'), 'r') as fileinfo: + with open( + os.path.join( + os.path.dirname(__file__), '../../resources/simple.html'), + 'r') as fileinfo: update_doc = self.discovery.update_document( self.environment_id, self.collection_id, diff --git a/test/integration/test_speech_to_text_v1.py b/test/integration/test_speech_to_text_v1.py index 5f7884115..6e3136c46 100644 --- a/test/integration/test_speech_to_text_v1.py +++ b/test/integration/test_speech_to_text_v1.py @@ -7,8 +7,8 @@ class TestSpeechToTextV1(TestCase): def setUp(self): self.speech_to_text = watson_developer_cloud.SpeechToTextV1( - username=os.getenv('SPEECH_TO_TEXT_USERNAME'), - password=os.getenv('SPEECH_TO_TEXT_PASSWORD')) + username=os.getenv('YOUR SERVICE USERNAME'), + password=os.getenv('YOUR SERVICE PASSWORD')) self.speech_to_text.set_default_headers({ 'X-Watson-Learning-Opt-Out': '1', diff --git a/test/integration/test_text_to_speech_v1.py b/test/integration/test_text_to_speech_v1.py index ba1fbdffc..c869fe781 100644 --- a/test/integration/test_text_to_speech_v1.py +++ b/test/integration/test_text_to_speech_v1.py @@ -5,7 +5,8 @@ class TestIntegrationTextToSpeechV1(unittest.TestCase): def setUp(self): - self.text_to_speech = watson_developer_cloud.TextToSpeechV1() + self.text_to_speech = watson_developer_cloud.TextToSpeechV1( + username="YOUR SERVICE USERNAME", password="YOUR SERVICE PASSWORD") self.text_to_speech.set_default_headers({ 'X-Watson-Learning-Opt-Out': '1', diff --git a/watson_developer_cloud/speech_to_text_v1.py b/watson_developer_cloud/speech_to_text_v1.py index 277b067a3..2f0d600e4 100644 --- a/watson_developer_cloud/speech_to_text_v1.py +++ b/watson_developer_cloud/speech_to_text_v1.py @@ -32,7 +32,7 @@ try: from urllib.parse import urlencode except ImportError: - from urllib import urlencode + from urllib import urlencode ############################################################################## # Service