Skip to content

Commit f169ecf

Browse files
committed
response headers, rename request headers to custom request headers
1 parent 2736cb9 commit f169ecf

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

Examples/ServiceExamples/Scripts/ExampleCustomHeaders.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,34 @@ void Start()
6868
// Add to the header dictionary
6969
customHeaders.Add("X-Watson-Metadata", "customer_id=some-customer-id");
7070
// Add the header dictionary to the custom data object
71-
customData.Add(Constants.String.CUSTOM_HEADERS, customHeaders);
71+
customData.Add(Constants.String.CUSTOM_REQUEST_HEADERS, customHeaders);
72+
73+
if (customData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
74+
{
75+
Log.Debug("ExampleCustomHeader.Start()", "Custom Request headers:");
76+
foreach (KeyValuePair<string, string> kvp in customData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
77+
{
78+
Log.Debug("ExampleCustomHeader.Start()", "\t{0}: {1}", kvp.Key, kvp.Value);
79+
}
80+
}
7281

7382
// Call service using custom data object
74-
_service.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData:customData);
83+
_service.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData: customData);
7584
}
7685

7786
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
7887
{
7988
Log.Debug("ExampleCustomHeader.OnMessage()", "Response: {0}", customData["json"].ToString());
89+
90+
if (customData.ContainsKey(Constants.String.RESPONSE_HEADERS))
91+
{
92+
Log.Debug("ExampleCustomHeader.OnMessage()", "Response headers:");
93+
94+
foreach (KeyValuePair<string, string> kvp in customData[Constants.String.RESPONSE_HEADERS] as Dictionary<string, string>)
95+
{
96+
Log.Debug("ExampleCustomHeader.OnMessage()", "\t{0}: {1}", kvp.Key, kvp.Value);
97+
}
98+
}
8099
}
81100

82101
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)

Scripts/Connection/RESTConnector.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public class Response
8484
/// The http response code from the server
8585
/// </summary>
8686
public long HttpResponseCode { get; set; }
87+
/// <summary>
88+
/// The response headers
89+
/// </summary>
90+
public Dictionary<string, string> Headers { get; set; }
8791
#endregion
8892
};
8993

@@ -524,6 +528,8 @@ private IEnumerator ProcessRequestQueue()
524528
resp.Error = error;
525529
}
526530

531+
resp.Headers = www.responseHeaders;
532+
527533
resp.ElapsedTime = (float)(DateTime.Now - startTime).TotalSeconds;
528534

529535
// if the response is over a threshold, then log with status instead of debug
@@ -563,6 +569,7 @@ private IEnumerator ProcessRequestQueue()
563569
resp.Error = deleteReq.Error;
564570
resp.HttpResponseCode = deleteReq.HttpResponseCode;
565571
resp.ElapsedTime = (float)(DateTime.Now - startTime).TotalSeconds;
572+
resp.Headers = deleteReq.ResponseHeaders;
566573
if (req.OnResponse != null)
567574
req.OnResponse(req, resp);
568575
}
@@ -624,6 +631,7 @@ private class DeleteRequest
624631
public long HttpResponseCode { get; set; }
625632
public byte[] Data { get; set; }
626633
public Error Error { get; set; }
634+
public Dictionary<string, string> ResponseHeaders { get; set; }
627635

628636
public IEnumerator Send(string url, Dictionary<string, string> headers)
629637
{
@@ -673,6 +681,7 @@ public IEnumerator Send(string url, Dictionary<string, string> headers)
673681

674682
Success = deleteReq.responseCode == HTTP_STATUS_OK || deleteReq.responseCode == HTTP_STATUS_OK || deleteReq.responseCode == HTTP_STATUS_NO_CONTENT;
675683
HttpResponseCode = deleteReq.responseCode;
684+
ResponseHeaders = deleteReq.GetResponseHeaders();
676685
Data = deleteReq.downloadHandler.data;
677686
Error = error;
678687
IsComplete = true;

Scripts/Services/Assistant/v1/Assistant.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ 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))
124+
if(req.CustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
125125
{
126-
foreach(KeyValuePair<string, string> kvp in req.CustomData[Constants.String.CUSTOM_HEADERS] as Dictionary<string, string>)
126+
foreach(KeyValuePair<string, string> kvp in req.CustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
127127
{
128128
req.Headers.Add(kvp.Key, kvp.Value);
129129
}
@@ -186,6 +186,8 @@ private void OnMessageResponse(RESTConnector.Request req, RESTConnector.Response
186186
}
187187
}
188188

189+
customData.Add(Constants.String.RESPONSE_HEADERS, resp.Headers);
190+
189191
if (resp.Success)
190192
{
191193
if (((MessageRequestObj)req).SuccessCallback != null)

Scripts/Utilities/Constants.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public static class String
5353
/// <summary>
5454
/// Name for creating custom headers in CustomData.
5555
/// </summary>
56-
public const string CUSTOM_HEADERS = "custom_header";
56+
public const string CUSTOM_REQUEST_HEADERS = "request_headers";
57+
/// <summary>
58+
/// Name for accessing response headers in CustomData.
59+
/// </summary>
60+
public const string RESPONSE_HEADERS = "response_headers";
5761
}
5862

5963
/// <summary>

0 commit comments

Comments
 (0)