private IChromelyResponse SaveMovies(IChromelyRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (request.PostData == null) { throw new Exception("Post data is null or invalid."); } var response = new ChromelyResponse(request.Id); var postDataJson = _serializerUtil.ObjectToJson(request.PostData); var options = new JsonSerializerOptions(); options.ReadCommentHandling = JsonCommentHandling.Skip; options.AllowTrailingCommas = true; var movies = JsonSerializer.Deserialize <List <MovieInfo> >(postDataJson, options); var rowsReceived = movies != null ? movies.Count : 0; response.Data = $"{DateTime.Now}: {rowsReceived} rows of data successfully saved."; return(response); }
public string Execute(string path, object parameters = null, object postData = null) { IChromelyResponse response = null; bool isRequestAsync = _routeProvider.IsActionRouteAsync(path); if (isRequestAsync) { var task = Task.Run(async() => { return(await _requestTaskRunner.RunAsync(path, parameters.ToDictionary(), postData)); }); task.Wait(); response = task.Result; } else { response = _requestTaskRunner.Run(path, parameters.ToDictionary(), postData); } return(response == null ? string.Empty : _serializerUtil.ObjectToJson(response)); }
protected override bool ProcessRequest(CefRequest request, CefCallback callback) { var isSchemeRegistered = _requestSchemeProvider?.IsSchemeRegistered(request.Url); if (isSchemeRegistered.HasValue && isSchemeRegistered.Value) { var uri = new Uri(request.Url); var path = uri.LocalPath; bool isRequestAsync = _routeProvider.IsActionRouteAsync(path); if (isRequestAsync) { ProcessRequestAsync(path); } else { ProcessRequest(path); } return(true); } else { _chromelyResponse = _chromelyErrorHandler.HandleRouteNotFound(request.Identifier.ToString(), request.Url); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } callback.Dispose(); return(false); #region Process Request void ProcessRequest(string path) { Task.Run(() => { using (callback) { try { if (string.IsNullOrWhiteSpace(path)) { _chromelyResponse = _chromelyErrorHandler.HandleRouteNotFound(request.Identifier.ToString(), path); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } else { var parameters = request.Url.GetParameters(request.ReferrerURL); var postData = GetPostData(request); var jsonRequest = _serializerUtil.ObjectToJson(request); _chromelyResponse = _requestTaskRunner.Run(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } } catch (Exception exception) { var chromelyRequest = new ChromelyRequest() { Id = request.Identifier.ToString(), RouteUrl = request.Url }; _chromelyResponse = _chromelyErrorHandler.HandleError(chromelyRequest, exception); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } finally { callback.Continue(); } } }); } #endregion #region Process Request Async void ProcessRequestAsync(string path) { Task.Run(async() => { using (callback) { try { if (string.IsNullOrWhiteSpace(path)) { _chromelyResponse = _chromelyErrorHandler.HandleRouteNotFound(request.Identifier.ToString(), path); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } else { var parameters = request.Url.GetParameters(request.ReferrerURL); var postData = GetPostData(request); var jsonRequest = _serializerUtil.ObjectToJson(request); _chromelyResponse = await _requestTaskRunner.RunAsync(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } } catch (Exception exception) { var chromelyRequest = new ChromelyRequest() { Id = request.Identifier.ToString(), RouteUrl = request.Url }; _chromelyResponse = _chromelyErrorHandler.HandleError(chromelyRequest, exception); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } finally { callback.Continue(); } } }); } #endregion }
protected override bool ProcessRequest(CefRequest request, CefCallback callback) { var isSchemeRegistered = _requestSchemeProvider?.IsSchemeRegistered(request.Url); if (isSchemeRegistered.HasValue && isSchemeRegistered.Value) { var uri = new Uri(request.Url); var path = uri.LocalPath; bool isRequestAsync = _routeProvider.IsActionRouteAsync(path); if (isRequestAsync) { ProcessRequestAsync(path); } else { ProcessRequest(path); } return(true); } Logger.Instance.Log.LogWarning($"Url {request.Url} is not of a registered custom scheme."); callback.Dispose(); return(false); #region Process Request void ProcessRequest(string path) { Task.Run(() => { using (callback) { try { var response = new ChromelyResponse(); if (string.IsNullOrEmpty(path)) { response.ReadyState = (int)ReadyState.ResponseIsReady; response.Status = (int)HttpStatusCode.BadRequest; response.StatusText = "Bad Request"; _chromelyResponse = response; } else { var parameters = request.Url.GetParameters(); var postData = GetPostData(request); var jsonRequest = _serializerUtil.ObjectToJson(request); _chromelyResponse = _requestTaskRunner.Run(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } } catch (Exception exception) { Logger.Instance.Log.LogError(exception, exception.Message); _chromelyResponse = new ChromelyResponse { Status = (int)HttpStatusCode.BadRequest, Data = "An error occured." }; } finally { callback.Continue(); } } }); } #endregion #region Process Request Async void ProcessRequestAsync(string path) { Task.Run(async() => { using (callback) { try { var response = new ChromelyResponse(); if (string.IsNullOrEmpty(path)) { response.ReadyState = (int)ReadyState.ResponseIsReady; response.Status = (int)System.Net.HttpStatusCode.BadRequest; response.StatusText = "Bad Request"; _chromelyResponse = response; } else { var parameters = request.Url.GetParameters(); var postData = GetPostData(request); var jsonRequest = _serializerUtil.ObjectToJson(request); _chromelyResponse = await _requestTaskRunner.RunAsync(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string jsonData = _serializerUtil.EnsureResponseDataIsJson(_chromelyResponse.Data); _responseBytes = Encoding.UTF8.GetBytes(jsonData); } } catch (Exception exception) { Logger.Instance.Log.LogError(exception, exception.Message); _chromelyResponse = new ChromelyResponse { Status = (int)HttpStatusCode.BadRequest, Data = "An error occured." }; } finally { callback.Continue(); } } }); } #endregion }