public async Task <EngineUser> FindById(string id)
        {
            using (var httpClient = new HttpClient())
            {
                SetDefaultHeaders(httpClient);

                var baseUrl = WorkflowSettingSingleTon.WorkflowSetting.BaseUrl;
                var url     = $@"{baseUrl}/identity/users/${id}";
                var resp    = await httpClient.GetAsync(url);

                if ((int)resp.StatusCode == 200)
                {
                    return(await ActivitiEngineClientHelper.Parse <EngineUser>(resp));
                }
                else if ((int)resp.StatusCode == 404)
                {
                    return(await Task.FromResult <EngineUser>(null));
                }
                else
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new Exception(errorxml);
                }
            }
        }
        public async Task <EngineProcessDefinitionGrid> GetProcessDefinitions(ProcessDefinitionsRequest request)
        {
            using (_httpClient = new HttpClient())
            {
                SetDefaultHeaders(_httpClient);

                var baseUrl = WorkflowSettingSingleTon.WorkflowSetting.BaseUrl;
                var url     = $@"{baseUrl}/repository/process-definitions";
                var resp    = await _httpClient.GetAsync(url);

                if ((int)resp.StatusCode == 200)
                {
                    return(await ActivitiEngineClientHelper.ParseProcessDefinitions(resp));
                }
                else if ((int)resp.StatusCode == 400)
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new ActivitiEngineClientException("پارامتر های اشتباه " + errorxml);
                }
                else
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new Exception(errorxml);
                }
            }
        }
        public async Task <EngineInboxTaskList> GetInboxTasks(EngineInboxTaskRequest inboxTaskRequest)
        {
            using (_httpClient = new HttpClient())
            {
                SetDefaultHeaders(_httpClient);

                var baseUrl = WorkflowSettingSingleTon.WorkflowSetting.BaseUrl;
                var url     = $@"{baseUrl}/runtime/tasks" + inboxTaskRequest.ToUrl();
                var resp    = await _httpClient.GetAsync(url);

                if ((int)resp.StatusCode == 200)
                {
                    return(await ActivitiEngineClientHelper.ParseTaskList(resp));
                }
                else if ((int)resp.StatusCode == 400)
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new ActivitiEngineClientException("پارامتر های اشتباه " + errorxml);
                }
                else
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new Exception(errorxml);
                }
            }
        }
        public async Task <EngineProcessDefinition> GetProcessDefinitionById(string processDefinitionId)
        {
            using (_httpClient = new HttpClient())
            {
                SetDefaultHeaders(_httpClient);

                // ایجاد کاربر در engine اگر وجود نداشته باشد
                IsUserExistsOrNotCreateIt();


                var baseUrl = WorkflowSettingSingleTon.WorkflowSetting.BaseUrl;
                var url     = $@"{baseUrl}/repository/process-definitions/{processDefinitionId}";
                var resp    = await _httpClient.GetAsync(url);

                if ((int)resp.StatusCode == 200)
                {
                    return(await ActivitiEngineClientHelper.Parse <EngineProcessDefinition>(resp));
                }
                else if ((int)resp.StatusCode == 400)
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new ActivitiEngineClientException("پارامتر های اشتباه " + errorxml);
                }
                else
                {
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    throw new Exception(errorxml);
                }
            }
        }
        QueryHistoric <T>(string url, QueryHistoricProcessInstanceRequest request)
            where T : BaseWorkflowResponse, new()
        {
            var rContinue = new T();

            var jsonInString = JsonConvert.SerializeObject(request);

            using (_httpClient = new HttpClient())
            {
                SetDefaultHeaders(_httpClient);

                // ایجاد کاربر در engine اگر وجود نداشته باشد
                IsUserExistsOrNotCreateIt();


                var resp = await _httpClient.PostAsync(url,
                                                       new StringContent(jsonInString, Encoding.UTF8, "application/json"));

                if ((int)resp.StatusCode == 200)
                {
                    rContinue.Status = EngineResponseStatus.Success;

                    return(await ActivitiEngineClientHelper.Parse <T>(resp));
                }
                else if ((int)resp.StatusCode == 400)
                {
                    rContinue.Status = EngineResponseStatus.Fail;
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    rContinue.Message = "مقادیر ارسالی اشتباه است یا assignee خالی است"
                                        + errorxml;
                }
                else if ((int)resp.StatusCode == 404)
                {
                    rContinue.Status = EngineResponseStatus.Fail;
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    rContinue.Message = "Task مورد نظر یافت نشد"
                                        + errorxml;
                }
                else if ((int)resp.StatusCode == 409)
                {
                    rContinue.Status = EngineResponseStatus.Fail;
                    var errorxml = await resp.Content.ReadAsStringAsync();

                    rContinue.Message =
                        "Task به دلیل کانفلیکت انجام نمیشود یا Task بطور همزمان آپدیت میشود یا این Task توسط کاربر دیگر در انحصار انجام قرار دارد"
                        + errorxml;
                }
                else
                {
                    throw new ActivitiEngineClientException("این کد رسپانس پیاده سازی نشده");
                }
            }

            return(rContinue);
        }
        private async Task <EngineTask> CheckTaskExists(HttpClient httpClient, WorkflowContinueRequest continueRequest)
        {
            var baseUrl = WorkflowSettingSingleTon.WorkflowSetting.BaseUrl;
            var url     = $@"{baseUrl}/runtime/tasks/{continueRequest.TaskId}";
            var resp    = await httpClient.GetAsync(url);

            if ((int)resp.StatusCode == 200)
            {
                return(this.EngineTask = await ActivitiEngineClientHelper.ParseTask(resp));
            }
            else if ((int)resp.StatusCode == 404)
            {
                throw new ActivitiEngineClientException("این Task با TaskId داده شده یافت نشد");
            }
            else
            {
                var errorxml = await resp.Content.ReadAsStringAsync();

                throw new Exception(errorxml);
            }
        }