public void QueueServiceCall(string service, string action, KalturaParams kparams, KalturaFiles kfiles)
        {
            // in start session partner id is optional (default -1). if partner id was not set, use the one in the config
            if (!kparams.ContainsKey("partnerId"))
            {
                kparams.AddIntIfNotNull("partnerId", this._Config.PartnerId);
            }

            if (kparams["partnerId"] == "-1")
            {
                kparams["partnerId"] = this._Config.PartnerId.ToString();
            }

            kparams.AddStringIfNotNull("ks", this._KS);

            KalturaServiceActionCall call = new KalturaServiceActionCall(service, action, kparams, kfiles);

            this._CallsQueue.Add(call);
        }
        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);
        }
        public XmlElement DoQueue()
        {
            if (_CallsQueue.Count == 0)
            {
                resetRequest();
                return(null);
            }

            DateTime startTime = DateTime.Now;

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

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

            foreach (string param in clientConfiguration.Keys)
            {
                kparams.Add(param, clientConfiguration[param].ToString());
            }
            kparams.AddIfNotNull("format", this._Config.ServiceFormat.GetHashCode());

            string url = this._Config.ServiceUrl + "/api_v3";

            if (_MultiRequestReturnType != null)
            {
                url += "/service/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, IKalturaSerializable> item in _MultiRequestParamsMap)
                {
                    string requestParam = item.Key;
                    IKalturaSerializable resultParam = item.Value;

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

            kparams.Add("kalsig", this.Signature(kparams));
            string json = kparams.ToJson();

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

            // 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;
            request.Accept  = "application/xml";

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

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

            // 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);

                if (!IsMultiRequest)
                {
                    resetRequest();
                }

                return(result);
            }
        }