Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Note: this is based on my question on SO and subsequent activity there I am trying to add resilience using Polly to HTTP calls that are performed using Refit library. I am using the Refit factory...
#1: Initial revision
How to define Polly policies when working with Refit library in ASP.NET Core?
Note: this is based on my question on SO and subsequent activity [there](https://stackoverflow.com/q/74055782/2780791) I am trying to add resilience using [Polly ](https://github.com/App-vNext/Polly)to HTTP calls that are performed using [Refit library](https://github.com/reactiveui/refit). I am using the Refit factory method `RestService.For<>` and I have a working code, but I feel it is way more complex than it should be: ### The refit interface ```c# [Headers("Authorization: Bearer")] public interface IBarIntegration { [Get("/api/ext/Foo/GetFooBriefInfo")] Task<ApiResponse<GetFooBriefInfoForFooDto>> GetFooBriefInfo(GetFooBriefInfoForFooInputDto inputData); } ``` ### Factory class that also configures the authentication ```c# internal sealed class BarApiClientHelper : IBarApiClientHelper { private readonly IOptionsSnapshot<BarApiSettings> _BarSettings; private readonly IAccessTokenHelperService _accessTokenHelperService; public BarApiClientHelper(IOptionsSnapshot<BarApiSettings> BarSettings, IAccessTokenHelperService accessTokenHelperService) { _BarSettings = BarSettings; _accessTokenHelperService = accessTokenHelperService; } public async Task<TApiClient> CreateApiClient<TApiClient>(CancellationToken token) { string baseUrl = _BarSettings.Value.BaseUrl; string accessToken = await _accessTokenHelperService.GetAccessToken(token); var refitClient = RestService.For<TApiClient>(baseUrl, new RefitSettings { AuthorizationHeaderValueGetter = () => Task.FromResult(accessToken) }); return refitClient; } } ``` ### Example call ```c# public async Task<GetFooBriefInfoForFooDto> GetFooBriefInfo(string upn, CancellationToken token) { var apiClient = await _clientHelper.CreateApiClient<IBarIntegration>(token); var retrialPolicy = _registry.Get<AsyncRetryPolicy<IApiResponse>>(DefaultRetrialPolicy); var func = async () => (IApiResponse) await apiClient.GetFooBriefInfo(new GetFooBriefInfoForFooInputDto { FooContactUpn = upn }); var FooInfo = (ApiResponse<GetFooBriefInfoForFooDto>) await retrialPolicy.ExecuteAsync(func); await FooInfo.EnsureSuccessStatusCodeAsync(); return FooInfo.Content!; } ```