-
-
Notifications
You must be signed in to change notification settings - Fork 50
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 #174 from christianhelle/ioc-registration
Add support for generating IServiceCollection extension methods for registering Refit clients
- Loading branch information
Showing
30 changed files
with
791 additions
and
53 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,95 @@ | ||
using System.Text; | ||
|
||
namespace Refitter.Core; | ||
|
||
public static class DependencyInjectionGenerator | ||
{ | ||
public static string Generate( | ||
RefitGeneratorSettings settings, | ||
string[] interfaceNames) | ||
{ | ||
var iocSettings = settings.DependencyInjectionSettings; | ||
if (iocSettings is null || !interfaceNames.Any()) | ||
return string.Empty; | ||
|
||
var code = new StringBuilder(); | ||
|
||
var methodDeclaration = string.IsNullOrEmpty(iocSettings.BaseUrl) | ||
? "public static IServiceCollection ConfigureRefitClients(this IServiceCollection services, Uri baseUrl)" | ||
: "public static IServiceCollection ConfigureRefitClients(this IServiceCollection services)"; | ||
|
||
var configureRefitClient = string.IsNullOrEmpty(iocSettings.BaseUrl) | ||
? ".ConfigureHttpClient(c => c.BaseAddress = baseUrl)" | ||
: $".ConfigureHttpClient(c => c.BaseAddress = new Uri(\"{iocSettings.BaseUrl}\"))"; | ||
|
||
var usings = iocSettings.UsePolly | ||
? """ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Polly; | ||
using Polly.Contrib.WaitAndRetry; | ||
using Polly.Extensions.Http; | ||
""" | ||
: """ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
"""; | ||
|
||
code.AppendLine(); | ||
code.AppendLine(); | ||
code.AppendLine( | ||
$$"""" | ||
namespace {{settings.Namespace}} | ||
{ | ||
{{usings}} | ||
public static partial class IServiceCollectionExtensions | ||
{ | ||
{{methodDeclaration}} | ||
{ | ||
""""); | ||
foreach (var interfaceName in interfaceNames) | ||
{ | ||
code.Append( | ||
$$""" | ||
services | ||
.AddRefitClient<{{interfaceName}}>() | ||
{{configureRefitClient}} | ||
"""); | ||
|
||
foreach (string httpMessageHandler in iocSettings.HttpMessageHandlers) | ||
{ | ||
code.AppendLine(); | ||
code.Append($" .AddHttpMessageHandler<{httpMessageHandler}>()"); | ||
} | ||
|
||
if (iocSettings.UsePolly) | ||
{ | ||
code.AppendLine(); | ||
code.Append( | ||
$$""" | ||
.AddPolicyHandler( | ||
HttpPolicyExtensions | ||
.HandleTransientHttpError() | ||
.WaitAndRetryAsync( | ||
Backoff.DecorrelatedJitterBackoffV2( | ||
TimeSpan.FromSeconds({{iocSettings.FirstBackoffRetryInSeconds}}), | ||
{{iocSettings.PollyMaxRetryCount}})) | ||
"""); | ||
} | ||
|
||
code.Append(");"); | ||
code.AppendLine(); | ||
code.AppendLine(); | ||
} | ||
|
||
code.Remove(code.Length - 2, 2); | ||
code.AppendLine(); | ||
code.AppendLine(" return services;"); | ||
code.AppendLine(" }"); | ||
code.AppendLine(" }"); | ||
code.AppendLine("}"); | ||
code.AppendLine(); | ||
return code.ToString(); | ||
} | ||
} |
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,14 @@ | ||
namespace Refitter.Core; | ||
|
||
public record RefitGeneratedCode( | ||
string SourceCode, | ||
params string[] InterfaceNames) | ||
{ | ||
public string SourceCode { get; } = SourceCode; | ||
public string[] InterfaceNames { get; } = InterfaceNames; | ||
|
||
public override string ToString() | ||
{ | ||
return SourceCode; | ||
} | ||
} |
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
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
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
Oops, something went wrong.