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
var logRecords = ReadLogFile(logFilePath); | |
var grouping = logRecords.GroupBy(l => l.StatusCode); | |
foreach (var group in grouping) | |
{ | |
Console.WriteLine($"StatusCode: {group.Key} - Count: {group.Count()}"); | |
} |
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
var repository = new CustomerRepository(); | |
var id = 1; | |
var existingCustomer = repository.GetCustomerById(id) | |
.Match( | |
some: (foundCustomer) => foundCustomer, | |
none: () => throw new Exception($"Customer with id {id} does not exists"); | |
Console.WriteLine($"Hello customer {existingCustomer.Name}"); |
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
public class CustomerRepository | |
{ | |
public Maybe<Customer> GetCustomerById(int id) | |
{ | |
var customer = SomeDatabaseAbstraction.GetUser(id); | |
return new Maybe<Customer>(customer, customer != null); | |
} | |
} |
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
public class Maybe<T> | |
{ | |
private T Value { get; } | |
private bool IsSome { get; } | |
public Maybe(T value, bool isSome) | |
{ | |
Value = value; | |
IsSome = isSome; |
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
using Flurl.Http; | |
using Polly; | |
using Polly.Retry; | |
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Threading.Tasks; | |
namespace ResilientHttpClient | |
{ |
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
public async Task<T> GetJsonAsync<T>(string url, object headers = null) | |
{ | |
var policy = BuildRetryPolicy(); | |
return await policy.ExecuteAsync(() => url | |
.WithHeaders(headers) | |
.GetJsonAsync<T>() | |
); | |
} |
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
public async Task<T> GetJsonAsync<T>(string url, object headers = null) | |
{ | |
return await url | |
.WithHeaders(headers) | |
.GetJsonAsync<T>(); | |
} |
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
private bool IsTransientError(FlurlHttpException exception) | |
{ | |
int[] httpStatusCodesWorthRetrying = | |
{ | |
(int)HttpStatusCode.RequestTimeout, // 408 | |
(int)HttpStatusCode.BadGateway, // 502 | |
(int)HttpStatusCode.ServiceUnavailable, // 503 | |
(int)HttpStatusCode.GatewayTimeout // 504 | |
}; |
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
private AsyncRetryPolicy BuildRetryPolicy() | |
{ | |
var retryPolicy = Policy | |
.Handle<FlurlHttpException>(IsTransientError) | |
.WaitAndRetryAsync(3, retryAttempt => | |
{ | |
var nextAttemptIn = TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)); | |
Console.WriteLine($"Retry attempt {retryAttempt} to make request. Next try on {nextAttemptIn.TotalSeconds} seconds."); | |
return nextAttemptIn; | |
}); |
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
public class HttpClient | |
{ | |
public HttpClient() | |
{ | |
} | |
} |
NewerOlder