Skip to content

Commit e8410b1

Browse files
committed
removed methods dor sessions and cookie utility methods
1 parent 57c26ba commit e8410b1

File tree

5 files changed

+1
-312
lines changed

5 files changed

+1
-312
lines changed

Examples/ServiceExamples/Scripts/ExampleSpeechToText.cs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using UnityEngine;
1919
using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1;
2020
using IBM.Watson.DeveloperCloud.Logging;
21+
#pragma warning disable 0414
2122

2223
public class ExampleSpeechToText : MonoBehaviour
2324
{
@@ -26,7 +27,6 @@ public class ExampleSpeechToText : MonoBehaviour
2627
private SpeechToText m_SpeechToText = new SpeechToText();
2728

2829
private string m_CreatedSessionID;
29-
private string m_CreatedSessionCookie;
3030

3131
private string m_CreatedCustomizationID;
3232

@@ -60,18 +60,6 @@ private void TestGetModel(string modelID)
6060
m_SpeechToText.GetModel(HandleGetModel, modelID);
6161
}
6262

63-
private void TestCreateSession(string model)
64-
{
65-
Log.Debug("ExampleSpeechToText", "Attempting to create session with model {0}", model);
66-
m_SpeechToText.CreateSession(HandleCreateSession, model);
67-
}
68-
69-
private void TestDeleteSession(string sessionID, string session_cookie)
70-
{
71-
Log.Debug("ExampleSpeechToText", "Attempting to delete session session with model {0} with cookie {1}", sessionID, session_cookie);
72-
m_SpeechToText.DeleteSession(HandleDeleteSession, sessionID, session_cookie);
73-
}
74-
7563
private void TestGetCustomizations()
7664
{
7765
Log.Debug("ExampleSpeechToText", "Attempting to get customizations");
@@ -169,46 +157,6 @@ private void HandleOnRecognize (SpeechRecognitionEvent result)
169157
}
170158
}
171159

172-
private void HandleCreateSession(Session session, string customData)
173-
{
174-
if (!string.IsNullOrEmpty(customData))
175-
Log.Debug("ExampleSpeechToText", "custom data: {0}", customData);
176-
177-
if(session != null)
178-
{
179-
Log.Debug("ExampleSpeechToText", "Session - sessionID: {0} | new_session_url: {1} | observeResult: {2} | recognize: {3} | recognizeWS: {4}",
180-
session.session_id, session.new_session_uri, session.observe_result, session.recognize, session.recognizeWS);
181-
182-
if (!string.IsNullOrEmpty(session.session_id))
183-
{
184-
m_CreatedSessionID = session.session_id;
185-
m_CreatedSessionCookie = session.session_cookie;
186-
187-
// test DeleteSesion
188-
TestDeleteSession(m_CreatedSessionID, m_CreatedSessionCookie);
189-
}
190-
else
191-
Log.Warning("ExampleSpeechToText", "session_id is null!");
192-
}
193-
else
194-
{
195-
Log.Debug("ExampleSpeechToText", "Failed to create session!");
196-
}
197-
}
198-
199-
private void HandleDeleteSession(bool success, string customData)
200-
{
201-
if (success)
202-
{
203-
Log.Debug("ExampleSpeechToText", "Deleted session {0}!", m_CreatedSessionID);
204-
m_CreatedSessionID = default(string);
205-
}
206-
else
207-
{
208-
Log.Debug("ExampleSpeechToText", "Failed to delete session!");
209-
}
210-
}
211-
212160
private void HandleGetCustomizations(Customizations customizations, string customData)
213161
{
214162
if (!string.IsNullOrEmpty(customData))

Scripts/Connection/RESTConnector.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ public class Response
7575
/// The amount of time in seconds it took to get this response from the server.
7676
/// </summary>
7777
public float ElapsedTime { get; set; }
78-
/// <summary>
79-
/// The response headers.
80-
/// </summary>
81-
public Dictionary<string, string> ResponseHeaders { get; set; }
8278
#endregion
8379
};
8480

@@ -483,7 +479,6 @@ private IEnumerator ProcessRequestQueue()
483479
{
484480
resp.Success = true;
485481
resp.Data = www.bytes;
486-
resp.ResponseHeaders = Utility.ParseCookies(www);
487482
}
488483
else
489484
{

Scripts/Services/SpeechToText/DataModels.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ public class Session
109109
/// URI for WebSocket recognition requests. Needed only for working with the WebSocket interface.
110110
/// </summary>
111111
public string recognizeWS { get; set; }
112-
/// <summary>
113-
/// The cookie for this session.
114-
/// </summary>
115-
public string session_cookie { get; set; }
116112
}
117113

118114
/// <summary>

Scripts/Services/SpeechToText/SpeechToText.cs

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -318,139 +318,6 @@ private void OnGetModelResponse(RESTConnector.Request req, RESTConnector.Respons
318318
}
319319
#endregion
320320

321-
#region Create Session
322-
/// <summary>
323-
/// This callback object is used by the CreateSession() method.
324-
/// </summary>
325-
/// <param name="session">The session data.</param>
326-
/// <param name="customData">Optional custom data.</param>
327-
public delegate void OnCreateSession(Session session, string customData);
328-
329-
/// <summary>
330-
/// This function creates a speech to text session.
331-
/// </summary>
332-
/// <param name="callback">This callback is invoked with the sesion data. The callback will be invoked with null on error.</param>
333-
/// <param name="modelName">The model name to create the session using.</param>
334-
/// <param name="customData">Optional custom data.vc </param>
335-
/// <returns></returns>
336-
public bool CreateSession(OnCreateSession callback, string modelName, string customData = default(string))
337-
{
338-
if (string.IsNullOrEmpty(modelName))
339-
throw new ArgumentNullException("modelName");
340-
341-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/sessions");
342-
if (connector == null)
343-
return false;
344-
345-
CreateSessionReq req = new CreateSessionReq();
346-
req.Callback = callback;
347-
req.ModelName = modelName;
348-
req.Data = customData;
349-
req.Parameters["model"] = modelName;
350-
req.Headers["Content-Type"] = "application/json";
351-
req.Headers["Accept"] = "application/json";
352-
req.Send = Encoding.UTF8.GetBytes("{}");
353-
req.OnResponse = CreateSessionResponse;
354-
355-
return connector.Send(req);
356-
}
357-
358-
private class CreateSessionReq : RESTConnector.Request
359-
{
360-
public OnCreateSession Callback { get; set; }
361-
public string ModelName { get; set; }
362-
public string Data { get; set; }
363-
}
364-
365-
private void CreateSessionResponse(RESTConnector.Request req, RESTConnector.Response resp)
366-
{
367-
Session response = new Session();
368-
if (resp.Success)
369-
{
370-
try
371-
{
372-
fsData data = null;
373-
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
374-
if (!r.Succeeded)
375-
throw new WatsonException(r.FormattedMessages);
376-
377-
object obj = response;
378-
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
379-
380-
string cookie;
381-
if (resp.ResponseHeaders.TryGetValue("Watson-DPAT", out cookie))
382-
response.session_cookie = cookie;
383-
else
384-
Log.Warning("SpeechToText", "Failed to get session cookie!");
385-
386-
if (!r.Succeeded)
387-
throw new WatsonException(r.FormattedMessages);
388-
}
389-
catch (Exception e)
390-
{
391-
Log.Error("SpeechToText", "CreateSessionResponse Exception: {0}", e.ToString());
392-
resp.Success = false;
393-
}
394-
}
395-
396-
if (((CreateSessionReq)req).Callback != null)
397-
((CreateSessionReq)req).Callback(resp.Success ? response : null, ((CreateSessionReq)req).Data);
398-
}
399-
#endregion
400-
401-
#region Delete Session
402-
/// <summary>
403-
/// This callback is used by the DeleteSession() function.
404-
/// </summary>
405-
/// <param name="success"></param>
406-
/// <param name="data"></param>
407-
public delegate void OnDeleteSessionCallback(bool success, string data);
408-
/// <summary>
409-
/// Deletes an existing session and its engine. You cannot send requests to a session after it is deleted.
410-
/// </summary>
411-
/// <param name="callback">The callback.</param>
412-
/// <param name="sessionID">The ID of the session to be deleted.</param>
413-
/// <param name="customData">Optional custom data.</param>
414-
/// <returns></returns>
415-
public bool DeleteSession(OnDeleteSessionCallback callback, string sessionID, string session_cookie, string customData = default(string))
416-
{
417-
if (callback == null)
418-
throw new ArgumentNullException("callback");
419-
if (string.IsNullOrEmpty(sessionID))
420-
throw new ArgumentNullException("A sessionID to delete is required for DeleteSession");
421-
422-
DeleteSessionRequest req = new DeleteSessionRequest();
423-
req.Callback = callback;
424-
req.SessionID = sessionID;
425-
req.SessionCookie = session_cookie;
426-
req.Data = customData;
427-
req.Delete = true;
428-
req.OnResponse = OnDeleteSessionResp;
429-
req.Headers["Cookie"] = session_cookie;
430-
431-
string service = "/v1/sessions/{0}";
432-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(service, sessionID));
433-
if (connector == null)
434-
return false;
435-
436-
return connector.Send(req);
437-
}
438-
439-
private class DeleteSessionRequest : RESTConnector.Request
440-
{
441-
public OnDeleteSessionCallback Callback { get; set; }
442-
public string SessionID { get; set; }
443-
public string SessionCookie { get; set; }
444-
public string Data { get; set; }
445-
}
446-
447-
private void OnDeleteSessionResp(RESTConnector.Request req, RESTConnector.Response resp)
448-
{
449-
if (((DeleteSessionRequest)req).Callback != null)
450-
((DeleteSessionRequest)req).Callback(resp.Success, ((DeleteSessionRequest)req).Data);
451-
}
452-
#endregion
453-
454321
#region Sessionless - Streaming
455322
/// <summary>
456323
/// This callback object is used by the Recognize() and StartListening() methods.

Scripts/Utilities/Utility.cs

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -490,122 +490,5 @@ public static byte[] Color32ArrayToByteArray(Color32[] colors)
490490
return null;
491491
}
492492
#endregion
493-
494-
#region Cookies
495-
/// <summary>
496-
/// Returns a cookie string from a www request.
497-
/// </summary>
498-
/// <param name="www">WWW request.</param>
499-
/// <returns></returns>
500-
public static string GetRawCookieString(this WWW www)
501-
{
502-
if (!www.responseHeaders.ContainsKey("SET-COOKIE"))
503-
{
504-
return null;
505-
}
506-
507-
// HACK: workaround for Unity bug that doesn't allow multiple SET-COOKIE headers
508-
var rhsPropInfo = typeof(WWW).GetProperty("responseHeadersString", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
509-
if (rhsPropInfo == null)
510-
{
511-
Log.Error("Utility", "www.responseHeadersString not found. Deprecated?");
512-
return null;
513-
}
514-
var headersString = rhsPropInfo.GetValue(www, null) as string;
515-
if (headersString == null)
516-
{
517-
return null;
518-
}
519-
520-
// concat cookie headers
521-
var allCookies = new StringBuilder();
522-
string[] lines = headersString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
523-
foreach (var l in lines)
524-
{
525-
var colIdx = l.IndexOf(':');
526-
if (colIdx < 1)
527-
{
528-
continue;
529-
}
530-
var headerType = l.Substring(0, colIdx).Trim();
531-
if (headerType.ToUpperInvariant() != "SET-COOKIE")
532-
{
533-
continue;
534-
}
535-
var headerVal = l.Substring(colIdx + 1).Trim();
536-
if (allCookies.Length > 0)
537-
{
538-
allCookies.Append("; ");
539-
}
540-
allCookies.Append(headerVal);
541-
}
542-
543-
return allCookies.ToString();
544-
}
545-
546-
/// <summary>
547-
/// Returns a dictionary of cookies from a WWW request.
548-
/// </summary>
549-
/// <param name="www">The WWW request.</param>
550-
/// <returns></returns>
551-
public static Dictionary<string, string> ParseCookies(this WWW www)
552-
{
553-
return ParseCookies(www.GetRawCookieString());
554-
}
555-
556-
/// <summary>
557-
/// Returns a dictionary of cookies from a cookie string.
558-
/// </summary>
559-
/// <param name="str">The cookie string.</param>
560-
/// <returns></returns>
561-
public static Dictionary<string, string> ParseCookies(string str)
562-
{
563-
// cookie parsing adapted from node.js cookie module, so it should be pretty robust.
564-
var dict = new Dictionary<string, string>();
565-
if (str != null)
566-
{
567-
string[] pairs = Regex.Split(str, "; *");
568-
foreach (string pair in pairs)
569-
{
570-
int equalIndex = pair.IndexOf('=');
571-
if (equalIndex == -1)
572-
{
573-
continue;
574-
}
575-
string key = pair.Substring(0, equalIndex).Trim();
576-
if (dict.ContainsKey(key))
577-
{
578-
continue;
579-
}
580-
string val = pair.Substring(equalIndex + 1).Trim();
581-
if (val[0] == '"')
582-
{
583-
val = val.Substring(1, val.Length - 2);
584-
}
585-
586-
dict[key] = WWW.UnEscapeURL(val);
587-
}
588-
}
589-
590-
return dict;
591-
}
592-
593-
/// <summary>
594-
/// Returns a dictionary of cookie headers.
595-
/// </summary>
596-
/// <param name="cookies">The cookie dictionary.</param>
597-
/// <returns></returns>
598-
public static Dictionary<string, string> GetCookieRequestHeader(Dictionary<string, string> cookies)
599-
{
600-
var str = new StringBuilder();
601-
foreach (var c in cookies)
602-
{
603-
if (str.Length > 0)
604-
str.Append("; ");
605-
str.Append(c.Key).Append('=').Append(WWW.EscapeURL(c.Value));
606-
}
607-
return new Dictionary<string, string> { { "Cookie", str.ToString() } };
608-
}
609-
#endregion
610493
}
611494
}

0 commit comments

Comments
 (0)