From 4b0a4039ae94947342524fd2f1fdeae066bcf69d Mon Sep 17 00:00:00 2001 From: BKowalczyk Date: Sun, 13 Mar 2022 08:18:14 +0100 Subject: [PATCH 1/3] initial change --- samples/BaseballSharpCli/Program.cs | 2 +- src/Core/HttpRequestHandler.cs | 48 +++++++++ src/Enums/eTeamId.cs | 159 ---------------------------- src/Enums/eTeamIdEnum.cs | 43 ++++++++ src/Models/HttpResponse.cs | 13 +++ 5 files changed, 105 insertions(+), 160 deletions(-) create mode 100644 src/Core/HttpRequestHandler.cs delete mode 100644 src/Enums/eTeamId.cs create mode 100644 src/Enums/eTeamIdEnum.cs create mode 100644 src/Models/HttpResponse.cs diff --git a/samples/BaseballSharpCli/Program.cs b/samples/BaseballSharpCli/Program.cs index a4daac9..5e352e7 100644 --- a/samples/BaseballSharpCli/Program.cs +++ b/samples/BaseballSharpCli/Program.cs @@ -33,7 +33,7 @@ private static async Task Main(string[] args) } // Example of casting the team ids enum to int in the parameter. - var teamRoster = await mlbClient.GetTeamRosterAsync((int)eTeamId.BlueJays, 2021, DateTime.Now, rosterType.rosterFull); + var teamRoster = await mlbClient.GetTeamRosterAsync(eTeamIdEnum.BlueJays.Id, 2021, DateTime.Now, rosterType.rosterFull); foreach (var team in teamRoster) { diff --git a/src/Core/HttpRequestHandler.cs b/src/Core/HttpRequestHandler.cs new file mode 100644 index 0000000..474751e --- /dev/null +++ b/src/Core/HttpRequestHandler.cs @@ -0,0 +1,48 @@ +using System; +using System.Net.Http; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading; +using System.Threading.Tasks; +using BaseballSharp.Models; + +namespace BaseballSharp.Core; + +public class HttpRequestHandler +{ + private static readonly Uri baseUrl = new("https://statsapi.mlb.com/api/v1"); + public async Task> GetResponseAsync(string endpoint, CancellationToken ct) + { + var httpResponse = new HttpResponse(); + + try + { + using (var httpClient = new HttpClient() {BaseAddress = baseUrl}) + { + var returnMessage = await httpClient.GetAsync(endpoint, ct).ConfigureAwait(false); + httpResponse.StatusCode = returnMessage.StatusCode; + if (returnMessage.IsSuccessStatusCode) + { + var options = new JsonSerializerOptions() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + }; + httpResponse.jsonResponse = await returnMessage.Content.ReadAsStringAsync(ct); + httpResponse.deserializedObject = JsonSerializer.Deserialize(httpResponse.jsonResponse, options); + } + else + { + httpResponse.AdditionalInformation = "Not expected incorrect response code - not 200"; + } + } + } + catch (Exception ex) + { + httpResponse.AdditionalInformation = "Unknown internal error"; + httpResponse.ExceptionMsg = ex.Message; + } + + + return httpResponse; + } +} \ No newline at end of file diff --git a/src/Enums/eTeamId.cs b/src/Enums/eTeamId.cs deleted file mode 100644 index d5a478f..0000000 --- a/src/Enums/eTeamId.cs +++ /dev/null @@ -1,159 +0,0 @@ -namespace BaseballSharp.Enums; - -/// -/// A list of all team ids in enum format. This can be used -/// to cast as an int to pass to functions that require a -/// teamId as a parameter. -/// -public enum eTeamId -{ - /// - /// 108 - /// - Angels = 108, - - /// - /// 109 - /// - DBacks = 109, - - /// - /// 110 - /// - Orioles = 110, - - /// - /// 111 - /// - RedSox = 111, - - /// - /// 112 - /// - Cubs = 112, - - /// - /// 113 - /// - Reds = 113, - - /// - /// 114 - /// - Indians = 114, - - /// - /// 115 - /// - Rockies = 115, - - /// - /// 116 - /// - Tigers = 116, - - /// - /// 117 - /// - Astros = 117, - - /// - /// 118 - /// - Royals = 118, - - /// - /// 119 - /// - Dodgers = 119, - - /// - /// 120 - /// - Nationals = 120, - - /// - /// 121 - /// - Mets = 121, - - /// - /// 133 - /// - Athletics = 133, - - /// - /// 134 - /// - Pirates = 134, - - /// - /// 135 - /// - Padres = 135, - - /// - /// 136 - /// - Mariners = 136, - - /// - /// 137 - /// - Giants = 137, - - /// - /// 138 - /// - Cardinals = 138, - - /// - /// 139 - /// - Rays = 139, - - /// - /// 140 - /// - Rangers = 140, - - /// - /// 141 - /// - BlueJays = 141, - - /// - /// 142 - /// - Twins = 142, - - /// - /// 143 - /// - Phillies = 143, - - /// - /// 144 - /// - Braves = 144, - - /// - /// 145 - /// - WhiteSox = 145, - - /// - /// 146 - /// - Marlins = 146, - - /// - /// 147 - /// - Yankees = 147, - - /// - /// 158 - /// - Brewers = 158 -} \ No newline at end of file diff --git a/src/Enums/eTeamIdEnum.cs b/src/Enums/eTeamIdEnum.cs new file mode 100644 index 0000000..f7d27ac --- /dev/null +++ b/src/Enums/eTeamIdEnum.cs @@ -0,0 +1,43 @@ +namespace BaseballSharp.Enums; + +public sealed class eTeamIdEnum +{ + public static eTeamIdEnum Angels { get; } = new(108, "Angels") ; + public static eTeamIdEnum DBacks { get; } = new(109, "DBacks") ; + public static eTeamIdEnum Orioles { get; } = new(110, "Orioles") ; + public static eTeamIdEnum RedSox { get; } = new(111, "RedSox") ; + public static eTeamIdEnum Cubs { get; } = new(112, "Cubs") ; + public static eTeamIdEnum Reds { get; } = new(113, "Reds") ; + public static eTeamIdEnum Indians { get; } = new(114, "Indians") ; + public static eTeamIdEnum Rockies { get; } = new(115, "Rockies") ; + public static eTeamIdEnum Tigers { get; } = new(116, "Tigers") ; + public static eTeamIdEnum Astros { get; } = new(117, "Astros") ; + public static eTeamIdEnum Royals { get; } = new(118, "Royals") ; + public static eTeamIdEnum Dodgers { get; } = new(119, "Dodgers") ; + public static eTeamIdEnum Nationals { get; } = new(120, "Nationals") ; + public static eTeamIdEnum Mets { get; } = new(121, "Mets") ; + public static eTeamIdEnum Athletics { get; } = new(133, "Athletics") ; + public static eTeamIdEnum Pirates { get; } = new(134, "Pirates") ; + public static eTeamIdEnum Padres { get; } = new(135, "Padres") ; + public static eTeamIdEnum Mariners { get; } = new(136, "Mariners") ; + public static eTeamIdEnum Giants { get; } = new(137, "Giants") ; + public static eTeamIdEnum Cardinals { get; } = new(138, "Cardinals") ; + public static eTeamIdEnum Rays { get; } = new(139, "Rays") ; + public static eTeamIdEnum Rangers { get; } = new(140, "Rangers") ; + public static eTeamIdEnum BlueJays { get; } = new(141, "BlueJays") ; + public static eTeamIdEnum Twins { get; } = new(142, "Twins") ; + public static eTeamIdEnum Phillies { get; } = new(143, "Phillies") ; + public static eTeamIdEnum Braves { get; } = new(144, "Braves") ; + public static eTeamIdEnum WhiteSox { get; } = new(145, "WhiteSox") ; + public static eTeamIdEnum Marlins { get; } = new(146, "Marlins") ; + public static eTeamIdEnum Yankees { get; } = new(147, "Yankees") ; + public static eTeamIdEnum Brewers { get; } = new(158, "Brewers") ; + private eTeamIdEnum(int id, string teamName) + { + Id = id; + TeamName = teamName; + } + + public readonly int Id; + public readonly string TeamName; +} \ No newline at end of file diff --git a/src/Models/HttpResponse.cs b/src/Models/HttpResponse.cs new file mode 100644 index 0000000..8b4c9c1 --- /dev/null +++ b/src/Models/HttpResponse.cs @@ -0,0 +1,13 @@ +using System.Net; + +namespace BaseballSharp.Models; + +public class HttpResponse +{ + public HttpStatusCode StatusCode { get; set; } + public T? deserializedObject { get; set; } + public string jsonResponse { get; set; } + + public string AdditionalInformation { get; set; } + public string ExceptionMsg { get; set; } +} \ No newline at end of file From 2940ac3a04cdd1282e80ea0aec24b853affe9e34 Mon Sep 17 00:00:00 2001 From: BKowalczyk Date: Sun, 13 Mar 2022 08:23:36 +0100 Subject: [PATCH 2/3] Expanded json serializer option to make deserialization less sensitive for malformed jsons. --- src/Core/HttpRequestHandler.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Core/HttpRequestHandler.cs b/src/Core/HttpRequestHandler.cs index 474751e..4874784 100644 --- a/src/Core/HttpRequestHandler.cs +++ b/src/Core/HttpRequestHandler.cs @@ -25,7 +25,10 @@ public async Task> GetResponseAsync(string endpoint, Cancella { var options = new JsonSerializerOptions() { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + NumberHandling = JsonNumberHandling.AllowReadingFromString, + IncludeFields = true, + PropertyNameCaseInsensitive = true }; httpResponse.jsonResponse = await returnMessage.Content.ReadAsStringAsync(ct); httpResponse.deserializedObject = JsonSerializer.Deserialize(httpResponse.jsonResponse, options); From 31bd5ccdf4f639bf93a5d51b9af8a56c8e3b8db1 Mon Sep 17 00:00:00 2001 From: BKowalczyk Date: Fri, 18 Mar 2022 17:18:12 +0100 Subject: [PATCH 3/3] Http Request Handler --- src/Core/HttpRequestHandler.cs | 53 ++++++++++++++++++++++++++++------ src/Models/HttpResponse.cs | 9 +++--- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Core/HttpRequestHandler.cs b/src/Core/HttpRequestHandler.cs index 4874784..7229587 100644 --- a/src/Core/HttpRequestHandler.cs +++ b/src/Core/HttpRequestHandler.cs @@ -10,37 +10,72 @@ namespace BaseballSharp.Core; public class HttpRequestHandler { - private static readonly Uri baseUrl = new("https://statsapi.mlb.com/api/v1"); + private static readonly Uri BaseUrl = new("https://statsapi.mlb.com/api/v1/"); public async Task> GetResponseAsync(string endpoint, CancellationToken ct) { var httpResponse = new HttpResponse(); try { - using (var httpClient = new HttpClient() {BaseAddress = baseUrl}) + using var httpClient = new HttpClient {BaseAddress = BaseUrl}; + var returnMessage = await httpClient.GetAsync(endpoint, ct).ConfigureAwait(false); + + httpResponse.StatusCode = returnMessage.StatusCode; + if (returnMessage.IsSuccessStatusCode) { - var returnMessage = await httpClient.GetAsync(endpoint, ct).ConfigureAwait(false); - httpResponse.StatusCode = returnMessage.StatusCode; - if (returnMessage.IsSuccessStatusCode) + try { - var options = new JsonSerializerOptions() + httpResponse.jsonResponse = await returnMessage.Content.ReadAsStringAsync(ct); + } + catch (Exception ex) + { + httpResponse.jsonResponse = ""; + httpResponse.IsError = true; + httpResponse.ExceptionMsg = ex.Message; + httpResponse.AdditionalInformation = "Error during reading response as string from http response content"; + return httpResponse; + } + + + try + { + var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, NumberHandling = JsonNumberHandling.AllowReadingFromString, IncludeFields = true, PropertyNameCaseInsensitive = true }; - httpResponse.jsonResponse = await returnMessage.Content.ReadAsStringAsync(ct); httpResponse.deserializedObject = JsonSerializer.Deserialize(httpResponse.jsonResponse, options); } - else + catch (ArgumentNullException ex) + { + httpResponse.IsError = true; + httpResponse.ExceptionMsg = ex.Message; + httpResponse.AdditionalInformation = "Tried to deserialize from null."; + } + catch (JsonException ex) { - httpResponse.AdditionalInformation = "Not expected incorrect response code - not 200"; + httpResponse.IsError = true; + httpResponse.ExceptionMsg = ex.Message; + httpResponse.AdditionalInformation = "Error during deserializing response to object from json sting"; } + catch (Exception ex) + { + httpResponse.IsError = true; + httpResponse.ExceptionMsg = ex.Message; + httpResponse.AdditionalInformation = "Unexpected error"; + } + } + else + { + httpResponse.IsError = true; + httpResponse.AdditionalInformation = "Not expected incorrect response code - received no 200"; } } catch (Exception ex) { + httpResponse.IsError = true; httpResponse.AdditionalInformation = "Unknown internal error"; httpResponse.ExceptionMsg = ex.Message; } diff --git a/src/Models/HttpResponse.cs b/src/Models/HttpResponse.cs index 8b4c9c1..fabe292 100644 --- a/src/Models/HttpResponse.cs +++ b/src/Models/HttpResponse.cs @@ -5,9 +5,10 @@ namespace BaseballSharp.Models; public class HttpResponse { public HttpStatusCode StatusCode { get; set; } - public T? deserializedObject { get; set; } - public string jsonResponse { get; set; } + public T deserializedObject { get; set; } = default!; + public string jsonResponse { get; set; } = default!; - public string AdditionalInformation { get; set; } - public string ExceptionMsg { get; set; } + public bool IsError { get; set; } + public string AdditionalInformation { get; set; } = default!; + public string ExceptionMsg { get; set; } = default!; } \ No newline at end of file