private void ValidateArguments(IRequestOptions requestOptions, IResponseOptions responseOptions) { if (requestOptions == null) { var exception = new ArgumentNullException(nameof(requestOptions), "One of required parameters are missing."); _logger.Log(exception, "Exception was thrown."); throw exception; } if (responseOptions == null) { var exception = new ArgumentNullException(nameof(responseOptions), "One of required parameters are missing."); _logger.Log(exception, "Exception was thrown."); throw exception; } if (!requestOptions.IsValid) { var exception = new ArgumentOutOfRangeException(nameof(requestOptions), "One of parameters is not valid."); _logger.Log(exception, "Exception was thrown."); throw exception; } if (!responseOptions.IsValid) { var exception = new ArgumentOutOfRangeException(nameof(responseOptions), "One of parameters is not valid."); _logger.Log(exception, "Exception was thrown."); throw exception; } }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { Response response = null; try { logger.Log($"Send: {requestOptions.Name}"); MainMenu.ConsoleWrite($"Send: {requestOptions.Name}"); response = (Response)await requestHandler.HandleRequestAsync(requestOptions); logger.Log($"Response: {requestOptions.Name} Code:{response.Code}"); MainMenu.ConsoleWrite($"Response: {requestOptions.Name} Code:{response.Code}"); await responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); return(true); } catch (Exception exception) { if (response != null) { response.Handled = false; await responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); } throw new PerformException("", exception); } }
public async Task HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { if (!response.Handled) { return; } await File.WriteAllTextAsync(responseOptions.Path, response.Content); }
public async Task HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { await using var writer = File.CreateText(responseOptions.Path); writer.Write("Status code: " + response.Code + Environment.NewLine); writer.Write("Is handled: " + response.Handled + Environment.NewLine); writer.Write("Content: " + Environment.NewLine); writer.Write(response.Content + Environment.NewLine); writer.Flush(); }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { ValidateArguments(requestOptions, responseOptions); bool result = true; IResponse response; try { _logger.Log("Sending request..."); response = await _requestHandler.HandleRequestAsync(requestOptions); _logger.Log("Response received."); } catch (TaskCanceledException exception) { response = new Response { Handled = false, Code = 504, Content = null }; _logger.Log(exception, "Timeout exception occured when sending request."); result = false; } catch (Exception exception) { if (exception is ArgumentNullException || exception is ArgumentOutOfRangeException || exception is InvalidOperationException || exception is HttpRequestException) { var performException = new PerformException("Critical performer error. See inner exception for more details", exception); _logger.Log(performException, "Exception was thrown."); throw performException; } throw; } bool isSaveSuccess = await SaveResponse(requestOptions, responseOptions, response); result = result && isSaveSuccess; return(result); }
private async Task <bool> SaveResponse(IRequestOptions requestOptions, IResponseOptions responseOptions, IResponse response) { try { await _responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); _logger.Log("Response was saved"); return(true); } catch (ArgumentNullException exception) { _logger.Log(exception, "One of required parameters for response handling are missing. See inner exception for more details"); return(false); } }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync(IRequestOptions requestOptions, IResponseOptions responseOptions) { if (requestOptions == null) { throw new ArgumentNullException(nameof(requestOptions)); } if (!requestOptions.IsValid) { throw new ArgumentOutOfRangeException(nameof(requestOptions)); } if (!responseOptions.IsValid) { throw new ArgumentOutOfRangeException(nameof(responseOptions)); } try { IResponse response; try { response = await _requestHandler.HandleRequestAsync(requestOptions); _logger.Log($"Sending request to {requestOptions.Address}" + $"\nGet response: status code {response.Code}, handled: {response.Handled} content: {response.Content}"); } catch (TimeoutException e) { _logger.Log(e, e.Message); response = new Response(false, (int)HttpStatusCode.RequestTimeout, ""); } await _responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); _logger.Log($"Save request to {responseOptions.Path}"); return(true); } catch (Exception e) { _logger.Log(e, e.Message); throw new PerformException("Something going wrong!", e); } }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { var returnValue = true; IResponse response; try { _logger.Log($"Start request {requestOptions.Name}"); response = await _requestHandler.HandleRequestAsync(requestOptions); } catch (ArgumentNullException ex) { _logger.Log(ex, $"Argument {ex.ParamName} is Null"); return(false); } catch (ArgumentOutOfRangeException ex) { _logger.Log(ex, "Invlaid options element"); return(false); } catch (Exception ex) { _logger.Log(ex, "Create empty response"); response = new Response(false, 0, null); returnValue = false; } try { _logger.Log($"Start response handle\nHandled: {response.Handled}\nCode: {response.Code}"); await _responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); _logger.Log($"Response was handled"); } catch (Exception ex) { _logger.Log($"Response was not handled. Throw PerformException"); throw new PerformException(ex.Message, ex.InnerException); } return(returnValue); }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { var failMessage = $"request {requestOptions.Name} failed!"; try { var response = await _requestHandler.HandleRequestAsync(requestOptions); await _responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); _logger.Log($"request {requestOptions.Name} succesfully performed and saved to {responseOptions.Path}!"); } catch (ArgumentNullException e) { _logger.Log(e, $"{failMessage} Parameter {e.ParamName} is null!"); return(false); } catch (ArgumentOutOfRangeException e) { _logger.Log(e, $"{failMessage} Parameter {e.ParamName} is not valid!"); return(false); } catch (Exception e) { if (e is HttpRequestException || e is TaskCanceledException) { _logger.Log(e, $"Request {requestOptions.Name} is cancelled due to timeout!"); IResponse response = new Response(false, 408, "Request was not performed due to timeout."); await _responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); return(true); } _logger.Log(e, $"Request {requestOptions.Name} is cancelled due to unexpected exception!"); throw new PerformException(e.Message); } return(true); }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { IResponse response = new Response(false, 504, null); logger.Log("Sending request..."); try { response = await requestHandler.HandleRequestAsync(requestOptions); } catch (AggregateException e) { logger.Log(e, "AggregateException was thrown in RequestPerformer"); if (e.InnerException is TaskCanceledException) { logger.Log(e.InnerException, "TaskCanceledException was thrown in RequestPerformer"); return(false); } else { throw new PerformException(e.InnerException.Message + " PerformException was thrown in RequestPerformer", e.InnerException); } } catch (Exception e) { logger.Log(e, e.StackTrace.ToString() + " some Exception was thrown in RequestPerformer"); throw new PerformException(e.Message + e.InnerException + " was thrown in RequestPerformer as PerformException", e); } finally { await responseHandler.HandleResponseAsync(response, requestOptions, responseOptions); } logger.Log("Send request and got response"); return(true); }
async Task IResponseHandler.HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { if (response == null || requestOptions == null) { throw new ArgumentNullException(); } // creating content to file var content = $"Status code: {response.Code}, handled: {response.Handled}" + $"\n{response.Content}"; await File.WriteAllTextAsync(responseOptions.Path, content); }
public Task HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { return(File.WriteAllTextAsync(responseOptions.Path, $"{response.Code} {response.Handled}\n+{response.Content}")); }
public async Task HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { var header = $"Name: {requestOptions.Name}\nIsValid: {requestOptions.IsValid}\n" + $"Handled: {response.Handled}\nCode: {response.Code}"; await File.WriteAllTextAsync(responseOptions.Path, $"{header}\n{response.Content}"); }
public async Task HandleResponseAsync(IResponse response, IRequestOptions requestOptions, IResponseOptions responseOptions) { if (response == null) { throw new ArgumentNullException(nameof(response), "One of required parameters are missing"); } if (requestOptions == null) { throw new ArgumentNullException(nameof(requestOptions), "One of required parameters are missing"); } if (responseOptions == null) { throw new ArgumentNullException(nameof(responseOptions), "One of required parameters are missing"); } string fileHeader; if (response.Handled && response.Code >= 200 && response.Code < 300) { fileHeader = $"{response.Code} Success\n"; } else { fileHeader = $"{response.Code} Fail\n"; } string textToSave = fileHeader + response.Content; await File.WriteAllTextAsync(responseOptions.Path, textToSave); }
/// <inheritdoc/> public async Task <bool> PerformRequestAsync( IRequestOptions requestOptions, IResponseOptions responseOptions) { throw new NotImplementedException(); }