Showing posts with label OoyalaAPI. Show all posts
Showing posts with label OoyalaAPI. Show all posts

Tuesday, March 11, 2014

Retrieving Ooyala Engagement data from API

To retrieve Engagement data, we need to provide an ID which will be available from EmbedCode_Property table as text column. 

The following figure will illustrate the process flow of retrieving the Ooyala Engagement data from OoyalaAPI:


Select all the distinct Ids in SQL Task and have it in a result set. Create variable for it 
as an object.

Create For each loop container to have Script Task and DFT. In Script task provide the User:apikey and 
User:secretkey parameters in the ReadOnlyVariables and click the Edit Script button.

Provide the below mentioned code which will retrieve data using the referrence support of Json and OoyalaAPI cs files.

Here we need to add "Newtonsoft.Json.Net20.dll". Download the Json referrence file from the web and save it  in the corresponding foler. 

Go to Solution Explorer - Right Click Reference - Select Add Reference - Browse the Json file and add it.
Also add the JSON.cs and OoyalaAPI.cs files.

------------------------------------------------------------------

#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services control flow. 
 * 
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script task. */
#endregion


#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
//using Newtonsoft.Json;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Collections;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
//using System.Xml;

using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
#endregion

namespace ST_e00b761ae09948d5aef4ab0b0a0e6a78
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
        #region Help:  Using Integration Services variables and parameters in a script
        ..........
        #endregion


/// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        
        XmlDocument test1;
public void Main()
{
           
Call_RestFectchData();
Dts.TaskResult = (int)ScriptResults.Success;
           
}
        string Call_RestFectchData()
        {
            string responseXml = string.Empty;
            

            try
            {
                string api_key, secret_key, ContentKey;
                DateTime PlayDate;
                api_key = (string)Dts.Variables["User::apiKey"].Value;
                secret_key = (string)Dts.Variables["User::secretKey"].Value;
                ContentKey = (string)Dts.Variables["User::ContentKey"].Value;

                PlayDate = (DateTime)Dts.Variables["User::PlayDate"].Value;
                var date = PlayDate.ToString("yyyy-MM-dd");

                OoyalaAPI api = new OoyalaAPI(api_key,secret_key);

                Dictionary<String, String> parameters = new Dictionary<String, String>();
               
                Hashtable values = api.getHashtable("analytics/reports/asset/" + ContentKey + "/engagement/total/" + date ,parameters);

                var s2 = JsonConvert.SerializeObject(new { items = values });

                test1 =JsonToXML(s2);
                System.IO.File.WriteAllText(@"M:\ooyala\PlayHistory\Ooyala_PlayHistory.xml", test1.InnerXml);
                
              
            }
            catch (Exception exc)
            {
                responseXml = "ERROR: " + exc.Message;
            }

            return responseXml;
        }
        
        public XmlDocument JsonToXML(string json)
        {
            XmlDocument doc = new XmlDocument();

            using (var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max))
            {
                XElement xml = XElement.Load(reader);
                doc.LoadXml(xml.ToString());
            }

            return doc;
        }
        }
    
        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

}

-------------------------------------------------------------
Now create DFT Task and add XML Source task and select the source files and generate the XSD file. From this XML source, we can map the columns to the corresponding tables.


Retrieving Ooyala Content (Assets) data from API

Ooyala is a venture-backed, privately held company that provides online video technology products and services. Ooyala provides powerful and easy-to-use RESTful APIs that enable you to programmatically access Backlot, whether you want to upload videos, manage your assets, or completely integrate your content management system.

To start work with Ooyala backlot API, we need username and password to log in http://www.ooyala.com/backlot/web in which we can get the API_Key and Secret_Key. These keys are used to authenticate the programming code to retrive data.

We can also view the data using the following URL by providing the API_Key and Secret_Key.
https://api.ooyala.com/docs/api_scratchpad?url=&method=GET&api_key=

Below is the Developer documentation URL:
http://support.ooyala.com/developers/documentation

The following figures will illustrate the process flow of retrieving the Ooyala Content data from OoyalaAPI:


create variables as shown below:


create ScriptTask and provide the User:apikey and User:secretkey parameters in the ReadOnlyVariables and click the Edit Script button.


Provide the below mentioned code which will retrieve data using the referrence support of Json and OoyalaAPI cs files.

Here we need to add "Newtonsoft.Json.Net20.dll". Download the Json referrence file from the web and save it in the corresponding foler. 

Go to Solution Explorer - Right Click Reference - Select Add Reference - Browse the Json file and add it.
Also add the JSON.cs and OoyalaAPI.cs files.

--------------------------------------------------------------------
#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services control flow. 
 * 
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script task. */
#endregion


#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
//using Newtonsoft.Json;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Collections;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
//using System.Xml;

using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
#endregion

namespace ST_e00b761ae09948d5aef4ab0b0a0e6a78
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
       ..........
         * */
        #endregion


/// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        public ArrayList asstesParam;
public void Main()
{
Call_RestFectchData();
Dts.TaskResult = (int)ScriptResults.Success;
}
        string Call_RestFectchData()
        {
            string responseXml = string.Empty;
            

            try
            {
                string api_key, secret_key;
                api_key = (string)Dts.Variables["User::apiKey"].Value;
                secret_key = (string)Dts.Variables["User::secretKey"].Value;

               // OoyalaAPI api = new OoyalaAPI("ZzZGMxOo_dNZEF6BH2MfXVqweXLU.C0AdU", "2_XzbDXJwNTKBRW3yLPu_bxtr-CxrBEZBHedKrrE");
                OoyalaAPI api = new OoyalaAPI(api_key,secret_key);

                Dictionary<String, String> parameters = new Dictionary<String, String>();
                parameters.Add("include", "metadata,labels");
                parameters.Add("limit", "500");


                Hashtable values = api.getHashtable("assets", parameters);


                asstesParam = (ArrayList)api.getHashtable("assets", parameters)["items"];
                getpage_token(values);

                var s2 = JsonConvert.SerializeObject(new { items = asstesParam });


                XmlDocument test1 = JsonToXML(s2);
                System.IO.File.WriteAllText(@"M:/Ooyala/Ooyala_Asset_Unconverted.xml", test1.InnerXml);




            }
            catch (Exception exc)
            {
                responseXml = "ERROR: " + exc.Message;
            }

            return responseXml;
        }
        public string getpage_token(Hashtable values)
        {
            string page_token = string.Empty;
            foreach (DictionaryEntry entry in values)
            {
                if (entry.Key.ToString() == "next_page")
                {
                    Dictionary<String, String> parameters = new Dictionary<String, String>();
                    OoyalaAPI api = new OoyalaAPI("ZzZGMxOo_dNZEF6BH2MfXVqweXLU.C0AdU", "2_XzbDXJwNTKBRW3yLPu_bxtr-CxrBEZBHedKrrE");
                    page_token = entry.Value.ToString();
                    string splitvariable = "page_token=";
                    string[] s = page_token.Split(new[] { splitvariable }, StringSplitOptions.None);
                    page_token = s[1];
                    parameters.Add("include", "metadata,labels");
                    parameters.Add("limit", "500");
                    parameters.Add("next_page", page_token);
                    parameters.Add("page_token", page_token);
                    Hashtable valuesnextpage = api.getHashtable("assets", parameters);
                    ArrayList asstesParam1 = (ArrayList)api.getHashtable("assets", parameters)["items"];
                    foreach (var item in asstesParam1)
                    {
                        asstesParam.Add(item);
                    }
                    string assets = api.get("assets", parameters);

                    return getpage_token(valuesnextpage);
                }
            }
            return page_token;
        }
        public XmlDocument JsonToXML(string json)
        {
            XmlDocument doc = new XmlDocument();

            using (var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max))
            {
                XElement xml = XElement.Load(reader);
                doc.LoadXml(xml.ToString());
            }

            return doc;
        }
        }
    
        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

}
--------------------------------------------------------------------

Now add XML Task to convert the output file in to readable file as shown below:



Create DFT Task and add XML Source task and select the source files and generate the XSD file as shown below:



From this XML source, we can map the columns to the corresponding tables.

Ooyala API reference file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using System.IO;
using System.Web;
using System.Security.Cryptography;

namespace ST_e00b761ae09948d5aef4ab0b0a0e6a78
{
    class OoyalaAPI
    {
        public const string BASE_URL = "https://api.ooyala.com/v2/";

        private const int UPLOAD_TIMEOUT = 1000 * 3600;

        public string apiKey;
        public string secretKey;

        private HttpWebResponse response;

        /// <summary>
        /// Default constructor. You need to provide your API key and secret key. You can find this in the developer tab in Backlot
        /// <seealso cref="http://api.ooyala.com/docs/v2/"/>
        /// </summary>
        ///<param name="apiKey">
        /// Your account's API key 
        /// </param>
        /// <param name="secretKey">
        /// Your account's secret key 
        /// </param>
        public OoyalaAPI(string apiKey, string secretKey)
        {
            this.apiKey = apiKey;
            this.secretKey = secretKey;
        }
        public Hashtable getHashtable(string path, Dictionary<String, String> parameters)
        {
            var url = this.generateURL("GET", path, parameters, "");

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            //string text;
            //var response = (HttpWebResponse)request.GetResponse();

            //using (var sr = new StreamReader(response.GetResponseStream()))
            //{
            //    text = sr.ReadToEnd();
            //}
            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }
        /// <summary>
        /// Makes an HTTP GET request to the Ooyala API with the specified path and using the given parameters
        /// </summary>
        /// <param name="path">
        /// The path to the resource to use for the GET request, i.e "players"
        /// </param>
        /// <param name="parameters">
        /// A Dictionary containing the parameters to be send along with the GET request. 
        /// The only required parameter is expires since api_key will be added for you with the value you passed in the constructor of this class
        /// </param>
        public string get(string path, Dictionary<String, String> parameters)
        {
            var url = this.generateURL("GET", path, parameters, "");

            //HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            //request.Method = "GET";
            string json = string.Empty;
            using (var client = new WebClient())
            {
                json = client.DownloadString(url);
            }
            //string text;
            //var response = (HttpWebResponse)request.GetResponse();

            //using (var sr = new StreamReader(response.GetResponseStream()))
            //{
            //    text = sr.ReadToEnd();
            //}
            //if (this.getResponse(request))
            //{
            //    return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            //}

            return json;
        }


        /// <summary>
        /// Makes a POST request to the Ooyala V2 API. Post request are used to create objects like Assets, Labels, Players, etc.
        /// </summary>
        /// <param name="path">
        /// The path to the resource to post to.
        /// </param>
        /// <param name="parameters">
        /// A Hash containing a list of parameters. The expires parameter is required. 
        /// </param>
        /// <param name="body">
        /// A String containing the JSON data to use for the creation of the object
        /// </param>
        /// <returns>
        /// returns the create object data in JSON format
        /// </returns>
        public Hashtable post(string path, Dictionary<String, String> parameters, Hashtable body)
        {
            String jsonBody = JSON.JsonEncode(body);
            var url = this.generateURL("POST", path, parameters, jsonBody);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";

            var data = Encoding.Default.GetBytes(jsonBody);

            request.ContentLength = data.Length;

            var stream = request.GetRequestStream();

            stream.Write(data, 0, data.Length);

            stream.Close();

            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }

        /// <summary>
        /// Makes a POST request sending bytes encoded in UTF8 to the Ooyala V2 API. This method can be used to upload preview images or closed caption files for assets.
        /// </summary>
        /// <param name="path">
        /// The path to the resource to post the bytes to.
        /// </param>
        /// <param name="parameters">
        /// A Hash containing parameters to be sent along with the request. Required parameter: expires.
        /// </param>
        /// <param name="body">
        /// An array of bytes representing the image/file you want to post to the API object.
        /// </param>
        /// <returns>
        /// A String containing the JSON response from the Server
        /// </returns>
        public Hashtable postBytes(string path, Dictionary<String, String> parameters, System.Byte[] body, String fileName = null)
        {
            var url = this.generateURL("POST", path, parameters, Encoding.Default.GetString(body));

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.AllowWriteStreamBuffering = false;
            request.Timeout = UPLOAD_TIMEOUT;
            request.SendChunked = false;

            request.ContentLength = body.Length;

            var stream = request.GetRequestStream();
            stream.Write(body, 0, body.Length);
            stream.Flush();
            stream.Close();

            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }

        /// <summary>
        /// Puts the bytes from the specified file to the API resource.
        /// </summary>
        /// <returns>
        /// The JSON response from the server in the form of a Hashtable.
        /// </returns>
        /// <param name='path'>
        /// The path to the resource to put the bytes to.
        /// </param>
        /// <param name='parameters'>
        /// Query string parameters in the form of a Dictionary.
        /// </param>
        /// <param name='body'>
        /// A Hashtable containing the body of the request that will later be converted into JSON.
        /// </param>
        /// <param name='fileName'>
        /// The name of the file whose bytes will be sent via the PUT request.
        /// </param>
        public Hashtable putBytes(string path, Dictionary<String, String> parameters, System.Byte[] body, String fileName = null)
        {
            var url = this.generateURL("PUT", path, parameters, Encoding.Default.GetString(body));

            System.Net.Cache.RequestCachePolicy requestCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.CachePolicy = requestCachePolicy;
            request.Method = "PUT";
            request.AllowWriteStreamBuffering = false;
            request.Timeout = UPLOAD_TIMEOUT;
            request.SendChunked = false;

            request.ContentLength = body.Length;

            var stream = request.GetRequestStream();
            stream.Write(body, 0, body.Length);
            stream.Flush();
            stream.Close();

            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }

        /// <summary>
        /// Make a PATCH request to the Ooyala V2 API. PATCH requests are used to update data on objects. <seealso cref="hhttp://api.ooyala.com/docs/v2"/>
        /// </summary>
        /// <param name="path">
        /// The path on the API server that indicates the object to be patched. In a patch request the path should look something like: :object_type/:object_id i.e. assets/zxhs16
        /// </param>
        /// <param name="parameters">
        /// A Hash containing the list of parameters to be sent along with the request. Required parameter: expires.
        /// </param>
        /// <param name="body">
        /// A String containing the data to be sent for the patch in JSON format.
        /// </param>
        /// <returns>
        /// A String containing the JSON response from the server.
        /// </returns>
        public Hashtable patch(string path, Dictionary<String, String> parameters, Hashtable body)
        {
            var url = this.generateURL("PATCH", path, parameters, body);
            String jsonBody = JSON.JsonEncode(body);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "PATCH";
            request.ContentType = "application/x-www-form-urlencoded";

            var data = Encoding.UTF8.GetBytes(jsonBody);

            request.ContentLength = data.Length;

            var stream = request.GetRequestStream();

            stream.Write(data, 0, data.Length);

            stream.Close();

            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }

        /// <summary>
        /// Make a PUT request to the Ooyala V2 API. PATCH requests are used to update data on objects. <seealso cref="http://api.ooyala.com/docs/v2"/>
        /// </summary>
        /// <param name="path">
        /// The path on the API server that indicates the object to be replaced. In a put request the path should look something like: :object_type/:object_id i.e. assets/zxhs16
        /// </param>
        /// <param name="parameters">
        /// A Hash containing the list of parameters to be sent along with the request. Required parameter: expires.
        /// </param>
        /// <param name="body">
        /// A String containing the data to be sent for the put in JSON format.
        /// </param>
        /// <returns>
        /// A String containing the JSON response from the server.
        /// </returns>
        public Hashtable put(string path, Dictionary<String, String> parameters, Hashtable body)
        {
            var url = this.generateURL("PUT", path, parameters, body);
            String jsonBody = JSON.JsonEncode(body);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "PUT";
            request.ContentType = "application/x-www-form-urlencoded";

            var data = Encoding.UTF8.GetBytes(jsonBody);

            request.ContentLength = data.Length;

            var stream = request.GetRequestStream();

            stream.Write(data, 0, data.Length);

            stream.Close();

            if (this.getResponse(request))
            {
                return (Hashtable)JSON.JsonDecode(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }

            return null;
        }

        /// <summary>
        /// Issues a DELETE request to remove an object using the Ooyala V2 API. <seealso cref="http://api.ooyala.com/docs/v2"/>
        /// </summary>
        /// <param name="path">
        /// The path of the resource to delete
        /// </param>
        /// <param name="parameters">
        /// A Hash containing the list of parameters to be sent along with the request. Required parameter: expires.
        /// </param>
        /// <returns>
        /// True if the asset was deleted, false if it was already deleted or does not exist
        /// </returns>
        public Boolean delete(string path, Dictionary<String, String> parameters)
        {
            var url = this.generateURL("DELETE", path, parameters, "");

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "DELETE";

            return this.getResponse(request);
        }

        private string generateURL(string HTTPMethod, string path, Dictionary<System.String, System.String> parameters, Hashtable body)
        {
            return generateURL(HTTPMethod, path, parameters, JSON.JsonEncode(body));
        }

        /// <summary>
        /// Takes in the necessary parameters to build a V2 signature for the Ooyala API
        /// </summary>
        /// <param name="HTTPMethod">
        /// The method to be used for the request. Possible values are: GET, POST, PUT, PATCH or DELETE
        /// </param>
        /// <param name="path">
        /// The path to use for the request
        /// </param>
        /// <param name="parameters">
        /// A hash containing the list of parameters that will be included in the request.
        /// </param>
        /// <param name="body">
        /// A string containing the JSON representation of the data to be sent on the request. If its a GET request, the body parameter will not be used to generate the signature.
        /// </param>
        /// <returns>
        /// The URL to be used in the HTTP request.
        /// </returns>
        private string generateURL(string HTTPMethod, string path, Dictionary<System.String, System.String> parameters, String body)
        {
            var url = BASE_URL + path;

            path = "/v2/" + path;

            if (!parameters.ContainsKey("api_key"))
            {
                parameters.Add("api_key", this.apiKey);
            }

            if (!parameters.ContainsKey("expires"))
            {
                DateTime now = DateTime.UtcNow;
                //Round up to the expiration to the next hour for higher caching performance
                DateTime expiresWindow = new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0);
                int expires = (int)(expiresWindow - new DateTime(1970, 1, 1)).TotalSeconds;
                parameters.Add("expires", expires.ToString());
            }

            //Sorting the keys
            var sortedKeys = new String[parameters.Keys.Count];
            parameters.Keys.CopyTo(sortedKeys, 0);
            Array.Sort(sortedKeys);

            for (int i = 0; i < sortedKeys.Length; i++)
            {
                url += (i == 0 && !url.Contains("?") ? "?" : "&") + sortedKeys[i] + "=" + HttpUtility.UrlEncode(parameters[sortedKeys[i]]);
            }

            url += "&signature=" + this.generateRequestSignature(HTTPMethod, path, sortedKeys, parameters, body);

            return url;
        }

        /// <summary>
        /// Generates the signature for the V2 API request based on the given parameters. <seealso cref="http://api.ooyala.com/v2/docs"/>
        /// </summary>
        /// <param name="HTTPMethod">
        /// The method to be used for the request. Possible values are: GET, POST, PUT, PATCH or DELETE
        /// </param>
        /// <param name="path">
        /// The path to use for the request
        /// </param>
        /// <param name="sortedParameterKeys">
        /// A sorted array containing the keys of the parameters hash. This is to improve efficiency and not sort them twice since generateURL already does it.
        /// </param>
        /// <param name="parameters">
        /// A hash containing the list of parameters that will be included in the request.
        /// </param>
        /// <param name="body">
        /// A string containing the JSON representation of the data to be sent on the request. If its a GET request, the body parameter will not be used to generate the signature.
        /// </param>
        /// <returns>
        /// A string containing the signature to be used in the V2 API request.
        /// </returns>
        private string generateRequestSignature(string HTTPMethod, String path, String[] sortedParameterKeys, Dictionary<String, String> parameters, String body)
        {
            var stringToSign = this.secretKey + HTTPMethod + path;

            for (int i = 0; i < sortedParameterKeys.Length; i++)
            {
                stringToSign += sortedParameterKeys[i] + "=" + parameters[sortedParameterKeys[i]];
            }

            stringToSign += body;

            var sha256 = new SHA256Managed();
            byte[] digest = sha256.ComputeHash(Encoding.Default.GetBytes(stringToSign));
            string signedInput = Convert.ToBase64String(digest);

            //Removing the trailing = signs
            var lastEqualsSignindex = signedInput.Length - 1;
            while (signedInput[lastEqualsSignindex] == '=')
            {
                lastEqualsSignindex--;
            }

            signedInput = signedInput.Substring(0, lastEqualsSignindex + 1);

            return HttpUtility.UrlEncode(signedInput.Substring(0, 43));
        }

        private Boolean getResponse(HttpWebRequest request)
        {
            try
            {
                response = request.GetResponse() as HttpWebResponse;
                return true;
            }
            catch (WebException e)
            {
                Console.WriteLine("Exception Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = ((HttpWebResponse)e.Response);
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);

                    var stream = response.GetResponseStream();
                    var reader = new StreamReader(stream);
                    var text = reader.ReadToEnd();
                    Console.WriteLine("Response Description : {0}", text);

                }
                return false;
            }
        }
    }
}