-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IConsulClient<T> and DotNetHttpClient implementation
- Loading branch information
1 parent
f715469
commit 72f9002
Showing
7 changed files
with
92 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Consul.Net.Client.DotNetHttpClient/Consul.Net.Client.DotNetHttpClient.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/Consul.Net.Client.DotNetHttpClient/ConsulDotNetHttpClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters