Skip to content

Commit

Permalink
Merge pull request #16 from Afischbacher/develop
Browse files Browse the repository at this point in the history
Release v1.5.0
  • Loading branch information
Afischbacher authored Nov 26, 2021
2 parents 397c369 + 2f24212 commit b357ae2
Show file tree
Hide file tree
Showing 24 changed files with 2,704 additions and 954 deletions.
163 changes: 100 additions & 63 deletions Nhl.Api.Common/Http/NhlApiHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,108 @@
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Nhl.Api.Common.Http
{
public interface INhlApiHttpClient
{
/// <summary>
/// Performs a HTTP GET request
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>The deserialized JSON payload of the generic type</returns>
Task<T> GetAsync<T>(string route) where T : class, new();

HttpClient HttpClient { get; }
}

public class NhlApiHttpClient : INhlApiHttpClient
{
private readonly object _lock = new object();
private static HttpClient _httpClient = new HttpClient();
public NhlApiHttpClient(string clientApiUri, string clientVersion, int timeoutInSeconds = 30)
{
Client = clientApiUri;
ClientVersion = clientVersion;
Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
}

/// <summary>
/// The HTTP Client for the NHL API
/// </summary>
public virtual HttpClient HttpClient { get; }

/// <summary>
/// The timeout for HTTP requests for the NHL API
/// </summary>
public TimeSpan Timeout { get; private set; }

/// <summary>
/// The client version for HTTP requests for the NHL API
/// </summary>
public string ClientVersion { get; private set; }

/// <summary>
/// The official client for the NHL API
/// </summary>
public string Client { get; private set; }

/// <summary>
/// Performs a HTTP GET request with a generic argument as the model or type to be returned
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>The deserialized JSON payload of the generic type</returns>
public async Task<T> GetAsync<T>(string route) where T : class, new()
{
if (string.IsNullOrWhiteSpace(route))
{
throw new ArgumentNullException(nameof(route));
}

var httpResponseMessage = await HttpClient.GetAsync($"{HttpClient.BaseAddress}{route}");

var contentResponse = await httpResponseMessage.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<T>(contentResponse);

}
}
public interface INhlApiHttpClient
{
/// <summary>
/// Performs a HTTP GET request
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>The deserialized JSON payload of the generic type</returns>
Task<T> GetAsync<T>(string route) where T : class, new();

/// <summary>
/// Performs a HTTP GET request and returns a byte array
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>A byte array payload from the HTTP GET request</returns>
Task<byte[]> GetByteArrayAsync(string route);

/// <summary>
/// The HTTP Client for the NHL API
/// </summary>
HttpClient HttpClient { get; }

/// <summary>
/// The official client for the NHL API
/// </summary>
string Client { get; }

/// <summary>
/// The client version for HTTP requests for the NHL API
/// </summary>
string ClientVersion { get; }
}

public abstract class NhlApiHttpClient : INhlApiHttpClient
{
public NhlApiHttpClient(string clientApiUri, string clientVersion, int timeoutInSeconds = 30)
{
ServicePointManager.ReusePort = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

Client = clientApiUri;
ClientVersion = clientVersion;
Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
}

/// <summary>
/// The HTTP Client for the NHL API
/// </summary>
public virtual HttpClient HttpClient { get; }

/// <summary>
/// The timeout for HTTP requests for the NHL API
/// </summary>
public TimeSpan Timeout { get; private set; }

/// <summary>
/// The client version for HTTP requests for the NHL API
/// </summary>
public string ClientVersion { get; private set; }

/// <summary>
/// The official client for the NHL API
/// </summary>
public string Client { get; private set; }

/// <summary>
/// Performs a HTTP GET request with a generic argument as the model or type to be returned
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>The deserialized JSON payload of the generic type</returns>
public async Task<T> GetAsync<T>(string route) where T : class, new()
{
if (string.IsNullOrWhiteSpace(route))
{
throw new ArgumentNullException(nameof(route));
}

using (var httpResponseMessage = await HttpClient.GetAsync($"{HttpClient.BaseAddress}{route}"))
{
var contentResponse = await httpResponseMessage.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<T>(contentResponse);
}
}

/// <summary>
/// Performs a HTTP GET request and returns a byte array
/// </summary>
/// <param name="route">The NHL API endpoint</param>
/// <returns>A byte array payload from the HTTP GET request</returns>
public async Task<byte[]> GetByteArrayAsync(string route)
{
if (string.IsNullOrWhiteSpace(route))
{
throw new ArgumentNullException(nameof(route));
}

return await HttpClient.GetByteArrayAsync($"{HttpClient.BaseAddress}{route}");
}
}
}
43 changes: 43 additions & 0 deletions Nhl.Api.Common/Http/NhlStaticAssetsApiHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Net.Http;

namespace Nhl.Api.Common.Http
{
/// <summary>
/// The dedicated NHL static assets HTTP Client for the NHL API
/// </summary>
public class NhlStaticAssetsApiHttpClient : NhlApiHttpClient
{
private static readonly object _lock = new object();
private static HttpClient _httpClient;
public static string ClientApiUrl;

public NhlStaticAssetsApiHttpClient() : base(clientApiUri: "https://www-league.nhlstatic.com", clientVersion: string.Empty, timeoutInSeconds: 30)
{
ClientApiUrl = Client;
}

/// <summary>
/// The HTTP client for the NHL static assets API
/// </summary>
public override HttpClient HttpClient
{
get
{
lock (_lock)
{
if (_httpClient == null)
{
_httpClient = new HttpClient
{
BaseAddress = new Uri($"{Client}{ClientVersion}"),
Timeout = Timeout
};
}

return _httpClient;
}
}
}
}
}
2 changes: 1 addition & 1 deletion Nhl.Api.Common/Http/NhlStatsApiHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Nhl.Api.Common.Http
public class NhlStatsApiHttpClient : NhlApiHttpClient
{
private static readonly object _lock = new object();
private HttpClient _httpClient;
private static HttpClient _httpClient;
public NhlStatsApiHttpClient() : base(clientApiUri: "https://statsapi.web.nhl.com/api/", clientVersion: "v1", timeoutInSeconds: 30)
{
}
Expand Down
3 changes: 1 addition & 2 deletions Nhl.Api.Common/Http/NhlSuggestionApiHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ namespace Nhl.Api.Common.Http
/// <summary>
/// The dedicated NHL HTTP Client for the NHL suggestion API
/// </summary>

public class NhlSuggestionApiHttpClient : NhlApiHttpClient
{
private static readonly object _lock = new object();
private HttpClient _httpClient;
private static HttpClient _httpClient;
public NhlSuggestionApiHttpClient() : base(clientApiUri: "https://suggest.svc.nhl.com/svc/suggest/", clientVersion: "v1", timeoutInSeconds: 30)
{
}
Expand Down
2 changes: 1 addition & 1 deletion Nhl.Api.Common/Nhl.Api.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.4.0</Version>
<Version>1.5.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion Nhl.Api.Domain/Enumerations/Award/AwardEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public enum AwardEnum
MarkMessierNHLLeadershipAward = 22,
JimGregoryGeneralManageroftheYearAward = 23,
EJMcGuireAwardofExcellence = 24,
WillieOReeCommunityHeroAward = 25,
WillieOReeCommunityHeroAward = 25
}
}
3 changes: 1 addition & 2 deletions Nhl.Api.Domain/Enumerations/Division/DivisionEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public enum DivisionEnum
Pacific = 15,
Central = 16,
Atlantic = 17,
Metropolitan = 18,

Metropolitan = 18
}
}
Loading

0 comments on commit b357ae2

Please sign in to comment.