Skip to content

Commit

Permalink
Add ability to get default fluent client instance
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Jun 3, 2022
1 parent 45f1eed commit f68bcda
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/FluentRest.Factory/FluentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Options;

namespace FluentRest
{
/// <summary>
/// Extension methods for configuring an <see cref="FluentClient"/>
/// </summary>
public static class FluentExtensions {

/// <summary>
/// Creates a new <see cref="FluentClient"/> using the default configuration.
/// </summary>
/// <param name="factory"></param>
/// <returns>An <see cref="FluentClient"/> configured using the default configuration.</returns>
public static IFluentClient CreateClient(this IFluentClientFactory factory)
{
return factory.CreateClient(Options.DefaultName);
}

/// <summary>
/// Sets the content serializer type to use when using the <see cref="FluentClient"/>.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions test/FluentRest.Factory.Tests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Xunit;

namespace FluentRest.Tests
Expand Down Expand Up @@ -79,6 +80,32 @@ public void NotConfiguredClient()
Assert.IsAssignableFrom<IFluentClient>(fluentSampleClient);
}

[Fact]
public void DefaultClient()
{
var services = new ServiceCollection();

services.AddFluentClient();
services.AddHttpClient(Options.DefaultName, c =>
{
c.BaseAddress = new Uri("https://sample.com/");
})
.SetSerializer<MyContentSerializer>();

var serviceProvider = services.BuildServiceProvider();

var clientFactory = serviceProvider.GetService<IHttpClientFactory>();
var sampleClient = clientFactory.CreateClient();
Assert.NotNull(sampleClient);
Assert.Equal(new Uri("https://sample.com/"), sampleClient.BaseAddress);

var fluentClientFactory = serviceProvider.GetService<IFluentClientFactory>();
var fluentSampleClient = fluentClientFactory.CreateClient();
Assert.Equal(typeof(MyContentSerializer), fluentSampleClient.ContentSerializer.GetType());
Assert.Equal(new Uri("https://sample.com/"), fluentSampleClient.HttpClient.BaseAddress);
Assert.IsAssignableFrom<IFluentClient>(fluentSampleClient);
}

[Fact]
public void ServicesSerializer()
{
Expand Down

0 comments on commit f68bcda

Please sign in to comment.