PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/dotnet/Api/ActivityInfoService.cs

http://sigma-h.googlecode.com/
C# | 131 lines | 111 code | 20 blank | 0 comment | 5 complexity | 009732ed1148dede2e2978566518a9c8 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Collections.Specialized;
  4using System.Linq;
  5using System.Text;
  6using System.Net;
  7using System.IO;
  8using System.Web;
  9using JsonExSerializer;
 10using JsonExSerializer.MetaData;
 11
 12namespace ActivityInfo.Api
 13{
 14    public class ActivityInfoService
 15    {
 16        private string baseUrl;
 17        private string authToken;
 18
 19        public ActivityInfoService(string baseUrl, string email, string password)
 20        {
 21            this.baseUrl = baseUrl;
 22            login(email, password);
 23        }
 24
 25        public ActivityInfoService(string email, string password)
 26        {
 27            this.baseUrl = "http://www.activityinfo.org/";
 28            login(email, password);
 29        }
 30
 31        public SyncRegions GetSyncRegions()
 32        {
 33            Serializer serializer = new Serializer(typeof(SyncRegions));
 34            serializer.Config.TypeHandlerFactory.SetPropertyNamingStrategy(new CamelCaseNamingStrategy());
 35
 36            return (SyncRegions)doGet("SyncRegions", new NameValueCollection(), serializer);
 37        }
 38
 39        public SyncRegionUpdates GetSyncRegionUpdates(string regionId, string localVersion) {
 40
 41            NameValueCollection parameters = new NameValueCollection();
 42            parameters.Add("regionId", regionId);
 43            if (localVersion != null)
 44            {
 45                parameters.Add("localVersion", localVersion);
 46            }
 47
 48            Serializer serializer = new Serializer(typeof(SyncRegionUpdates));
 49            serializer.Config.TypeHandlerFactory.SetPropertyNamingStrategy(new CamelCaseNamingStrategy());
 50
 51            return (SyncRegionUpdates)doGet("SyncRegionUpdates", parameters, serializer);
 52        }
 53
 54        private void login(String email, String password)
 55        {
 56            Uri uri = new Uri(baseUrl + "Login/service");
 57            String postData = "email=" + email + "&password=" + password;
 58            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
 59            request.Method = "POST";
 60            request.ContentType = "application/x-www-form-urlencoded";
 61            request.ContentLength = postData.Length;
 62            request.CookieContainer = new CookieContainer();
 63
 64            using (Stream writeStream = request.GetRequestStream())
 65            {
 66                UTF8Encoding encoding = new UTF8Encoding();
 67                byte[] bytes = encoding.GetBytes(postData);
 68                writeStream.Write(bytes, 0, bytes.Length);
 69            }
 70
 71            String result;
 72            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 73            {
 74                using (Stream responseStream = response.GetResponseStream())
 75                {
 76                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
 77                    {
 78                        result = readStream.ReadToEnd();
 79                    }
 80                }
 81                if (result.Contains("OK"))
 82                {
 83                    authToken = response.Cookies["authToken"].Value;
 84                }
 85                else
 86                {
 87                    throw new AuthenticationException();
 88                }
 89            }
 90        }
 91
 92        private object doGet(String commandName, NameValueCollection parameters, Serializer serializer)
 93        {
 94            Uri uri = new Uri(baseUrl + "api/" + commandName + ToQueryString(parameters));
 95           
 96            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
 97            request.Method = "GET";
 98            request.Headers.Add("X-ActivityInfo-AuthToken", authToken);
 99           
100
101            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
102            {
103                using (Stream responseStream = response.GetResponseStream())
104                {
105                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
106                    {
107                        string json = readStream.ReadToEnd();
108                        return serializer.Deserialize(json);
109                    }
110                }
111            }
112        }
113
114        private string ToQueryString(NameValueCollection nvc)
115        {
116            if (nvc.Count == 0)
117            {
118                return "";
119            }
120            else
121            {
122                return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys,
123                    key => string.Format("{0}={1}", HttpUtility.UrlEncode(key),
124                            HttpUtility.UrlEncode(nvc[key]))));
125            }
126        }
127
128    }
129}
130
131