diff --git a/src/RestSharp.Extensions.DependencyInjection/README.md b/src/RestSharp.Extensions.DependencyInjection/README.md new file mode 100644 index 000000000..498c5fac2 --- /dev/null +++ b/src/RestSharp.Extensions.DependencyInjection/README.md @@ -0,0 +1,45 @@ +# About + +The `RestSharp.Extensions.DependencyInjection` library provides integration with `Microsoft.Extensions.DependencyInjection` components as well as integrates with `IHttpClientFactory`. + +# How to use + +Use the extension method provided by the package to configure the client: + +```csharp +// Add a default client with no base URL, with default options +services.AddRestClient(); + +// Add a client with a base URL +services.AddRestClient(new Uri("https://example.com")); + +// Add a client with a base URL and custom options +services.AddRestClient(options => +{ + options.BaseUrl = new Uri("https://example.com"); + options.Timeout = TimeSpan.FromSeconds(30); +}); +``` + +When the above registrations are used, the `IRestClient` interface can be injected into any class. + +In addition, the package supports registering named clients: + +```csharp +services.AddRestClient("my-client", options => +{ + options.BaseUrl = new Uri("https://example.com"); + options.Timeout = TimeSpan.FromSeconds(30); +}); +``` + +When the above registrations are used, resolving the client instance should be done using the `IRestClientFactory`: + +```csharp +public class MyClass(IRestClientFactory restClientFactory) +{ + IRestClient client = restClientFactory.CreateClient("my-client"); + + // Use the client in your code +} +``` \ No newline at end of file diff --git a/src/RestSharp.Extensions.DependencyInjection/RestSharp.Extensions.DependencyInjection.csproj b/src/RestSharp.Extensions.DependencyInjection/RestSharp.Extensions.DependencyInjection.csproj index e294131f3..bbd028f31 100644 --- a/src/RestSharp.Extensions.DependencyInjection/RestSharp.Extensions.DependencyInjection.csproj +++ b/src/RestSharp.Extensions.DependencyInjection/RestSharp.Extensions.DependencyInjection.csproj @@ -5,4 +5,10 @@ + + + true + / + + diff --git a/src/RestSharp.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/RestSharp.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 70e56fbcc..d1f13c36d 100644 --- a/src/RestSharp.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/RestSharp.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -77,6 +77,16 @@ public void AddRestClient(RestClientOptions options) { services.AddRestClient(Constants.DefaultRestClient, o => o.CopyFrom(options)); } + /// + /// Adds a RestClient to the service collection with custom options. + /// + /// Function to configure the RestClient options. + [PublicAPI] + public void AddRestClient(ConfigureRestClient configureRestClient) { + Ensure.NotNull(configureRestClient, nameof(configureRestClient)); + services.AddRestClient(Constants.DefaultRestClient, configureRestClient); + } + /// /// Adds a named RestClient to the service collection with base URL. ///