Skip to content

Commit

Permalink
Added IConsulClient<T> and DotNetHttpClient implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloKitty committed Apr 25, 2018
1 parent f715469 commit 72f9002
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Consul.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consul.Net.API", "src\Consu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consul.Net.Manual.Test", "tests\Consul.Net.Manual.Test\Consul.Net.Manual.Test.csproj", "{0E22CDF4-A455-468C-978D-E7F347A6EDE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consul.Net.Client.DotNetHttpClient", "src\Consul.Net.Client.DotNetHttpClient\Consul.Net.Client.DotNetHttpClient.csproj", "{2ECEB26A-C99F-44A5-A947-795D1110DCDF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,13 +27,18 @@ Global
{0E22CDF4-A455-468C-978D-E7F347A6EDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E22CDF4-A455-468C-978D-E7F347A6EDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E22CDF4-A455-468C-978D-E7F347A6EDE2}.Release|Any CPU.Build.0 = Release|Any CPU
{2ECEB26A-C99F-44A5-A947-795D1110DCDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ECEB26A-C99F-44A5-A947-795D1110DCDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ECEB26A-C99F-44A5-A947-795D1110DCDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ECEB26A-C99F-44A5-A947-795D1110DCDF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6BB4BA77-AD1F-48E2-B68C-BBC2ADFFDC81} = {82223BC2-FD46-45B7-8714-A1BBB6FACC0E}
{0E22CDF4-A455-468C-978D-E7F347A6EDE2} = {FCFAE99C-C28A-44B7-A7BE-F9D5481C35B9}
{2ECEB26A-C99F-44A5-A947-795D1110DCDF} = {82223BC2-FD46-45B7-8714-A1BBB6FACC0E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {886DD3D7-35E3-42F0-8CE9-74FB5AD7BBD0}
Expand Down
18 changes: 18 additions & 0 deletions src/Consul.Net.API/Client/IConsulClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Consul.Net
{
/// <summary>
/// Contract for all Consul clients provided by the Consul.Net library.
/// </summary>
/// <typeparam name="TConsulServiceType">The type of service to expose. (Ex. <see cref="IConsulAgentServiceHttpApiService"/>)</typeparam>
public interface IConsulClient<out TConsulServiceType>
{
/// <summary>
/// The specific Consul service.
/// </summary>
TConsulServiceType Service { get; }
}
}
2 changes: 1 addition & 1 deletion src/Consul.Net.API/Consul.Net.API.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
<TargetFrameworks>net45;netstandard1.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.4</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="TypeSafe.Http.Net.Core" Version="2.2.11" />
<PackageReference Include="TypeSafe.Http.Net.HttpClient" Version="2.2.11" />
<PackageReference Include="TypeSafe.Http.Net.Serializer.JsonNET" Version="2.2.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Consul.Net.API\Consul.Net.API.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions src/Consul.Net.Client.DotNetHttpClient/ConsulDotNetHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Net.Http;
using TypeSafe.Http.Net;

namespace Consul.Net
{
/// <summary>
/// .NET <see cref="HttpClient"/> implementation of the <see cref="IConsulClient{TConsulServiceType}"/>.
/// Services the API requests over the .NET <see cref="HttpClient"/>.
/// (Other implementations can exists)
/// </summary>
/// <typeparam name="TConsulServiceType">The type of the consul service.</typeparam>
public sealed class ConsulDotNetHttpClient<TConsulServiceType> : IConsulClient<TConsulServiceType>
where TConsulServiceType : class
{
/// <inheritdoc />
public TConsulServiceType Service { get; }

public ConsulDotNetHttpClient(string endpoint)
{
if(string.IsNullOrWhiteSpace(endpoint)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(endpoint));

Service = TypeSafeHttpBuilder<TConsulServiceType>
.Create()
.RegisterDefaultSerializers()
.RegisterDotNetHttpClient(endpoint)
.RegisterJsonNetSerializer()
.Build();

if(Service == null)
throw new InvalidOperationException($"Failed to build {nameof(Service)} for Type: {typeof(TConsulServiceType).Name}");
}
}
}
4 changes: 4 additions & 0 deletions tests/Consul.Net.Manual.Test/Consul.Net.Manual.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<Project>{6bb4ba77-ad1f-48e2-b68c-bbc2adffdc81}</Project>
<Name>Consul.Net.API</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Consul.Net.Client.DotNetHttpClient\Consul.Net.Client.DotNetHttpClient.csproj">
<Project>{2eceb26a-c99f-44a5-a947-795d1110dcdf}</Project>
<Name>Consul.Net.Client.DotNetHttpClient</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
28 changes: 10 additions & 18 deletions tests/Consul.Net.Manual.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ class Program
{
static async Task Main(string[] args)
{
IConsulAgentServiceHttpApiService consulAgentService = TypeSafeHttpBuilder<IConsulAgentServiceHttpApiService>
.Create()
.RegisterDefaultSerializers()
.RegisterJsonNetSerializer()
.RegisterDotNetHttpClient("http://localhost.fiddler:8500")
.Build();
IConsulClient<IConsulAgentServiceHttpApiService> consulAgentService =
new ConsulDotNetHttpClient<IConsulAgentServiceHttpApiService>(@"http://localhost.fiddler:8500");

IConsulCatalogServiceHttpApiService consulCatalogService = TypeSafeHttpBuilder<IConsulCatalogServiceHttpApiService>
.Create()
.RegisterDefaultSerializers()
.RegisterJsonNetSerializer()
.RegisterDotNetHttpClient("http://localhost.fiddler:8500")
.Build();
IConsulClient<IConsulCatalogServiceHttpApiService> consulCatalogService =
new ConsulDotNetHttpClient<IConsulCatalogServiceHttpApiService>(@"http://localhost.fiddler:8500");

//Manually tests registeration
await consulAgentService.RegisterService(new AgentServiceRegisterationRequest()
await consulAgentService.Service.RegisterService(new AgentServiceRegisterationRequest()
{
Address = "127.0.0.1",
Name = "TestService2",
Expand All @@ -36,22 +28,22 @@ await consulAgentService.RegisterService(new AgentServiceRegisterationRequest()
Tags = new []{"CN"}
});

Dictionary<string, AgentServiceEntry> servicesResponse = await consulAgentService.GetServices();
Dictionary<string, AgentServiceEntry> servicesResponse = await consulAgentService.Service.GetServices();

foreach(var entry in servicesResponse)
{
Console.WriteLine(entry.Value.ToString());
}

//Manual testing for catalog API
Dictionary<string, string[]> serviceEntries = await consulCatalogService.GetServices();
Dictionary<string, string[]> serviceEntries = await consulCatalogService.Service.GetServices();

foreach(var kvp in serviceEntries)
{
Console.WriteLine($"{kvp.Key} Tags: {kvp.Value.Aggregate("", (s, s1) => $"{s} {s1}")}");
}

CatalogServiceNodeEntry[] entries = await consulCatalogService.GetServiceNodes("TestService2");
CatalogServiceNodeEntry[] entries = await consulCatalogService.Service.GetServiceNodes("TestService2");

foreach(CatalogServiceNodeEntry entry in entries)
{
Expand All @@ -60,11 +52,11 @@ await consulAgentService.RegisterService(new AgentServiceRegisterationRequest()
Console.WriteLine(o);
}

CatalogServiceNodeEntry[] entries2 = await consulCatalogService.GetServiceNodes("TestService2", "NA");
CatalogServiceNodeEntry[] entries2 = await consulCatalogService.Service.GetServiceNodes("TestService2", "NA");

Console.WriteLine("\n\n\n");

foreach(CatalogServiceNodeEntry entry in entries)
foreach(CatalogServiceNodeEntry entry in entries2)
{
//Convert to JSON for easier logging
string o = JsonConvert.SerializeObject(entry);
Expand Down

0 comments on commit 72f9002

Please sign in to comment.