-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for generating IServiceCollection extension methods for registering Refit clients #174
Conversation
This commit introduces a new property to RefitGeneratorSettings, 'ServiceCollectionRegistration'. This new setting controls whether generated interfaces are registered in a .NET Core DI container. The setting is included in the 'GenerateCommand' and 'Settings' classes, and can be controlled via the '--service-collection-registration' command line option.
This commit removes the serviceCollectionRegistration setting from both GenerateCommand.cs and Settings.cs. The reason for removal is to simplify the generation process by no longer generating code for registering interfaces with Microsoft.Extensions.DependencyInjection. The decision is due to the fact that such registration should be left to the consuming project, allowing for more flexibility and control.
…rator.GenerateCode()
This commit includes the implementation of a new DependencyInjectionGenerator class which is used to generate Dependency Injection configurations for Refit clients in a service collection. Furthermore, we also added tests to verify the code produced by the generator. This change makes it easier to set up Refit clients with built-in Polly support and multiple HttpMessageHandlers.
DependencyInjectionGenerator class was altered to static. The modification ensures the class has just one instance throughout the application, enhancing the efficiency and minimizing resource usage. This will also avoid potential multithreading issues.
Updated the type of the 'interfaceNames' parameter from IEnumerable<string> to string[] in the Generate method in DependencyInjectionGenerator.cs. Also, an additional condition is added in the if block to check if 'interfaceNames' is empty. This was necessary because the subsequent code execution assumes that 'interfaceNames' contains at least one item and will fail otherwise.
Added functionality to record interface names in Refitter.Core. Now, multiple interface generator and interface generator have been modified to capture interface names during interface generation. This will allow us to use the interface names for further improvements or capabilities in the future.
Added DependencyInjectionGenerator invocation to the RefitGenerator class to support dependency injection setup. Refactored `GenerateCode()` function to make it more legible. Also, extended unit tests in SwaggerPetstoreTests to validate the code generation with dependency injection settings. These changes aim to enhance the code testability and maintainability by introducing dependency injection setup and verifying its correct usage.
In RefitGenerator.cs, the code is improved with increase in code readability by adding a new line before appending new namespace imports to meet the code standard. In ProjectFileContents.cs, new package references have been added to enhance the project functionalities with updated version of DependencyInjection and Polly. In MultipleInterfacesByEndpointWithIoCTests, the newly added file runs specific test scenarios to verify the Multiple Interfaces By Endpoint functionality. In DependencyInjectionGenerator.cs, adjusted the code formatting to reduce redundancy and complexity. Inclusion of certain return statements and new lines are to meet the coding standards. This will help make the code more readable and understandable.
Updated the API endpoints in the tests to support 'delete' operation instead of 'get'. The changes are made to align the tests with the new API specifications where the focus is more on delete operations. Also, a new test is added to check if 'IServiceCollectionExtensions' are generated properly. Module 'RefitGeneratorSettings' was updated to avoid generating contracts during testing.
This commit updates the 'Remove' method in the DependencyInjectionGenerator.cs file to remove two characters instead of three from the end of the 'code' StringBuilder. It then adds a new line to maintain consistent formatting and to ensure that the "return services;" line is properly separated from the preceding lines of code.
Removed the endpoint tests for `IGetFooDetailsEndpoint`, `IGetAllFoosEndpoint`, `IGetBarDetailsEndpoint`, `IGetAllBarsEndpoint` and added new tests for `IDeleteFooEndpoint` and `IDeleteBarEndpoint`. The changes represent a reorientation of testing towards delete functionality instead of get methods. Furthermore, the name of `ServiceCollectionExtensions` was changed to `IServiceCollectionExtensions` in `DependencyInjectionGenerator.cs` for consistency with Interface naming conventions.
Codecov Report
@@ Coverage Diff @@
## main #174 +/- ##
==========================================
+ Coverage 98.37% 98.70% +0.33%
==========================================
Files 41 47 +6
Lines 1353 1626 +273
==========================================
+ Hits 1331 1605 +274
Misses 6 6
+ Partials 16 15 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
You might find this interesting @Noblix |
The changes were made in DependencyInjectionGenerator and associated test case. The changes allow for the base URL to be null, which makes it possible to configure Refit clients without a baseUrl. Earlier, it was mandatory to provide a baseUrl but with these changes, we can configure clients without specifying any baseUrl. These changes provide flexibility in configuring Refit clients and hence improve the usability of the library.
This change allows 'FirstBackoffRetryInSeconds' property in the 'RefitGeneratorSettings' to accept double values, rather than integers only. This is done to provide more precision for backoff retry times.
Modified DependencyInjectionGeneratorTests to include Polly retry parameters. These additions will allow us to test and validate the Polly retry functionality within our Dependency Injection framework.
This commit adds a new test class, DeprecatedMultipleInterfaceByTagEndpointsSkippedTests, which verifies that deprecated endpoints tagged by interfaces are correctly skipped when generating code. This ensures that users do not unintentionally use deprecated API endpoints.
Removed unnecessary white spaces and added a new unit test 'Can_Generate_Without_BaseUrl'. This unit test will validate if Dependency Injection code can generate without providing a BaseUrl. This allows users to provide the BaseUrl at runtime.
Updated README files in a few directories to reflect new settings for dependency injection and Polly configuration. This includes detailed descriptions and example of usage for each new setting such as `baseUrl`, `usePolly`, `pollyMaxRetryCount`, and more. These updates provide users with additional control on HttpClient configuration, retry policies, and service collection configurations. [skip ci]
A new class `TelemetryDelegatingHandler` is added to track outbound HTTP requests. It logs every outbound HTTP request along with its method and URL. It is registered as a transient dependency and will be added to the HTTP handlers via the `AddRefitClient` extension method. Changes have also been made in the `MinimalApi.csproj` and `Program.cs` files to reflect this update. Telemetry can be very useful to gain insights, especially about bottlenecks and to improve performance. Newline issue at the end of the files is also addressed. [skip ci]
This commit adds two new fields to the "dependencyInjectionSettings" in the petstore refitter. 'pollyMaxRetryCount' is added with a value of 3, to specify the maximum number of retries. Also, 'firstBackoffRetryInSeconds' is added with a value of 0.5 to indicate the backoff delay before the first retry. These settings will ensure more reliable request handling by implementing retries in case of failure. [skip ci]
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Use case:
I'm too lazy to setup my Refit clients in using HttpClientFactory and I want all this bootstrapping code written for me
Example OpenAPI spec:
.refitter settings file
Generated code:
Example usage of generated code:
Using HttpMessageHandlers
The changes here also support adding existing
HttpMessageHandler
implementations to the Refit client by specifying"httpMessageHandlers"
it in the .refitter settingsWhich would generate the following:
The generate
IServiceCollection
methods above require that the specifiedHttpMessageHandler
implementations are already registered to the IoC container