Skip to content

Commit 2736cb9

Browse files
committed
pass custom headers in customData
1 parent 4374504 commit 2736cb9

File tree

5 files changed

+160
-4
lines changed

5 files changed

+160
-4
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using FullSerializer;
19+
using IBM.Watson.DeveloperCloud.Connection;
20+
using IBM.Watson.DeveloperCloud.Logging;
21+
using IBM.Watson.DeveloperCloud.Services.Assistant.v1;
22+
using IBM.Watson.DeveloperCloud.Utilities;
23+
using System.Collections;
24+
using System.Collections.Generic;
25+
using UnityEngine;
26+
27+
public class ExampleCustomHeaders : MonoBehaviour
28+
{
29+
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
30+
[SerializeField]
31+
private string _assistantUsername;
32+
[SerializeField]
33+
private string _assistantPassword;
34+
[SerializeField]
35+
private string _assistantUrl;
36+
[SerializeField]
37+
private string _assistantWorkspaceId;
38+
[SerializeField]
39+
private string _assistantVersionDate;
40+
#endregion
41+
42+
private Assistant _service;
43+
private fsSerializer _serializer = new fsSerializer();
44+
private string _inputString = "Turn on the winshield wipers";
45+
46+
void Start()
47+
{
48+
LogSystem.InstallDefaultReactors();
49+
50+
// Create credential and instantiate service
51+
Credentials credentials = new Credentials(_assistantUsername, _assistantPassword, _assistantUrl);
52+
53+
_service = new Assistant(credentials);
54+
_service.VersionDate = _assistantVersionDate;
55+
56+
Dictionary<string, object> input = new Dictionary<string, object>();
57+
input.Add("text", _inputString);
58+
MessageRequest messageRequest = new MessageRequest()
59+
{
60+
Input = input,
61+
AlternateIntents = true
62+
};
63+
64+
// Create custom data object
65+
Dictionary<string, object> customData = new Dictionary<string, object>();
66+
// Create a dictionary of custom headers
67+
Dictionary<string, string> customHeaders = new Dictionary<string, string>();
68+
// Add to the header dictionary
69+
customHeaders.Add("X-Watson-Metadata", "customer_id=some-customer-id");
70+
// Add the header dictionary to the custom data object
71+
customData.Add(Constants.String.CUSTOM_HEADERS, customHeaders);
72+
73+
// Call service using custom data object
74+
_service.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData:customData);
75+
}
76+
77+
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
78+
{
79+
Log.Debug("ExampleCustomHeader.OnMessage()", "Response: {0}", customData["json"].ToString());
80+
}
81+
82+
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
83+
{
84+
Log.Debug("ExampleCustomHeader.OnFail()", "Response: {0}", customData["json"].ToString());
85+
Log.Error("ExampleCustomHeader.OnFail()", "Error received: {0}", error.ToString());
86+
}
87+
}

Examples/ServiceExamples/Scripts/ExampleCustomHeaders.cs.meta

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

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
41+
m_IndirectSpecularColor: {r: 0.37311926, g: 0.38073996, b: 0.35872692, a: 1}
4242
--- !u!157 &3
4343
LightmapSettings:
4444
m_ObjectHideFlags: 0
@@ -289,6 +289,51 @@ Transform:
289289
m_Father: {fileID: 0}
290290
m_RootOrder: 11
291291
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
292+
--- !u!1 &832860416
293+
GameObject:
294+
m_ObjectHideFlags: 0
295+
m_PrefabParentObject: {fileID: 0}
296+
m_PrefabInternal: {fileID: 0}
297+
serializedVersion: 5
298+
m_Component:
299+
- component: {fileID: 832860418}
300+
- component: {fileID: 832860417}
301+
m_Layer: 0
302+
m_Name: ExampleCustomHeaders
303+
m_TagString: Untagged
304+
m_Icon: {fileID: 0}
305+
m_NavMeshLayer: 0
306+
m_StaticEditorFlags: 0
307+
m_IsActive: 1
308+
--- !u!114 &832860417
309+
MonoBehaviour:
310+
m_ObjectHideFlags: 0
311+
m_PrefabParentObject: {fileID: 0}
312+
m_PrefabInternal: {fileID: 0}
313+
m_GameObject: {fileID: 832860416}
314+
m_Enabled: 1
315+
m_EditorHideFlags: 0
316+
m_Script: {fileID: 11500000, guid: 17a0ff55debceaa4fa4d4215aef1dc8d, type: 3}
317+
m_Name:
318+
m_EditorClassIdentifier:
319+
_assistantUsername:
320+
_assistantPassword:
321+
_assistantUrl:
322+
_assistantWorkspaceId:
323+
_assistantVersionDate:
324+
--- !u!4 &832860418
325+
Transform:
326+
m_ObjectHideFlags: 0
327+
m_PrefabParentObject: {fileID: 0}
328+
m_PrefabInternal: {fileID: 0}
329+
m_GameObject: {fileID: 832860416}
330+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
331+
m_LocalPosition: {x: 0, y: 0, z: 0}
332+
m_LocalScale: {x: 1, y: 1, z: 1}
333+
m_Children: []
334+
m_Father: {fileID: 0}
335+
m_RootOrder: 13
336+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
292337
--- !u!1 &854511683
293338
GameObject:
294339
m_ObjectHideFlags: 0
@@ -567,7 +612,7 @@ GameObject:
567612
m_Icon: {fileID: 0}
568613
m_NavMeshLayer: 0
569614
m_StaticEditorFlags: 0
570-
m_IsActive: 1
615+
m_IsActive: 0
571616
--- !u!114 &1713392458
572617
MonoBehaviour:
573618
m_ObjectHideFlags: 0
@@ -680,11 +725,11 @@ MonoBehaviour:
680725
m_EditorClassIdentifier:
681726
_conversationUsername:
682727
_conversationPassword:
683-
_conversationUrl: https://gateway.watsonplatform.net/conversation/api
728+
_conversationUrl:
684729
_workspaceId:
685730
_discoveryUsername:
686731
_discoveryPassword:
687-
_discoveryUrl: https://gateway.watsonplatform.net/discovery/api
732+
_discoveryUrl:
688733
--- !u!1 &2118312579
689734
GameObject:
690735
m_ObjectHideFlags: 0

Scripts/Services/Assistant/v1/Assistant.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallba
121121
req.SuccessCallback = successCallback;
122122
req.FailCallback = failCallback;
123123
req.CustomData = customData == null ? new Dictionary<string, object>() : customData;
124+
if(req.CustomData.ContainsKey(Constants.String.CUSTOM_HEADERS))
125+
{
126+
foreach(KeyValuePair<string, string> kvp in req.CustomData[Constants.String.CUSTOM_HEADERS] as Dictionary<string, string>)
127+
{
128+
req.Headers.Add(kvp.Key, kvp.Value);
129+
}
130+
}
124131
req.Parameters["version"] = VersionDate;
125132
req.Headers["Content-Type"] = "application/json";
126133
fsData data = null;

Scripts/Utilities/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public static class String
5050
public const string Version = "watson-apis-unity-sdk/2.2.0";
5151
/// <exclude />
5252
public const string DebugDispalyQuality = "Quality: {0}";
53+
/// <summary>
54+
/// Name for creating custom headers in CustomData.
55+
/// </summary>
56+
public const string CUSTOM_HEADERS = "custom_header";
5357
}
5458

5559
/// <summary>

0 commit comments

Comments
 (0)