RestClient.cs 9.37 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace MoyaAdminLib
{
    


    public enum HttpVerb
    {
        GET,
        POST,
        PUT,
        DELETE
    }
    public class RestClient
    {
        public string EndPoint { get; set; }
        public HttpVerb Method { get; set; }
        public string ContentType { get; set; }
        public string PostData { get; set; }

        public static string ApiApplicationKey;
        public static string ApiUser;
        public static string ApiPass;
        public static string ApiURL;

        public RestClient()
        {
            EndPoint = ApiURL;
            Method = HttpVerb.GET;
            
            ContentType = "application/json; charset=utf-8";
            PostData = "";
        }
        public RestClient(string endpoint)
        {
            EndPoint = endpoint;
            Method = HttpVerb.GET;
            ContentType = "application/json; charset=utf-8";
            PostData = "";
        }
        public RestClient(string endpoint, HttpVerb method)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "application/json; charset=utf-8";
            PostData = "";
        }

        public RestClient(string endpoint, HttpVerb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "application/json; charset=utf-8";
            PostData = postData;
        }


        public static string CalculateSHA1(string text)
        {
            // Convert the input string to a byte array
            byte[] buffer = Encoding.GetEncoding("iso-8859-1").GetBytes(text);

            // In doing your test, you won't want to re-initialize like this every time you test a
            // string.
            SHA1CryptoServiceProvider cryptoTransformSHA1 =
                new SHA1CryptoServiceProvider();

            // The replace won't be necessary for your tests so long as you are consistent in what
            // you compare.    
            string hash = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "").ToLower();

            return hash;
        }
        private static int ConvertToTimestamp(DateTime value)
        {
            //create Timespan by subtracting the value provided from
            //the Unix Epoch
            TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

            //return the total seconds (which is a UNIX timestamp)
            return (int)span.TotalSeconds;
        }

        public static string GetRequestURL(string server, string queryPath)
        {
            return GetRequestURL(server, queryPath,null);
        }
        public static string GetRequestURL(string server, string queryPath, string getparms)
        {
            int timestamp = ConvertToTimestamp(DateTime.Now);
            
            string hash = CalculateSHA1("/" + queryPath +  "+" + ApiApplicationKey + "+" + ApiUser + "+" + timestamp + "+" + ApiPass);
            
            if (getparms != null && getparms.Length > 0)
                getparms = getparms + "&";

            string url = server + "/rest/" + queryPath + "?";
            if (getparms != null)
                url += getparms;

            url += "appkey=" + ApiApplicationKey + "&appuser=" + ApiUser + "&appstamp=" + timestamp + "&appmac=" + hash;

            Console.WriteLine(url);
            return url;
        }
        
        public string MakeRequest(string queryPath)
        {
            return MakeRequest(queryPath, null);
        }

        public string MakeRequest(string queryPath, string getparms)
        {

            ///placeadmin/places/30+abcdefg+testuser-asdf+1393735570144+acdcabbacd
            ///
            
            var request = (HttpWebRequest)WebRequest.Create(GetRequestURL(EndPoint,queryPath,getparms));


            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;
            

            if (!string.IsNullOrEmpty(PostData) && (Method == HttpVerb.POST || Method == HttpVerb.PUT ))
            {
                var encoding = new UTF8Encoding();
                var bytes = Encoding.GetEncoding("utf-8").GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }


            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;

                    if ((int)response.StatusCode < 200 && (int)response.StatusCode >= 300)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    // grab the response
                    //if (response.ContentLength > 0)
                    //{
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }
                    //}

                    return responseValue;
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ConnectFailure)
                    throw e;
                Stream responseStream = ((WebException)e).Response.GetResponseStream();

                if (responseStream != null)
                {
                    string responseValue = StreamToString(responseStream);
                    Console.WriteLine("Response was " + responseValue);
                    throw new Exception(responseValue);
                }

                
                throw e;
            }

        }
        /// <summary>
        /// Convert streams from web to string
        /// </summary>
        /// <param name="responseStream">Webresponse stream</param>
        /// <returns>string</returns>
        private string StreamToString(Stream responseStream)
        {
            StreamReader reader = new StreamReader(responseStream);
            string responseString = reader.ReadToEnd();
            responseStream.Close();
            reader.Close();
            return responseString;
        }

        public static ApiCredential GetApiCredential(string username, string pw, string apiKey, string url)
        {
            string requestUrl = url + "/rest/apiapp/v1/createInstance/" + apiKey;
            Console.WriteLine("Url: " + requestUrl);
            var request = (HttpWebRequest)WebRequest.Create(url+"/rest/apiapp/v1/createInstance/"+apiKey);
            /*
            NetworkCredential credentials = new NetworkCredential();
            credentials.UserName = username;
            credentials.Password = pw;
            CredentialCache credentialCache = new CredentialCache();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            credentialCache.Add(new System.Uri(url), "Basic", credentials);
            request.Credentials = credentialCache;
            
            request.PreAuthenticate = true;
            */

            

            string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(username + ":" + pw));
            request.Headers.Add("Authorization", "Basic " + encoded);

            request.Method = "POST";
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;

                    if ((int)response.StatusCode < 200 && (int)response.StatusCode >= 300)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    // grab the response
                    //if (response.ContentLength > 0)
                    //{
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }
                    //}

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    try
                    {
                        ApiCredential credential = ser.Deserialize<ApiCredential>(responseValue);
                        return credential;
                    } catch(Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (WebException e)
            {   
                throw e;
            }

        }


    } // class

}