Skip to content

Commit

Permalink
Merge pull request #17 from bkowalczyk88/master
Browse files Browse the repository at this point in the history
Proposition of new approach how to handle exceptions in the GetResponseAsync() Method
  • Loading branch information
markjamesm authored Nov 28, 2023
2 parents b946bf9 + 31bd5cc commit 6957986
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 160 deletions.
2 changes: 1 addition & 1 deletion samples/BaseballSharpCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
86 changes: 86 additions & 0 deletions src/Core/HttpRequestHandler.cs
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

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 49 in src/Core/HttpRequestHandler.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
}
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;
}
}
159 changes: 0 additions & 159 deletions src/Enums/eTeamId.cs

This file was deleted.

43 changes: 43 additions & 0 deletions src/Enums/eTeamIdEnum.cs
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;
}
14 changes: 14 additions & 0 deletions src/Models/HttpResponse.cs
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!;
}

0 comments on commit 6957986

Please sign in to comment.