PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/OrbAPI/Orb.cs

https://github.com/davuxcom/OrbWP7
C# | 220 lines | 196 code | 23 blank | 1 comment | 7 complexity | d7053a00fe9c4fbb0df239b7b47d954d MD5 | raw file
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using RestSharp;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using Newtonsoft.Json.Linq;
  15. using System.IO;
  16. using System.Xml.Linq;
  17. using System.Linq;
  18. namespace OrbAPI
  19. {
  20. public class Orb
  21. {
  22. private const string APIKey = "3orf1r01syqih";
  23. private string UserName, Password;
  24. public string SessionID { get; private set; }
  25. public string LoginName { get; private set; }
  26. public Orb(string UserName, string Password, Action SuccessfulLogin, Action<string> LoginError)
  27. {
  28. this.UserName = UserName;
  29. this.Password = Password;
  30. CallXmlMethod("session.login",
  31. new Dictionary<string, string>()
  32. {
  33. // TODO: add speed
  34. { "l", UserName },
  35. { "password", Password },
  36. { "width", "800" },
  37. { "height", "480" },
  38. },
  39. doc =>
  40. {
  41. SessionID = (from el in doc.Descendants("orbSessionId")
  42. select el).First().Value;
  43. Debug.WriteLine("SessionID: " + SessionID);
  44. SuccessfulLogin();
  45. }, LoginError);
  46. }
  47. public void Search(string mediaType, string filter, Action<SearchResult> Callback, Action<string> ErrorCallback)
  48. {
  49. var dict = new Dictionary<string, string>()
  50. {
  51. { "fields", "title,orbMediumId,size,thumbnailId" },
  52. { "sortBy", "title" },
  53. { "groupBy", "virtualPath" },
  54. };
  55. if (!string.IsNullOrEmpty(filter))
  56. {
  57. dict.Add("filter", filter);
  58. }
  59. else
  60. {
  61. dict.Add( "q", "mediaType=" + mediaType);
  62. }
  63. CallXmlMethod("media.search",
  64. dict,
  65. doc =>
  66. {
  67. Debug.WriteLine("search: " + doc);
  68. Callback(new SearchResult(doc,mediaType));
  69. },
  70. ErrorCallback);
  71. }
  72. private void CallXmlMethod(string method, Dictionary<string, string> keys, Action<XDocument> Callback, Action<string> ErrorCallback)
  73. {
  74. CallWebMethod("https://api.orb.com/orb/xml/", method, keys, (rgContent, strContent) =>
  75. {
  76. try
  77. {
  78. PreParse(XDocument.Load(new StringReader(strContent)), Callback, ErrorCallback);
  79. }
  80. catch (Exception ex)
  81. {
  82. string error = string.Format("Orb Parse Exception: {0}", ex);
  83. Debug.WriteLine(error);
  84. ErrorCallback(error);
  85. }
  86. },
  87. ErrorCallback);
  88. }
  89. private void CallWebMethod(string url, string method, Dictionary<string, string> keys, Action<byte[], string> Callback, Action<string> ErrorCallback)
  90. {
  91. Debug.WriteLine(string.Format("Calling Orb Method {0}", method));
  92. var request = new RestRequest(method, Method.GET);
  93. request.AddParameter("apiKey", APIKey, ParameterType.GetOrPost);
  94. if (SessionID != null)
  95. {
  96. request.AddParameter("sid", SessionID, ParameterType.GetOrPost);
  97. }
  98. foreach (var pair in keys)
  99. {
  100. request.AddParameter(pair.Key, pair.Value, ParameterType.GetOrPost);
  101. }
  102. new RestClient(url).ExecuteAsync(request, (response) =>
  103. {
  104. Debug.WriteLine(string.Format("Orb Call ({0}): {1}", method, response.Content));
  105. if (response.StatusCode == HttpStatusCode.OK)
  106. {
  107. Callback(response.RawBytes, response.Content);
  108. }
  109. else
  110. {
  111. string error = string.Format("Orb HTTP Error: {0} {1}", response.StatusCode, response.StatusDescription);
  112. Debug.WriteLine(error);
  113. ErrorCallback(error);
  114. }
  115. });
  116. }
  117. private void CallDataMethod(string method, Dictionary<string, string> keys, Action<byte[]> Callback, Action<string> ErrorCallback)
  118. {
  119. CallWebMethod("https://api.orb.com/orb/data/", method, keys, (rgContent, strContent) =>
  120. {
  121. try
  122. {
  123. Callback(rgContent);
  124. }
  125. catch (Exception ex)
  126. {
  127. string error = string.Format("Orb Parse Exception: {0}", ex);
  128. Debug.WriteLine(error);
  129. ErrorCallback(error);
  130. }
  131. }, ErrorCallback);
  132. }
  133. private void CallDataMethod(string method, Dictionary<string, string> keys, Action<string> Callback, Action<string> ErrorCallback)
  134. {
  135. CallWebMethod("https://api.orb.com/orb/data/", method, keys, (rgContent, strContent) =>
  136. {
  137. try
  138. {
  139. Callback(strContent);
  140. }
  141. catch (Exception ex)
  142. {
  143. string error = string.Format("Orb Parse Exception: {0}", ex);
  144. Debug.WriteLine(error);
  145. ErrorCallback(error);
  146. }
  147. }, ErrorCallback);
  148. }
  149. public void DownloadThumbnail(string item, Action<byte[]> Callback, Action<string> ErrorCallback)
  150. {
  151. CallDataMethod("image.png", new Dictionary<string, string>()
  152. {
  153. { "mediumId", item },
  154. { "maxWidth", "96" },
  155. { "maxHeight", "96" },
  156. { "allowEnlarge", "1" },
  157. { "forceSize", "1" },
  158. },
  159. Callback, ErrorCallback);
  160. }
  161. public void DownloadImage(string item, Action<byte[]> Callback, Action<string> ErrorCallback)
  162. {
  163. CallDataMethod("image.jpg", new Dictionary<string, string>()
  164. {
  165. { "mediumId", item },
  166. },
  167. Callback, ErrorCallback);
  168. }
  169. public void GetStream(string item, int speed, Action<string> asxContent, Action<string> ErrorCallback)
  170. {
  171. CallDataMethod("stream.asx", new Dictionary<string, string>()
  172. {
  173. { "mediumId", item },
  174. { "speed", speed.ToString() },
  175. },
  176. asxContent, ErrorCallback);
  177. }
  178. private void PreParse(XDocument doc, Action<XDocument> SuccessCallback, Action<string> ErrorCallback)
  179. {
  180. var orb = (from el in doc.Descendants("status") select el).First();
  181. var error = int.Parse(orb.Attribute("code").Value);
  182. Debug.WriteLine("Error: " + error);
  183. if (error == 0)
  184. {
  185. var login = doc.Root.Attribute("login").Value;
  186. Debug.WriteLine("Login: " + login);
  187. SuccessCallback(doc);
  188. }
  189. else
  190. {
  191. var errorDesc = string.Format("Orb Error: {0}", orb.Attribute("desc").Value);
  192. Debug.WriteLine(errorDesc);
  193. ErrorCallback(errorDesc);
  194. }
  195. }
  196. }
  197. }