public KalturaServiceActionCall(string service, string action, KalturaParams kparams, KalturaFiles kfiles) { this._Service = service; this._Action = action; this._Params = kparams; this._Files = kfiles; }
private void PostMultiPartWithFiles(HttpWebRequest request, KalturaParams kparams, KalturaFiles kfiles) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); request.ContentType = "multipart/form-data; boundary=" + boundary; // use a memory stream because we don't know the content length of the request when we have multiple files MemoryStream memStream = new MemoryStream(); byte[] buffer; int bytesRead = 0; StringBuilder sb = new StringBuilder(); sb.Append("--" + boundary + "\r\n"); foreach (KeyValuePair<string, string> param in kparams) { sb.Append("Content-Disposition: form-data; name=\"" + param.Key + "\"" + "\r\n"); sb.Append("\r\n"); sb.Append(param.Value); sb.Append("\r\n--" + boundary + "\r\n"); } buffer = Encoding.UTF8.GetBytes(sb.ToString()); memStream.Write(buffer, 0, buffer.Length); foreach (KeyValuePair<string, FileStream> file in kfiles) { sb = new StringBuilder(); FileStream fileStream = file.Value; sb.Append("Content-Disposition: form-data; name=\"" + file.Key + "\"; filename=\"" + Path.GetFileName(fileStream.Name) + "\"" + "\r\n"); sb.Append("Content-Type: application/octet-stream" + "\r\n"); sb.Append("\r\n"); // write the current string builder content buffer = Encoding.UTF8.GetBytes(sb.ToString()); memStream.Write(buffer, 0, buffer.Length); // write the file content buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) memStream.Write(buffer, 0, bytesRead); buffer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); memStream.Write(buffer, 0, buffer.Length); } request.ContentLength = memStream.Length; Stream requestStream = request.GetRequestStream(); // write the memorty stream to the request stream memStream.Seek(0, SeekOrigin.Begin); buffer = new Byte[checked((uint)Math.Min(4096, (int)memStream.Length))]; bytesRead = 0; while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); requestStream.Close(); memStream.Close(); }
public void QueueServiceCall(string service, string action, string fallbackClass, 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); if (_MultiRequestReturnType != null) _MultiRequestReturnType.Add(fallbackClass); this._CallsQueue.Add(call); }
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; } }
public KalturaFiles GetFilesForMultiRequest(int multiRequestNumber) { KalturaFiles multiRequestParams = new KalturaFiles(); foreach (KeyValuePair<string, FileStream> param in this._Files) { multiRequestParams.Add(multiRequestNumber + ":" + param.Key, param.Value); } return multiRequestParams; }
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; }
private void PostMultiPartWithFiles(HttpWebRequest request, string json, KalturaFiles kfiles) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); request.ContentType = "multipart/form-data; boundary=" + boundary; byte[] paramsBuffer = BuildMultiPartParamsBuffer(json, boundary); SortedList<string, MultiPartFileDescriptor> filesDescriptions = new SortedList<string, MultiPartFileDescriptor>(); foreach (KeyValuePair<string, FileStream> file in kfiles) filesDescriptions.Add(file.Key, BuildMultiPartFileDescriptor(file, boundary)); // Set content's length request.ContentLength = paramsBuffer.LongLength; foreach (KeyValuePair<string, MultiPartFileDescriptor> fileDesc in filesDescriptions) request.ContentLength += fileDesc.Value.GetTotalLength(); // And let's upload request.AllowWriteStreamBuffering = false; // A more sensible approach may be relevant Stream requestStream = request.GetRequestStream(); requestStream.Write(paramsBuffer, 0, paramsBuffer.Length); foreach (KeyValuePair<string, MultiPartFileDescriptor> fileDesc in filesDescriptions) { requestStream.Write(fileDesc.Value._Header, 0, fileDesc.Value._Header.Length); byte[] buffer = new Byte[checked(Math.Min((uint)4096, fileDesc.Value._Stream.Length))]; int bytesRead = 0; while ((bytesRead = fileDesc.Value._Stream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); requestStream.Write(fileDesc.Value._Footer, 0, fileDesc.Value._Footer.Length); } requestStream.Close(); }
public void QueueServiceCall(string service, string action, string fallbackClass, KalturaParams kparams, KalturaFiles kfiles) { Object value; foreach (string param in requestConfiguration.Keys) { value = requestConfiguration[param]; if (value is KalturaObjectBase) { kparams.Add(param, ((KalturaObjectBase)value).ToParams()); } else { kparams.Add(param, value.ToString()); } } KalturaServiceActionCall call = new KalturaServiceActionCall(service, action, kparams, kfiles); if (_MultiRequestReturnType != null) _MultiRequestReturnType.Add(fallbackClass); this._CallsQueue.Add(call); }