Beispiel #1
0
        public async Task CanMakePostRequests()
        {
            Fitting fitting = new Fitting
            {
                ApiBasePath   = "https://localhost:5001",
                RequestSuffix = "/api/offers",
                ContentType   = "application/json",
                Method        = "POST"
            };

            FittingResponse <object> response = await fitting.SendRequest <object>();

            Assert.IsNotNull(response);
            Assert.IsTrue(response.Status.Health == FittingResponseStatusHealth.Good);
        }
Beispiel #2
0
        public async Task CanMakeGetRequests()
        {
            Fitting fitting = new Fitting
            {
                ApiBasePath   = "https://xkcd.com",
                RequestSuffix = "/info.0.json",
                ContentType   = "application/json",
                Method        = "GET"
            };

            FittingResponse <object> response = await fitting.SendRequest <object>();

            Assert.IsNotNull(response);
            Assert.IsTrue(response.Status.Health == FittingResponseStatusHealth.Good);
        }
Beispiel #3
0
        /// <summary>
        /// Initiate the HTTP request asynchronously
        /// </summary>
        public async Task <FittingResponse <T> > SendRequest <T>() where T : class
        {
            ValidateParameters();

            FittingResponse <T> fittingResponse = PrepareNewResponse <T>();

            try
            {
                WebRequest webRequest = await PrepareWebRequest();

                WebResponse response = await webRequest.GetResponseAsync();

                Stream dataStream = response.GetResponseStream();

                if (dataStream != null)
                {
                    StreamReader reader             = new StreamReader(dataStream);
                    string       responseFromServer = reader.ReadToEnd();

                    //JToken json = JToken.Parse(responseFromServer);
                    T result = JsonConvert.DeserializeObject <T>(responseFromServer);

                    reader.Close();
                    dataStream.Close();
                    response.Close();

                    fittingResponse.Status.Health = FittingResponseStatusHealth.Good;
                    fittingResponse.Result        = result;

                    _logger?.LogInformation(
                        $"Fitting successfully received a response from {webRequest.RequestUri.ToString()}");
                }
            }
            catch (WebException webException)
            {
                fittingResponse.ExceptionThrown = webException;

                StringBuilder errorLogBuilder = new StringBuilder();

                switch (webException.Status)
                {
                case WebExceptionStatus.Timeout:
                    errorLogBuilder.Append($"Fitting SendRequest threw a WebException due to a timeout");
                    break;

                case WebExceptionStatus.SecureChannelFailure:
                    errorLogBuilder.Append($"Fitting SendRequest threw a WebException due to an SSL/TLS error");
                    break;

                case WebExceptionStatus.ProtocolError:
                    int statusCode = (int)((HttpWebResponse)webException.Response).StatusCode;
                    errorLogBuilder.Append($"Fitting SendRequest threw a WebException with status code {statusCode}");
                    break;

                default:
                    errorLogBuilder.Append($"Fitting SendRequest threw a WebException due to an unknown error");
                    break;
                }

                errorLogBuilder.AppendLine(
                    $"{Environment.NewLine}{webException.Message}{Environment.NewLine}{webException.StackTrace}");

                fittingResponse.Status.Health = FittingResponseStatusHealth.Bad;

                _logger?.LogError(
                    $"{errorLogBuilder}");
            }
            catch (Exception exception)
            {
                fittingResponse.ExceptionThrown = exception;

                fittingResponse.Status.Health = FittingResponseStatusHealth.Bad;

                _logger?.LogError(
                    $"Fitting SendRequest threw an Exception {Environment.NewLine}{exception.Message}{Environment.NewLine}{exception.StackTrace}");
            }

            fittingResponse.Status.ResponseUtcDateTime = DateTime.UtcNow;
            return(fittingResponse);
        }