Exemple #1
0
        /// <summary>
        /// Get task data from xml node "task"
        /// </summary>
        private static Task GetTaskInfo(XElement xTask)
        {
            TaskId id = new TaskId(xTask.Attribute("id").Value);
            TaskStatus status = StatusFromString(xTask.Attribute("status").Value);

            Task task = new Task();
            task.Id = id;
            task.Status = status;

            XAttribute xRegistrationTime = xTask.Attribute("registrationTime");
            if (xRegistrationTime != null)
            {
                DateTime time;
                if (DateTime.TryParse(xRegistrationTime.Value, out time))
                    task.RegistrationTime = time;
            }

            XAttribute xStatusChangeTime = xTask.Attribute("statusChangeTime");
            if (xStatusChangeTime != null)
            {
                DateTime time;
                if (DateTime.TryParse(xStatusChangeTime.Value, out time))
                    task.StatusChangeTime = time;
            }

            XAttribute xPagesCount = xTask.Attribute("filesCount");
            if (xPagesCount != null)
            {
                int pagesCount;
                if (Int32.TryParse(xPagesCount.Value, out pagesCount))
                    task.PagesCount = pagesCount;
            }

            XAttribute xCredits = xTask.Attribute("credits");
            if (xCredits != null)
            {
                int credits;
                if( Int32.TryParse( xCredits.Value, out credits ))
                    task.Credits = credits;
            }

            XAttribute xDescription = xTask.Attribute("description");
            if (xDescription != null)
                task.Description = xDescription.Value;

            XAttribute xResultUrl = xTask.Attribute("resultUrl");
            if (xResultUrl != null)
            {
                task.DownloadUrl = xResultUrl.Value;
            }

            return task;
        }
 public Task GetTaskStatus(Task task)
 {
     Task newTask = _restServiceClient.GetTaskStatus(task.Id);
     return newTask;
 }
 public string DownloadResult(Task task)
 {
     return _restServiceClient.DonwloadTextResult(task);
 }
        /// <summary>
        /// Delete task on a server. This function cannot delete tasks that are being processed.
        /// </summary>
        public Task DeleteTask(Task task)
        {
            switch (task.Status)
            {
                case TaskStatus.Deleted:
                case TaskStatus.InProgress:
                case TaskStatus.Unknown:
                    throw new ArgumentException("Invalid task status: " + task.Status + ". Cannot delete");
            }

            string url = String.Format("{0}/deleteTask?TaskId={1}", ServerUrl, Uri.EscapeDataString(task.Id.ToString()));
            WebRequest request = WebRequest.Create(url);
            SetupGetRequest(url, request);

            XDocument response = PerformRequest(request);
            Task serverTask = ServerXml.GetTaskStatus(response);
            return serverTask;
        }
        /// <summary>
        /// Download filePath that has finished processing and save it to given path
        /// </summary>
        /// <param name="task">Id of a task</param>
        /// <param name="outputFile">Path to save a filePath</param>
        public void DownloadResult(Task task, string outputFile)
        {
            if (task.Status != TaskStatus.Completed)
            {
                throw new ArgumentException("Cannot download result for not completed task");
            }

            try
            {
                if (File.Exists(outputFile))
                    File.Delete(outputFile);

                if (task.DownloadUrl == null)
                {
                    throw new ArgumentException("Cannot download task without download url");
                }

                string url = task.DownloadUrl;

                // Emergency code. In normal situations it shouldn't be called
                /*
                url = String.Format("{0}/{1}?TaskId={2}", ServerUrl, _getResultUrl,
                    Uri.EscapeDataString(task.Id.ToString()));
                 */

                WebRequest request = WebRequest.Create(url);
                SetupGetRequest(url, request);

                using (HttpWebResponse result = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = result.GetResponseStream())
                    {
                        // Write result directly to file
                        using (Stream file = File.OpenWrite(outputFile))
                        {
                            CopyStream(stream, file);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                throw new ProcessingErrorException(e.Message, e);
            }
        }
        public string DonwloadTextResult(Task task)
        {
            if (task.Status != TaskStatus.Completed)
            {
                throw new ArgumentException("Cannot download result for not completed task");
            }

            if (task.DownloadUrl == null)
            {
                throw new ArgumentException("Cannot download task without download url");
            }

            try
            {
                string url = task.DownloadUrl;

                WebRequest request = WebRequest.Create(url);
                SetupGetRequest(url, request);

                using (var result = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = result.GetResponseStream())
                    {
                        var reader = new StreamReader(stream);
                        string text = reader.ReadToEnd();
                        return text;
                    }
                }
            }
            catch (WebException e)
            {
                throw new ProcessingErrorException(e.Message, e);
            }
        }