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 2 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
172 changes: 91 additions & 81 deletions Src/VTEX.Health/VtexHealthClient.cs
Original file line number Diff line number Diff line change
@@ -1,81 +1,91 @@
// ***********************************************************************
// 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
private readonly IHttpClientFactory _httpClientFactory;

public VtexHealthClient(IHttpClientFactory httpClientFactory)

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The modifier 'public' is not valid for this item

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Identifier expected

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

The modifier 'public' is not valid for this item

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Identifier expected
{

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

} expected

Check failure on line 8 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
{
/// <summary>
/// The logger
/// </summary>
private readonly ILogger<VtexHealthClient> _logger;

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

Check failure on line 43 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 43 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 43 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>
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
}
}

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

View workflow job for this annotation

GitHub Actions / Deep Source Coverage report

Type or namespace definition, or end-of-file expected
Loading
Loading