示例#1
0
        /// <summary>
        /// Thực thi post api tới web quảng cáo
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public ResponseProductStatusModel postAPI(CronJobProductStatu schedule)
        {
            ResponseProductStatusModel result = null;

            try
            {
                var client = new HttpClient();
                client.BaseAddress = new Uri(schedule.Web);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // HTTP POST
                var content = new FormUrlEncodedContent(new[] {
                    new KeyValuePair <string, string>("key", "828327"),
                    new KeyValuePair <string, string>("sku", schedule.SKU),
                    new KeyValuePair <string, string>("visibility", schedule.IsHidden ? "exclude-from-catalog" : "visible")
                });

                var response = client.PostAsync(schedule.API, content).Result;

                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsAsync <ResponseProductStatusModel>().Result;
                }
                else
                {
                    result = new ResponseProductStatusModel()
                    {
                        response = "error",
                        content  = "HTTP status: " + response.StatusCode.ToString()
                    };
                }
            }
            catch (ThreadAbortException ex)
            {
                Thread.ResetAbort();
                _log.Error("Post API for website", ex);
                postAPI(schedule);
            }
            catch (Exception ex)
            {
                _log.Error("Post API for website", ex);
                result = new ResponseProductStatusModel()
                {
                    response = "error",
                    content  = ex.Message.Length > 255 ? ex.Message.Substring(0, 254) : ex.Message
                };
            }

            return(result);
        }
示例#2
0
        public static CronJobProductStatu postScheduleProductStatus(CronJobProductStatu scheduleNew)
        {
            try
            {
                using (var con = new inventorymanagementEntities())
                {
                    con.CronJobProductStatus.Add(scheduleNew);
                    con.SaveChanges();

                    return(scheduleNew);
                }
            }
            catch (Exception ex)
            {
                Log.Instance().Error("Insert the schedule new", ex);
                throw;
            }
        }
示例#3
0
        /// <summary>
        /// Dựa vào kết quả trả về API
        /// Cập nhật lại cho trạng thái schedule
        /// </summary>
        /// <param name="schedule"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public CronJobProductStatu checkResponse(CronJobProductStatu schedule, ResponseProductStatusModel response)
        {
            if (response == null)
            {
                schedule.Status       = (int)CronJobStatus.Fail;
                schedule.Note         = "Đã xãy ra lỗi khi gọi thực thi api";
                schedule.ModifiedDate = DateTime.Now;
            }
            else if (response.response == "done")
            {
                schedule.Status       = (int)CronJobStatus.Done;
                schedule.Note         = response.response;
                schedule.ModifiedDate = DateTime.Now;
            }
            else if (response.response == "notfound")
            {
                schedule.Status       = (int)CronJobStatus.Continue;
                schedule.Note         = response.response;
                schedule.ModifiedDate = DateTime.Now;
            }
            else if (response.response == "error")
            {
                schedule.Status       = (int)CronJobStatus.Fail;
                schedule.Note         = response.response;
                schedule.ModifiedDate = DateTime.Now;
            }
            else
            {
                schedule.Status  = (int)CronJobStatus.Fail;
                response.content = "Trường hợp success không có trong định nghĩa. " + response.content;
                if (response.content.Length > 255)
                {
                    schedule.Note = response.content.Substring(0, 254);
                }
                else
                {
                    schedule.Note = response.content;
                }
                schedule.ModifiedDate = DateTime.Now;
            }

            return(schedule);
        }