public XmlElement DoQueue()
        {
            if (_CallsQueue.Count == 0)
            {
                _IsMultiRequest = false;
                return(null);
            }

            DateTime startTime = DateTime.Now;

            this.Log("service url: [" + this._Config.ServiceUrl + "]");

            KalturaParams kparams = new KalturaParams();
            KalturaFiles  kfiles  = new KalturaFiles();

            // append the basic params
            kparams.Add("apiVersion", this._ApiVersion);
            kparams.Add("clientTag", this._Config.ClientTag);
            kparams.AddIntIfNotNull("format", this._Config.ServiceFormat.GetHashCode());

            string url = this._Config.ServiceUrl + "/api_v3/index.php?service=";

            if (_IsMultiRequest)
            {
                url += "multirequest";
                int i = 1;
                foreach (KalturaServiceActionCall call in _CallsQueue)
                {
                    KalturaParams callParams = call.GetParamsForMultiRequest(i);
                    kparams.Add(callParams);
                    KalturaFiles callFiles = call.GetFilesForMultiRequest(i);
                    kfiles.Add(callFiles);
                    i++;
                }

                // map params
                foreach (KeyValuePair <string, string> item in _MultiRequestParamsMap)
                {
                    string requestParam = item.Key;
                    string resultParam  = item.Value;

                    if (kparams.ContainsKey(requestParam))
                    {
                        kparams[requestParam] = resultParam;
                    }
                }
            }
            else
            {
                KalturaServiceActionCall call = _CallsQueue[0];
                url += call.Service + "&action=" + call.Action;
                kparams.Add(call.Params);
                kfiles.Add(call.Files);
            }

            // cleanup
            _CallsQueue.Clear();
            _IsMultiRequest = false;
            _MultiRequestParamsMap.Clear();

            kparams.Add("sig", this.Signature(kparams));

            this.Log("full reqeust url: [" + url + "]");

            // build request
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Timeout = _Config.Timeout;
            request.Method  = "POST";

            // Add proxy information if required
            if (!_Config.ProxyAddress.Equals("") && !_Config.ProxyAddress.Equals(null))
            {
                request.Proxy = new WebProxy(_Config.ProxyAddress);
            }

            if (kfiles.Count > 0)
            {
                this.PostMultiPartWithFiles(request, kparams, kfiles);
            }
            else
            {
                this.PostUrlEncodedParams(request, kparams);
            }

            // get the response
            WebResponse  response       = request.GetResponse();
            Encoding     enc            = System.Text.Encoding.UTF8;
            StreamReader responseStream = new StreamReader(response.GetResponseStream(), enc);
            string       responseString = responseStream.ReadToEnd();

            this.Log("result (serialized): " + responseString);

            DateTime endTime = DateTime.Now;

            this.Log("execution time for [" + url + "]: [" + (endTime - startTime).ToString() + "]");

            XmlDocument xml = new XmlDocument();

            xml.LoadXml(responseString);

            this.ValidateXmlResult(xml);
            XmlElement result = xml["xml"]["result"];

            this.ThrowExceptionOnAPIError(result);

            return(result);
        }
Example #2
0
        public XmlElement DoQueue()
        {
            if (_CallsQueue.Count == 0)
            {
                _MultiRequestReturnType.Clear();
                _MultiRequestReturnType = null;
                return(null);
            }

            DateTime startTime = DateTime.Now;

            this.Log("service url: [" + this._Config.ServiceUrl + "]");

            KalturaParams kparams = new KalturaParams();
            KalturaFiles  kfiles  = new KalturaFiles();

            // append the basic params
            kparams.Add("apiVersion", this._ApiVersion);
            kparams.Add("clientTag", this._Config.ClientTag);
            kparams.AddIntIfNotNull("format", this._Config.ServiceFormat.GetHashCode());

            string url = this._Config.ServiceUrl + "/api_v3/index.php?service=";

            if (_MultiRequestReturnType != null)
            {
                url += "multirequest";
                int i = 1;
                foreach (KalturaServiceActionCall call in _CallsQueue)
                {
                    KalturaParams callParams = call.GetParamsForMultiRequest(i);
                    kparams.Add(callParams);
                    KalturaFiles callFiles = call.GetFilesForMultiRequest(i);
                    kfiles.Add(callFiles);
                    i++;
                }

                // map params
                foreach (KeyValuePair <string, string> item in _MultiRequestParamsMap)
                {
                    string requestParam = item.Key;
                    string resultParam  = item.Value;

                    if (kparams.ContainsKey(requestParam))
                    {
                        kparams[requestParam] = resultParam;
                    }
                }
            }
            else
            {
                KalturaServiceActionCall call = _CallsQueue[0];
                url += call.Service + "&action=" + call.Action;
                kparams.Add(call.Params);
                kfiles.Add(call.Files);
            }

            // cleanup
            _CallsQueue.Clear();
            _MultiRequestParamsMap.Clear();

            kparams.Add("kalsig", this.Signature(kparams));

            this.Log("full reqeust url: [" + url + "]");

            // build request
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            if (kfiles.Count == 0)
            {
                request.Timeout = _Config.Timeout;
            }
            else
            {
                request.Timeout = Timeout.Infinite;
            }
            request.Method = "POST";
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.Headers = _Config.RequestHeaders;

            // Add proxy information if required
            createProxy(request, _Config);

            if (kfiles.Count > 0)
            {
                this.PostMultiPartWithFiles(request, kparams, kfiles);
            }
            else
            {
                this.PostUrlEncodedParams(request, kparams);
            }

            // get the response
            using (WebResponse response = request.GetResponse())
            {
                Encoding     enc            = System.Text.Encoding.UTF8;
                StreamReader responseStream = new StreamReader(response.GetResponseStream(), enc);
                string       responseString = responseStream.ReadToEnd();

                this._ResponseHeaders = response.Headers;
                string serverName    = null;
                string serverSession = null;
                for (int i = 0; i < this._ResponseHeaders.Count; ++i)
                {
                    if (this._ResponseHeaders.Keys[i] == "X-Me")
                    {
                        serverName = this._ResponseHeaders[i];
                    }
                    if (this._ResponseHeaders.Keys[i] == "X-Kaltura-Session")
                    {
                        serverSession = this._ResponseHeaders[i];
                    }
                }
                if (serverName != null || serverSession != null)
                {
                    this.Log("server: [" + serverName + "], session: [" + serverSession + "]");
                }

                this.Log("result (serialized): " + responseString);

                DateTime endTime = DateTime.Now;

                this.Log("execution time for [" + url + "]: [" + (endTime - startTime).ToString() + "]");

                XmlDocument xml = new XmlDocument();
                xml.LoadXml(responseString);

                this.ValidateXmlResult(xml);
                XmlElement result = xml["xml"]["result"];
                this.ThrowExceptionOnAPIError(result);

                return(result);
            }
        }