Skip to content

Commit

Permalink
Merge pull request #75 from Unleash/feature/supporting-project-query
Browse files Browse the repository at this point in the history
feat: support project query parameter (#69)
  • Loading branch information
Christopher Kolstad committed Jun 16, 2021
2 parents ea0238c + 399ee63 commit 4313a49
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ When your application shuts down, remember to dispose the unleash instance.
unleash?.Dispose()
```

### Configuring projects in unleash client

If you're organizing your feature toggles in `Projects` in Unleash Enterprise, you can specify the `ProjectId` on the `UnleashSettings` to select which project to fetch feature toggles for.

```csharp

var settings = new UnleashSettings()
{
AppName = "dotnet-test",
InstanceTag = "instance z",
UnleashApi = new Uri("http://unleash.herokuapp.com/api/"),
ProjectId = "projectId"
};

```

### Feature toggle api

It is really simple to use unleash.
Expand Down
9 changes: 7 additions & 2 deletions src/Unleash/Communication/UnleashApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ internal class UnleashApiClient : IUnleashApiClient
private readonly HttpClient httpClient;
private readonly IJsonSerializer jsonSerializer;
private readonly UnleashApiClientRequestHeaders clientRequestHeaders;
private readonly string projectId;

public UnleashApiClient(
HttpClient httpClient,
IJsonSerializer jsonSerializer,
UnleashApiClientRequestHeaders clientRequestHeaders)
UnleashApiClientRequestHeaders clientRequestHeaders,
string projectId = null)
{
this.httpClient = httpClient;
this.jsonSerializer = jsonSerializer;
this.clientRequestHeaders = clientRequestHeaders;
this.projectId = projectId;
}

public async Task<FetchTogglesResult> FetchToggles(string etag, CancellationToken cancellationToken)
{
const string resourceUri = "client/features";
string resourceUri = "client/features";
if (!string.IsNullOrWhiteSpace(this.projectId))
resourceUri += "?project=" + this.projectId;

using (var request = new HttpRequestMessage(HttpMethod.Get, resourceUri))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Unleash/Internal/UnleashServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public UnleashServices(UnleashSettings settings, Dictionary<string, IStrategy> s
InstanceTag = settings.InstanceTag,
CustomHttpHeaders = settings.CustomHttpHeaders,
CustomHttpHeaderProvider = settings.UnleashCustomHttpHeaderProvider
});
}, settings.ProjectId);
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/Unleash/UnleashSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class UnleashSettings
/// </summary>
public string InstanceTag { get; set; } = GetDefaultInstanceTag();

/// <summary>
/// Sets the project to fetch feature toggles for.
/// </summary>
public string ProjectId { get; set; } = null;

/// <summary>
/// Gets or sets the interval in which feature toggle changes are re-fetched.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using Unleash.Communication;
using Unleash.Serialization;
using Unleash.Tests.Mock;

namespace Unleash.Tests.Communication
{
public class UnleashApiClient_Project_Tests
{
private UnleashApiClient NewTestableClient(string project, MockHttpMessageHandler messageHandler)
{
var apiUri = new Uri("http://unleash.herokuapp.com/api/");

var jsonSerializer = new DynamicNewtonsoftJsonSerializer();
jsonSerializer.TryLoad();

var requestHeaders = new UnleashApiClientRequestHeaders
{
AppName = "api-test-client",
InstanceTag = "instance1",
CustomHttpHeaders = null,
CustomHttpHeaderProvider = null
};

var httpClient = new HttpClient(messageHandler)
{
BaseAddress = apiUri,
Timeout = TimeSpan.FromSeconds(5)
};

return new UnleashApiClient(httpClient, jsonSerializer, requestHeaders, project);
}

[Test]
public async Task FetchToggles_ForProject()
{
var project = "testproject";
var messageHandler = new MockHttpMessageHandler();
var client = NewTestableClient(project, messageHandler);

var toggles = await client.FetchToggles("", CancellationToken.None);
toggles.Should().NotBeNull();

messageHandler.SentMessages.Count.Should().Be(1);
messageHandler.SentMessages.First().RequestUri.Query.Should().Be("?project=" + project);
}

[Test]
public async Task FetchToggles_WithoutProject()
{
string project = null;
var messageHandler = new MockHttpMessageHandler();
var client = NewTestableClient(project, messageHandler);

var toggles = await client.FetchToggles("", CancellationToken.None);
toggles.Should().NotBeNull();

messageHandler.SentMessages.Count.Should().Be(1);
messageHandler.SentMessages.First().RequestUri.Query.Should().Be("");
}
}
}
25 changes: 25 additions & 0 deletions tests/Unleash.Tests/Mock/MockHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Unleash.Tests.Mock
{
public class MockHttpMessageHandler : HttpMessageHandler
{
public List<HttpRequestMessage> SentMessages = new List<HttpRequestMessage>();

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
SentMessages.Add(request);

return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("{}", Encoding.UTF8, "application/json")
});
}
}
}
2 changes: 2 additions & 0 deletions tests/Unleash.Tests/Unleash.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<ItemGroup>
<Compile Include="BaseTest.cs" />
<Compile Include="Communication\BaseUnleashApiClientTest.cs" />
<Compile Include="Communication\UnleashApiClient_Project_Tests.cs" />
<Compile Include="Communication\UnleashApiClient_RegisterClient_Tests.cs" />
<Compile Include="Communication\UnleashApiClient_SendMetrics_Tests.cs" />
<Compile Include="Communication\UnleashHttpClientFactory_RegisterHttpClient_Tests.cs" />
Expand All @@ -79,6 +80,7 @@
<Compile Include="Integration\UnleashContextDefinition.cs" />
<Compile Include="Metrics\MetricsBucketTests.cs" />
<Compile Include="Mock\MockApiClient.cs" />
<Compile Include="Mock\MockHttpMessageHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Communication\UnleashApiClient_FetchToggles_Tests.cs" />
<Compile Include="Serialization\JsonNetSerializer.cs" />
Expand Down

0 comments on commit 4313a49

Please sign in to comment.