Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: [FEATURE] Refactor to use HttpClientFactory for HTTP client management #355

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 90 additions & 81 deletions Src/VTEX.Health/VtexHealthClient.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,90 @@
// ***********************************************************************
// Assembly : VTEX.Health
// Author : Guilherme Branco Stracini
// Created : 01-15-2023
//
// Last Modified By : Guilherme Branco Stracini
// Last Modified On : 01-15-2023
// ***********************************************************************
// <copyright file="VtexHealthClient.cs" company="Guilherme Branco Stracini">
// © 2020 Guilherme Branco Stracini. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace VTEX.Health
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

/// <summary>
/// Class VtexHealthClient.
/// Implements the <see cref="VTEX.Health.IVtexHealthClient" />
/// </summary>
/// <seealso cref="VTEX.Health.IVtexHealthClient" />
public class VtexHealthClient : IVtexHealthClient
{
/// <summary>
/// The logger
/// </summary>
private readonly ILogger<VtexHealthClient> _logger;

/// <summary>
/// The HTTP client
/// </summary>
private readonly HttpClient _httpClient;

#region ~ctors

/// <summary>
/// Initializes a new instance of the <see cref="VtexHealthClient" /> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="httpClient">The HTTP client.</param>
/// <exception cref="System.ArgumentNullException">loggerFactory</exception>
/// <exception cref="System.ArgumentNullException">httpClient</exception>
public VtexHealthClient(ILoggerFactory loggerFactory, HttpClient httpClient)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}

_logger = loggerFactory.CreateLogger<VtexHealthClient>();
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

#endregion

#region Implementation of IVtexHealthClient

/// <inheritdoc />
public async Task<IEnumerable<PlatformStatus>> GetPlatformStatuesAsync(
CancellationToken cancellationToken
)
{
_logger.LogDebug("Getting platform status");
var response = await _httpClient.GetAsync("/", cancellationToken).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
_logger.LogDebug($"Platform status response: {response.StatusCode}");
return response.IsSuccessStatusCode
? JsonConvert.DeserializeObject<PlatformStatus[]>(responseContent)
: default;
}

#endregion
}
}
// ***********************************************************************
// Assembly : VTEX.Health
// Author : Guilherme Branco Stracini
// Created : 01-15-2023
{
//
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}

_logger = loggerFactory.CreateLogger<VtexHealthClient>();
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));

Check failure on line 13 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

} expected
// Last Modified By : Guilherme Branco Stracini
// Last Modified On : 01-15-2023
// ***********************************************************************
// <copyright file="VtexHealthClient.cs" company="Guilherme Branco Stracini">
// © 2020 Guilherme Branco Stracini. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace VTEX.Health
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

/// <summary>
/// Class VtexHealthClient.
/// Implements the <see cref="VTEX.Health.IVtexHealthClient" />
/// </summary>
/// <seealso cref="VTEX.Health.IVtexHealthClient" />
public class VtexHealthClient : IVtexHealthClient

Check warning on line 37 in Src/VTEX.Health/VtexHealthClient.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/VTEX.Health/VtexHealthClient.cs#L37

This class can't be instantiated; make its constructor 'public'.
{
/// <summary>
/// The logger
/// </summary>
private readonly ILogger<VtexHealthClient> _logger;

/// <summary>
/// The HTTP client
/// </summary>
_httpClientFactory = httpClientFactory;

Check failure on line 47 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token '=' in class, record, struct, or interface member declaration

Check failure on line 47 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token ';' in class, record, struct, or interface member declaration

#region ~ctors


private HttpClient CreateClient()
{
/// <summary>
/// Initializes a new instance of the <see cref="VtexHealthClient" /> class.
return _httpClientFactory.CreateClient();
}
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="httpClient">The HTTP client.</param>
/// <exception cref="System.ArgumentNullException">loggerFactory</exception>
/// <exception cref="System.ArgumentNullException">httpClient</exception>

_logger = loggerFactory.CreateLogger<VtexHealthClient>();

Check failure on line 64 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token '=' in class, record, struct, or interface member declaration

Check failure on line 64 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token '(' in class, record, struct, or interface member declaration

Check failure on line 64 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Tuple must contain at least two elements.

Check failure on line 64 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token ';' in class, record, struct, or interface member declaration
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));

Check failure on line 65 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token '=' in class, record, struct, or interface member declaration

Check failure on line 65 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Invalid token '??' in class, record, struct, or interface member declaration

Check failure on line 65 in Src/VTEX.Health/VtexHealthClient.cs

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Identifier expected
}

#endregion

#region Implementation of IVtexHealthClient

/// <inheritdoc />
public async Task<IEnumerable<PlatformStatus>> GetPlatformStatuesAsync(
CancellationToken cancellationToken
)
{
_logger.LogDebug("Getting platform status");
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

Check warning on line 78 in Src/VTEX.Health/VtexHealthClient.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/VTEX.Health/VtexHealthClient.cs#L78

Remove this useless assignment to local variable 'responseContent'.
_logger.LogDebug($"Platform status response: {response.StatusCode}");
return response.IsSuccessStatusCode
var httpClient = _httpClientFactory.CreateClient();
? JsonConvert.DeserializeObject<PlatformStatus[]>(responseContent)
: default;
var response = await httpClient.GetAsync("/", cancellationToken).ConfigureAwait(false);

Check warning on line 84 in Src/VTEX.Health/VtexHealthClient.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Src/VTEX.Health/VtexHealthClient.cs#L84

Remove this useless assignment to local variable 'response'.
}

#endregion
}
}
}
10 changes: 10 additions & 0 deletions Src/VTEX/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddScoped<IVtexHealthClient, VtexHealthClient>();
services.AddScoped<VTEXWrapper>();
}

}

Check notice on line 10 in Src/VTEX/Startup.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/VTEX/Startup.cs#L10

A closing brace should not be preceded by a blank line. (SA1508)
Loading
Loading