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