Skip to content

Instantly share code, notes, and snippets.

@mrtank
Created December 16, 2019 09:38
Show Gist options
  • Save mrtank/fe312eecc1ded16df50de5e0138b01f6 to your computer and use it in GitHub Desktop.
Save mrtank/fe312eecc1ded16df50de5e0138b01f6 to your computer and use it in GitHub Desktop.
public abstract class ApiCaller
{
...
public TRecord Get< TRecord >( string url )
{
TRecord item = default( TRecord );
try
{
var response = GetHttpClient( ).GetAsync( $"{url}" ).Result;
EnsureSuccessStatusCode( response );
item = response.Content.ReadAsAsync<TRecord>( ).Result;
}
catch ( Exception ex )
{
LogAndThrowException( ex );
}
return item;
}
...
public partial class DataAccess : IDALXYInterface
{
// usually calling from that
public List<SomeDTO> GetSomething( long id )
{
return DataAccessHelper.HandleCalling( NLog.LogManager.GetCurrentClassLogger( ), ( ) =>
{
return new XYAPICaller( ).Get<List<SomeDTO>>( "someUrl" );
} );
}
...
public static TResult HandleCalling< TResult >( NLog.ILogger logger, Func<TResult> func,
[System.Runtime.CompilerServices.CallerMemberName]
string callerName = null )
{
try
{
logger?.Trace( "[Start] {0}", callerName );
var actionResult = func( );
return actionResult;
}
catch ( ServiceReferenceException )
{
...
...
// that sneaky one left out of DataAccessHelper.HandleCalling. Also it's not clear if it's left out of DataAccessHelper.HandleCalling,
// because I have to check inside GetXY, if the wrapping is inside there...
return new XYAPICaller().GetXY( url );
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment