-
Notifications
You must be signed in to change notification settings - Fork 7
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 #17 from bkowalczyk88/master
Proposition of new approach how to handle exceptions in the GetResponseAsync() Method
- Loading branch information
Showing
5 changed files
with
144 additions
and
160 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
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,86 @@ | ||
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<HttpResponse<T>> GetResponseAsync<T>(string endpoint, CancellationToken ct) | ||
{ | ||
var httpResponse = new HttpResponse<T>(); | ||
|
||
try | ||
{ | ||
using var httpClient = new HttpClient {BaseAddress = BaseUrl}; | ||
var returnMessage = await httpClient.GetAsync(endpoint, ct).ConfigureAwait(false); | ||
|
||
httpResponse.StatusCode = returnMessage.StatusCode; | ||
if (returnMessage.IsSuccessStatusCode) | ||
{ | ||
try | ||
{ | ||
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.deserializedObject = JsonSerializer.Deserialize<T>(httpResponse.jsonResponse, options); | ||
Check warning on line 49 in src/Core/HttpRequestHandler.cs GitHub Actions / build
|
||
} | ||
catch (ArgumentNullException ex) | ||
{ | ||
httpResponse.IsError = true; | ||
httpResponse.ExceptionMsg = ex.Message; | ||
httpResponse.AdditionalInformation = "Tried to deserialize from null."; | ||
} | ||
catch (JsonException ex) | ||
{ | ||
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; | ||
} | ||
|
||
|
||
return httpResponse; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
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; | ||
} |
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,14 @@ | ||
using System.Net; | ||
|
||
namespace BaseballSharp.Models; | ||
|
||
public class HttpResponse<T> | ||
{ | ||
public HttpStatusCode StatusCode { get; set; } | ||
public T deserializedObject { get; set; } = default!; | ||
public string jsonResponse { get; set; } = default!; | ||
|
||
public bool IsError { get; set; } | ||
public string AdditionalInformation { get; set; } = default!; | ||
public string ExceptionMsg { get; set; } = default!; | ||
} |