public TopFiveViewModel() { WebHookResponse = new WebHookResponse(); TopFiveGroups = new List <TopFiveGroupsViewModel>(); TopFiveUsers = new List <TopFiveUsersViewModel>(); UserLevel = new List <UserLevel>(); }
/// <summary> /// Create webhook with given newModel. /// </summary> /// <param name="newModel">Model to update webhoook</param> /// <returns>Creates webhook data model - WebHookResponse</returns> public WebHookResponse CreateWebHook(WebHook newModel) { WebHookResponse result = null; var url = $"/api/v1/webhooks"; var request = new RestRequest(url, Method.POST); request.AddJsonBody(newModel); var response = Execute <WebHookResponse>(request); if (response.StatusCode == HttpStatusCode.OK && string.Equals(response.Content, NullDataContent, StringComparison.OrdinalIgnoreCase)) { result = null; } else if (response.ErrorException != null || response.StatusCode != HttpStatusCode.OK) { var message = $"Ceate webhook failed. Status code:{response.StatusCode}; Error:{response.ErrorMessage}"; throw new Exception(message); } ; result = response.Data; return(result); }
public async Task Post([FromBody] WebHookResponse body) { if (body == null || String.IsNullOrWhiteSpace(body.TaskId)) { return; } var taskCall = await _client.GetAsync($"{body.TaskId}"); var taskContent = await taskCall.Content.ReadAsStringAsync(); var task = JsonSerializer.Deserialize <ClickUpTask>(taskContent); var prediction = _predictionEnginePool.Predict(new TaskInput { TaskName = task.Name }); var maxPrediction = prediction.Score.Max(); if (maxPrediction > 0.75) { await _client.PostAsync($"{body.TaskId}/tag/{prediction.PredictedLabel}", new StringContent("")); _logger.LogInformation($"Update on task ID {body.TaskId} with tag {prediction.PredictedLabel} and score {maxPrediction}"); } else { _logger.LogInformation($"No update due to low score of {maxPrediction}"); } }
/// <summary> /// Delete WebHook by Id /// </summary> public void DeleteWebHook(Guid id) { WebHookResponse result = null; var url = $"/api/v1/webhooks/{id}"; var request = new RestRequest(url, Method.DELETE); var response = Execute(request); if (response.ErrorException != null || response.StatusCode != HttpStatusCode.NoContent) { var message = $"Delete webhook failed. Status code:{response.StatusCode}; Error:{response.ErrorMessage}"; throw new Exception(message); } ; }
public void RaveWebHook() { string res = ""; try { res = new StreamReader(HttpContext.Request.Body).ReadToEnd(); WebHookResponse response = JsonConvert.DeserializeObject <WebHookResponse>(res); _logger.Information("Webhook-->" + res); var orderGuid = Guid.Parse(response.txRef); Order order = _orderService.GetOrderByGuid(orderGuid); if (order != null && response.status == "successful") { order.PaymentStatus = PaymentStatus.Paid; order.OrderNotes.Add(new OrderNote { DisplayToCustomer = false, CreatedOnUtc = DateTime.Now, Note = "Order paid! Status: " + response.status + " Ref: " + response.orderRef + " Amount: " + response.charged_amount + " Customer: " + response.customer + " Created: " + response.createdAt }); _orderService.UpdateOrder(order); } else { order.PaymentStatus = PaymentStatus.Voided; order.OrderNotes.Add(new OrderNote { DisplayToCustomer = false, CreatedOnUtc = DateTime.Now, Note = "Payment voided! Status: " + response.status + " Ref: " + response.orderRef + " Amount: " + response.charged_amount }); _orderService.UpdateOrder(order); } } catch (Exception ex) { _logger.Error("Webhook error: " + res, ex); } }
private static void WorkWithWebHooks(OgoShipApi api) { List <WebHookResponse> webHooksList = api.GetWebHooks(); var newHook = new WebHook() { Url = "test", Key = "testKey", Type = WebhookType.OrderShipped }; WebHookResponse createdWebhook = api.CreateWebHook(newHook); WebHookResponse webhook = api.GetWebHook(createdWebhook.Id); webhook.Key = "TestTestTestKey"; WebHookResponse updatedWebhook = api.UpdateWebHook(createdWebhook.Id, webhook); webHooksList = api.GetWebHooks(); api.DeleteWebHook(webhook.Id); webHooksList = api.GetWebHooks(); }
/// <summary> /// Get webHook by id /// </summary> /// <returns>WebHookResponse model</returns> public WebHookResponse GetWebHook(Guid id) { WebHookResponse result = null; var url = $"/api/v1/webhooks/{id}"; var request = new RestRequest(url, Method.GET); var response = Execute <WebHookResponse>(request); if (response.StatusCode == HttpStatusCode.OK && string.Equals(response.Content, NullDataContent, StringComparison.OrdinalIgnoreCase)) { result = null; } else if (response.ErrorException != null || response.StatusCode != HttpStatusCode.OK) { var message = $"Get webhook by Id failed. Status code:{response.StatusCode}; Error:{response.ErrorMessage}"; throw new Exception(message); } ; result = response.Data; return(result); }
public IActionResult AhaMoveCallback([FromBody] WebHookResponse responseData) { Logger.WriteLog(Logger.LogType.Info, "Start AhaMoveCallback: " + JsonConvert.SerializeObject(responseData)); if (responseData == null || string.IsNullOrEmpty(responseData.DeliveryOrderId)) { Logger.WriteLog(Logger.LogType.Error, "Data post null or DeliveryOrderId empty"); return(BadRequest()); } /*WebHookResponse responseData; * try * { * responseData = JsonConvert.DeserializeObject<WebHookResponse>(value); * } * catch (Exception ex) * { * Logger.WriteLog(Logger.LogType.Error, ex.ToString()); * return BadRequest(); * }*/ var orderInfo = _orderBo.GetByDeliveryOrderId(responseData.DeliveryOrderId); // If order is not found in DB if (orderInfo == null) { Logger.WriteLog(Logger.LogType.Error, "Order from delivery API not found"); return(BadRequest()); } // If current order is invalid if (orderInfo.status != (short)OrderStatusEnum.KitchenDone && orderInfo.status != (short)OrderStatusEnum.Delivering) { Logger.WriteLog(Logger.LogType.Error, string.Format("Order with ID {0} has status '{1}' is not 'Delivering'.", orderInfo.order_id, orderInfo.status)); return(BadRequest()); } // Change to new delivery status var changeDeliveryStatus = CoreUtils.GetValueFromDescription <DeliveryStatus>(responseData.Status); if (changeDeliveryStatus == DeliveryStatus.Default || (short)changeDeliveryStatus == orderInfo.delivery_status) { Logger.WriteLog(Logger.LogType.Error, string.Format("Order with ID {0} hasn't change deleviry status.", orderInfo.order_id, orderInfo.status)); return(Ok()); } // If CANCELLED status DeliveryCancelStatus cancelStatus; ErrorCodes result; switch (changeDeliveryStatus) { // Khi hoàn thành vận chuyển, cần kiểm tra trạng thái chính xác là Hoàn thành hay Bị hủy case DeliveryStatus.Completed: OrderStatusEnum status; string reason; if (responseData.Paths.Count > 1 && responseData.Paths[1].Status == CompleteStatus) { status = OrderStatusEnum.Success; reason = "Delivery SUCCESS"; } else { status = OrderStatusEnum.Failure; reason = "Delivery FAILED"; } result = _orderBo.UpdateCompleteStatus(orderInfo.order_id, status, changeDeliveryStatus, reason, Modifier); break; case DeliveryStatus.Cancelled: Logger.WriteLog(Logger.LogType.Info, responseData.CancelComment); if (responseData.CancelByUser) { cancelStatus = DeliveryCancelStatus.Customer; } else if (responseData.CancelComment == CommentStatus) { cancelStatus = DeliveryCancelStatus.Timeout; } else { cancelStatus = DeliveryCancelStatus.Supplier; } result = _orderBo.UpdateDeliveryStatus(orderInfo.order_id, (short)changeDeliveryStatus, (short)cancelStatus, Modifier); break; default: result = _orderBo.UpdateDeliveryStatus(orderInfo.order_id, (short)changeDeliveryStatus, 0, Modifier); break; } dynamic response; if (result == ErrorCodes.Success) { response = new { _id = responseData.DeliveryOrderId, status = "SUCCESS" }; } else { response = new { _id = responseData.DeliveryOrderId, status = "FAILURE" }; } Logger.WriteLog(Logger.LogType.Info, "Finish AhaMoveCallback successfully"); return(Ok(response)); }