public IChromelyResponse Execute(string requestId, string routePath, IDictionary <string, object>?parameters, object?postData, string?requestData) { if (string.IsNullOrWhiteSpace(routePath)) { return(_chromelyErrorHandler.HandleRouteNotFound(requestId, routePath)); } if (routePath.ToLower().Equals("/info")) { return(_chromelyInfo.GetInfo(requestId)); } var route = _routeProvider.GetRoute(routePath); if (route is null) { throw new Exception($"Route for path = {routePath} is null or invalid."); } return(ExecuteRoute(requestId, routePath, parameters, postData, requestData)); }
/// <summary> /// The process request async. /// </summary> /// <param name="request"> /// The request. /// </param> /// <param name="callback"> /// The callback. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public override CefReturnValue ProcessRequestAsync(CefRequest request, CefCallback callback) { var scheme = _requestSchemeProvider?.GetScheme(request.Url); if (scheme is not null && scheme.UrlSchemeType == UrlSchemeType.LocalRequest) { _stream = Stream.Null; var uri = new Uri(request.Url); var path = uri.LocalPath; _mimeType = "application/json"; bool isRequestAsync = _routeProvider.IsRouteAsync(path); if (isRequestAsync) { ProcessRequestAsync(path); } else { ProcessRequest(path); } } return(CefReturnValue.ContinueAsync); #region Process Request void ProcessRequest(string path) { Task.Run(() => { using (callback) { try { var response = new ChromelyResponse(); if (string.IsNullOrEmpty(path)) { _chromelyResponse = _chromelyErrorHandler.HandleRouteNotFound(request.Identifier.ToString(), path); } else { var parameters = request.Url.GetParameters(); var postData = GetPostData(request); var jsonRequest = _dataTransferOptions.ConvertObjectToJson(request); _chromelyResponse = _requestHandler.Execute(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string?jsonData = _dataTransferOptions.ConvertResponseToJson(_chromelyResponse.Data); if (jsonData is not null) { var content = Encoding.UTF8.GetBytes(jsonData); _stream = new MemoryStream(); _stream.Write(content, 0, content.Length); } } } catch (Exception exception) { _stream = Stream.Null; var chromelyRequest = new ChromelyRequest() { Id = request.Identifier.ToString(), RouteUrl = request.Url }; _chromelyResponse = _chromelyErrorHandler.HandleError(chromelyRequest, exception); } if (_stream is null) { callback.Cancel(); } else { SetResponseInfoOnSuccess(); 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)) { _chromelyResponse = _chromelyErrorHandler.HandleRouteNotFound(request.Identifier.ToString(), path); } else { var parameters = request.Url.GetParameters(request.ReferrerURL); var postData = GetPostData(request); var jsonRequest = _dataTransferOptions.ConvertObjectToJson(request); _chromelyResponse = await _requestHandler.ExecuteAsync(request.Identifier.ToString(), path, parameters, postData, jsonRequest); string?jsonData = _dataTransferOptions.ConvertResponseToJson(_chromelyResponse.Data); if (jsonData is not null) { var content = Encoding.UTF8.GetBytes(jsonData); _stream = new MemoryStream(); _stream.Write(content, 0, content.Length); } } } catch (Exception exception) { _stream = Stream.Null; var chromelyRequest = new ChromelyRequest() { Id = request.Identifier.ToString(), RouteUrl = request.Url }; _chromelyResponse = _chromelyErrorHandler.HandleError(chromelyRequest, exception); } if (_stream is null) { callback.Cancel(); } else { SetResponseInfoOnSuccess(); 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); } 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 }
public IChromelyResponse Run(string routeUrl, IDictionary <string, string> parameters, object postData) { if (string.IsNullOrWhiteSpace(routeUrl)) { return(_chromelyErrorHandler.HandleRouteNotFound(string.Empty, routeUrl)); } if (routeUrl.ToLower().Equals("/info")) { return(_chromelyInfo?.GetInfo(string.Empty)); } var route = _routeProvider.GetActionRoute(routeUrl); if (route == null) { throw new Exception($"Route for path = {routeUrl} is null or invalid."); } return(ExecuteRoute(string.Empty, routeUrl, parameters, postData, string.Empty)); }