-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Afischbacher/develop
Release v1.5.0
- Loading branch information
Showing
24 changed files
with
2,704 additions
and
954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ public enum DivisionEnum | |
Pacific = 15, | ||
Central = 16, | ||
Atlantic = 17, | ||
Metropolitan = 18, | ||
|
||
Metropolitan = 18 | ||
} | ||
} |
Oops, something went wrong.