Skip to content

Commit da248fe

Browse files
Merge pull request #204 from watson-developer-cloud/iam-support
feat(UI): Use the server url in the client
2 parents 27854d4 + a991e7c commit da248fe

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

app.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,34 @@ require('./config/express')(app);
2727

2828
// Create the token manager
2929
let tokenManager;
30+
const serviceUrl = process.env.SPEECH_TO_TEXT_URL || 'https://stream.watsonplatform.net/speech-to-text/api';
3031

3132
if (process.env.SPEECH_TO_TEXT_IAM_APIKEY && process.env.SPEECH_TO_TEXT_IAM_APIKEY !== '') {
32-
tokenManager = new IamTokenManagerV1({
33+
tokenManager = new IamTokenManagerV1.IamTokenManagerV1({
3334
iamApikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY || '<iam_apikey>',
3435
iamUrl: process.env.SPEECH_TO_TEXT_IAM_URL || 'https://iam.bluemix.net/identity/token',
3536
});
3637
} else {
3738
const speechService = new SpeechToTextV1({
3839
username: process.env.SPEECH_TO_TEXT_USERNAME || '<username>',
3940
password: process.env.SPEECH_TO_TEXT_PASSWORD || '<password>',
40-
url: process.env.SPEECH_TO_TEXT_URL || '<url>',
41+
url: serviceUrl,
4142
});
4243
tokenManager = new AuthorizationV1(speechService.getCredentials());
4344
}
4445

4546
app.get('/', (req, res) => res.render('index'));
4647

47-
// Get token using your credentials
48-
app.get('/api/token', (req, res, next) => {
48+
// Get credentials using your credentials
49+
app.get('/api/credentials', (req, res, next) => {
4950
tokenManager.getToken((err, token) => {
5051
if (err) {
5152
next(err);
5253
} else {
53-
res.send(token);
54+
res.json({
55+
token,
56+
serviceUrl,
57+
});
5458
}
5559
});
5660
});

public/scripts/bundle.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './polyfills';
22
import React from 'react';
33
import ReactDOM from 'react-dom';
4-
import Demo from '../../views/demo.jsx'
4+
import Demo from '../../views/demo.jsx';
55

6-
ReactDOM.render(<Demo/>, document.getElementById('root'));
6+
ReactDOM.render(<Demo />, document.getElementById('root'));

test/unit/offline.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ describe('offline tests', () => {
1717
request(app).get('/foo/bar').expect(404, done);
1818
});
1919

20-
it('should fetch and return a token for GET /api/token', (done) => {
21-
const fakeToken = 'asdfasdfasdf';
20+
it('should fetch and return a token for GET /api/credentials', (done) => {
21+
const fakeToken = {
22+
token: 'faketoken',
23+
serviceUrl: 'https://stream.watsonplatform.net/speech-to-text/api',
24+
};
2225

2326
nock('https://stream.watsonplatform.net:443', { encodedQueryParams: true })
2427
.get('/authorization/api/v1/token')
2528
.query({ url: 'https://stream.watsonplatform.net/speech-to-text/api' })
26-
.reply(200, fakeToken, {
29+
.reply(200, 'faketoken', {
2730
connection: 'close',
2831
'transfer-encoding': 'chunked',
2932
'content-type': 'text/xml',
3033
'x-dp-watson-tran-id': 'stream-dp01-34302424',
3134
date: 'Tue, 29 Mar 2016 19:50:27 GMT',
3235
});
3336

34-
request(app).get('/api/token').expect(200, fakeToken, done);
37+
request(app).get('/api/credentials').expect(200, fakeToken, done);
3538
});
3639
});
3740
});

views/demo.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export default React.createClass({
9696
resultsBySpeaker: this.state.speakerLabels,
9797
// allow interim results through before the speaker has been determined
9898
speakerlessInterim: this.state.speakerLabels,
99+
url: this.state.serviceUrl,
99100
}, extra);
100101
},
101102

@@ -263,13 +264,13 @@ export default React.createClass({
263264
},
264265

265266
fetchToken() {
266-
return fetch('/api/token').then((res) => {
267+
return fetch('/api/credentials').then((res) => {
267268
if (res.status !== 200) {
268269
throw new Error('Error retrieving auth token');
269270
}
270-
return res.text();
271+
return res.json();
271272
}) // todo: throw here if non-200 status
272-
.then(token => this.setState({ token })).catch(this.handleError);
273+
.then(creds => this.setState({ ...creds })).catch(this.handleError);
273274
},
274275

275276
getKeywords(model) {
@@ -401,7 +402,7 @@ export default React.createClass({
401402

402403
const messages = this.getFinalAndLatestInterimResult();
403404
const micBullet = (typeof window !== 'undefined' && recognizeMicrophone.isSupported)
404-
? <li className="base--li">Use your microphone to record audio.</li>
405+
? <li className="base--li">Use your microphone to record audio. For best results, use broadband models for microphone input.</li>
405406
: <li className="base--li base--p_light">Use your microphone to record audio. (Not supported in current browser)</li>;// eslint-disable-line
406407

407408
return (
@@ -432,7 +433,7 @@ export default React.createClass({
432433

433434
<ul className="base--ul">
434435
{micBullet}
435-
<li className="base--li">'Upload pre-recorded audio (.mp3, .mpeg, .wav, .flac, or .opus only).</li>
436+
<li className="base--li">Upload pre-recorded audio (.mp3, .mpeg, .wav, .flac, or .opus only).</li>
436437
<li className="base--li">Play one of the sample audio files.*</li>
437438
</ul>
438439

@@ -535,7 +536,6 @@ export default React.createClass({
535536
<JSONView raw={rawMessages} formatted={formattedMessages} />
536537
</Pane>
537538
</Tabs>
538-
539539
</Dropzone>
540540
);
541541
},

0 commit comments

Comments
 (0)