Skip to content

Commit 31431d1

Browse files
committed
support for ws response headers
1 parent 4388873 commit 31431d1

File tree

6 files changed

+82
-158
lines changed

6 files changed

+82
-158
lines changed

Examples/ServiceExamples/Scripts/ExampleCustomHeaders.cs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*
1616
*/
1717

18-
using FullSerializer;
1918
using IBM.Watson.DeveloperCloud.Connection;
2019
using IBM.Watson.DeveloperCloud.DataTypes;
2120
using IBM.Watson.DeveloperCloud.Logging;
2221
using IBM.Watson.DeveloperCloud.Services.Assistant.v1;
2322
using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1;
2423
using IBM.Watson.DeveloperCloud.Utilities;
25-
using System;
2624
using System.Collections;
2725
using System.Collections.Generic;
2826
using UnityEngine;
@@ -64,43 +62,43 @@ void Start()
6462
LogSystem.InstallDefaultReactors();
6563

6664

67-
//#region http custom headers
68-
//// Create credential and instantiate Assistant service
69-
//Credentials assistantCredentials = new Credentials(_assistantUsername, _assistantPassword, _assistantUrl);
70-
71-
//_assistant = new Assistant(assistantCredentials);
72-
//_assistant.VersionDate = _assistantVersionDate;
73-
74-
//Dictionary<string, object> input = new Dictionary<string, object>();
75-
//input.Add("text", _inputString);
76-
//MessageRequest messageRequest = new MessageRequest()
77-
//{
78-
// Input = input,
79-
// AlternateIntents = true
80-
//};
81-
82-
//// Create customData object
83-
//Dictionary<string, object> assistantCustomData = new Dictionary<string, object>();
84-
//// Create a dictionary of custom headers
85-
//Dictionary<string, string> assistantCustomHeaders = new Dictionary<string, string>();
86-
//// Add to the header dictionary
87-
//assistantCustomHeaders.Add("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
88-
//// Add the header dictionary to the custom data object
89-
//assistantCustomData.Add(Constants.String.CUSTOM_REQUEST_HEADERS, assistantCustomHeaders);
90-
91-
//// Logging what we will send
92-
//if (assistantCustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
93-
//{
94-
// Log.Debug("ExampleCustomHeader.Start()", "Assistant custom request headers:");
95-
// foreach (KeyValuePair<string, string> kvp in assistantCustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
96-
// {
97-
// Log.Debug("ExampleCustomHeader.Start()", "\t{0}: {1}", kvp.Key, kvp.Value);
98-
// }
99-
//}
100-
101-
//// Call service using custom data object
102-
//_assistant.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData: assistantCustomData);
103-
//#endregion
65+
#region http custom headers
66+
// Create credential and instantiate Assistant service
67+
Credentials assistantCredentials = new Credentials(_assistantUsername, _assistantPassword, _assistantUrl);
68+
69+
_assistant = new Assistant(assistantCredentials);
70+
_assistant.VersionDate = _assistantVersionDate;
71+
72+
Dictionary<string, object> input = new Dictionary<string, object>();
73+
input.Add("text", _inputString);
74+
MessageRequest messageRequest = new MessageRequest()
75+
{
76+
Input = input,
77+
AlternateIntents = true
78+
};
79+
80+
// Create customData object
81+
Dictionary<string, object> assistantCustomData = new Dictionary<string, object>();
82+
// Create a dictionary of custom headers
83+
Dictionary<string, string> assistantCustomHeaders = new Dictionary<string, string>();
84+
// Add to the header dictionary
85+
assistantCustomHeaders.Add("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
86+
// Add the header dictionary to the custom data object
87+
assistantCustomData.Add(Constants.String.CUSTOM_REQUEST_HEADERS, assistantCustomHeaders);
88+
89+
// Logging what we will send
90+
if (assistantCustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
91+
{
92+
Log.Debug("ExampleCustomHeader.Start()", "Assistant custom request headers:");
93+
foreach (KeyValuePair<string, string> kvp in assistantCustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
94+
{
95+
Log.Debug("ExampleCustomHeader.Start()", "\t{0}: {1}", kvp.Key, kvp.Value);
96+
}
97+
}
98+
99+
// Call service using custom data object
100+
_assistant.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData: assistantCustomData);
101+
#endregion
104102

105103
#region websocket custom headers
106104
// Websocket custom headers
@@ -128,8 +126,7 @@ void Start()
128126
}
129127
}
130128

131-
// Call service using custom data object
132-
//_speechToText.StartListening(OnRecognize, customData: speechToTextCustomData);
129+
// Call service using custom data object (see Active)
133130
Active = true;
134131

135132
StartRecording();

Plugins/websocket-sharp.dll

512 Bytes
Binary file not shown.

Plugins/websocket-sharp.dll.meta

Lines changed: 20 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Connection/WSConnector.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,22 @@ public class BinaryMessage : Message
9191
/// Constructor for a BinaryMessage object.
9292
/// </summary>
9393
/// <param name="data">The binary data to send as a message.</param>
94-
public BinaryMessage(byte[] data)
94+
/// <param name="headers">The response headers.</param>
95+
public BinaryMessage(byte[] data, Dictionary<string, string> headers = null)
9596
{
9697
Data = data;
98+
Headers = headers;
9799
}
98100

99101
#region Public Properties
100102
/// <summary>
101103
/// Binary payload.
102104
/// </summary>
103105
public byte[] Data { get; set; }
106+
/// <summary>
107+
/// The response headers
108+
/// </summary>
109+
public Dictionary<string, string> Headers { get; set; }
104110
#endregion
105111
};
106112
/// <summary>
@@ -112,16 +118,22 @@ public class TextMessage : Message
112118
/// Constructor for a TextMessage object.
113119
/// </summary>
114120
/// <param name="text">The string of the text to send as a message.</param>
115-
public TextMessage(string text)
121+
/// <param name="headers">The response headers.</param>
122+
public TextMessage(string text, Dictionary<string, string> headers = null)
116123
{
117124
Text = text;
125+
Headers = headers;
118126
}
119127

120128
#region Public Properties
121129
/// <summary>
122130
/// Text payload.
123131
/// </summary>
124132
public string Text { get; set; }
133+
/// <summary>
134+
/// The response headers
135+
/// </summary>
136+
public Dictionary<string, string> Headers { get; set; }
125137
#endregion
126138
};
127139
#endregion
@@ -367,9 +379,9 @@ private void OnWSMessage(object sender, MessageEventArgs e)
367379
{
368380
Message msg = null;
369381
if (e.IsText)
370-
msg = new TextMessage(e.Data);
382+
msg = new TextMessage(e.Data, e.Headers);
371383
else if (e.IsBinary)
372-
msg = new BinaryMessage(e.RawData);
384+
msg = new BinaryMessage(e.RawData, e.Headers);
373385

374386
lock (_receiveQueue)
375387
_receiveQueue.Enqueue(msg);

Scripts/Services/SpeechToText/v1/SpeechToText.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,10 @@ private void OnListenMessage(WSConnector.Message msg)
757757
{
758758
WSConnector.TextMessage tm = (WSConnector.TextMessage)msg;
759759
Dictionary<string, object> customData = new Dictionary<string, object>();
760-
customData.Add("json", tm.Text);
760+
customData.Add(Constants.String.JSON, tm.Text);
761+
if(tm.Headers != null && tm.Headers.Count > 0)
762+
customData.Add(Constants.String.RESPONSE_HEADERS, tm.Headers);
763+
761764
IDictionary json = Json.Deserialize(tm.Text) as IDictionary;
762765
if (json != null)
763766
{

Scripts/Utilities/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public static class String
5858
/// Name for accessing response headers in CustomData.
5959
/// </summary>
6060
public const string RESPONSE_HEADERS = "response_headers";
61+
/// <summary>
62+
/// Name for accessing json.
63+
/// </summary>
64+
public const string JSON = "json";
6165
}
6266

6367
/// <summary>

0 commit comments

Comments
 (0)