Skip to content

Commit

Permalink
Merge pull request #21 from fs7744/httpclient
Browse files Browse the repository at this point in the history
start http client #14
  • Loading branch information
fs7744 authored Dec 27, 2020
2 parents 2497694 + c3376f9 commit bc0b1ca
Show file tree
Hide file tree
Showing 38 changed files with 947 additions and 19 deletions.
9 changes: 8 additions & 1 deletion Norns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Cache", "Cache", "{1AF242BB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Norns.Urd.Caching.Abstractions", "src\Norns.Urd.Caching.Abstractions\Norns.Urd.Caching.Abstractions.csproj", "{C9894C9F-C17F-4C4E-BD58-4B098C57F87D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Norns.Urd.Caching.DistributedCache", "src\Norns.Urd.Caching.DistributedCache\Norns.Urd.Caching.DistributedCache.csproj", "{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Norns.Urd.Caching.DistributedCache", "src\Norns.Urd.Caching.DistributedCache\Norns.Urd.Caching.DistributedCache.csproj", "{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Norns.Urd.HttpClient", "src\Norns.Urd.HttpClient\Norns.Urd.HttpClient.csproj", "{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8}.Release|Any CPU.Build.0 = Release|Any CPU
{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -83,6 +89,7 @@ Global
{1AF242BB-9AA3-469F-83E4-3C1752C9087A} = {3FD69CA3-BAF7-4B7A-AAB2-B9853759F4D7}
{C9894C9F-C17F-4C4E-BD58-4B098C57F87D} = {1AF242BB-9AA3-469F-83E4-3C1752C9087A}
{32BAE170-E9B1-4EE5-B79F-10B6EAD018B8} = {1AF242BB-9AA3-469F-83E4-3C1752C9087A}
{D2BF85FF-3D14-40A9-8CE8-A0404FBA1C36} = {3FD69CA3-BAF7-4B7A-AAB2-B9853759F4D7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C2BFA03B-F071-472F-95AD-C2E127881EDE}
Expand Down
2 changes: 1 addition & 1 deletion ProjectCommon.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>true</IsPackable>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>0.0.3.0</Version>
<Version>0.0.4.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
Expand Down
8 changes: 0 additions & 8 deletions src/Norns.Urd.Extensions.Polly/PolicyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,5 @@ public static PolicyBuilder CreatePolicyBuilder(Type exceptionType)
{
return ((PolicyBuilder)PolicyExtensions.HandleException.MakeGenericMethod(exceptionType).Invoke(null, null));
}

public static IAspectConfiguration EnablePolly(this IAspectConfiguration configuration)
{
configuration.NonPredicates.AddNamespace("Polly")
.AddNamespace("Polly.*");
configuration.GlobalInterceptors.Add(new PolicyInterceptor());
return configuration;
}
}
}
16 changes: 16 additions & 0 deletions src/Norns.Urd.Extensions.Polly/PollyServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Norns.Urd;
using Norns.Urd.Extensions.Polly;

namespace Microsoft.Extensions.DependencyInjection
{
public static class PollyServiceCollectionExtensions
{
public static IAspectConfiguration EnablePolly(this IAspectConfiguration configuration)
{
configuration.NonPredicates.AddNamespace("Polly")
.AddNamespace("Polly.*");
configuration.GlobalInterceptors.Add(new PolicyInterceptor());
return configuration;
}
}
}
9 changes: 9 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/BodyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Norns.Urd.Http
{
[AttributeUsage(AttributeTargets.Parameter)]
public class BodyAttribute : Attribute
{
}
}
16 changes: 16 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/ClientNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Options;
using System;

namespace Norns.Urd.Http
{
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class ClientNameAttribute : Attribute
{
public ClientNameAttribute(string name)
{
Name = string.IsNullOrWhiteSpace(name) ? Options.DefaultName : name;
}

public string Name { get; }
}
}
58 changes: 58 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/ClientSettingsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Client = System.Net.Http.HttpClient;

namespace Norns.Urd.Http
{
public abstract class ClientSettingsAttribute : Attribute
{
public abstract void SetClient(Client client, AspectContext context);
}

[AttributeUsage(AttributeTargets.Interface)]
public class BaseAddressAttribute : ClientSettingsAttribute
{
private readonly Uri baseAddress;

public BaseAddressAttribute(string baseAddress)
{
this.baseAddress = new Uri(baseAddress);
}

public override void SetClient(Client client, AspectContext context)
{
client.BaseAddress = baseAddress;
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class MaxResponseContentBufferSizeAttribute : ClientSettingsAttribute
{
private readonly long maxResponseContentBufferSize;

public MaxResponseContentBufferSizeAttribute(long maxResponseContentBufferSize)
{
this.maxResponseContentBufferSize = maxResponseContentBufferSize;
}

public override void SetClient(Client client, AspectContext context)
{
client.MaxResponseContentBufferSize = maxResponseContentBufferSize;
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class TimeoutAttribute : ClientSettingsAttribute
{
private readonly TimeSpan timeout;

public TimeoutAttribute(string timeout)
{
this.timeout = TimeSpan.Parse(timeout);
}

public override void SetClient(Client client, AspectContext context)
{
client.Timeout = timeout;
}
}
}
44 changes: 44 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/ContentTypeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Net.Http.Headers;
using System.Text;

namespace Norns.Urd.Http
{
public abstract class MediaTypeHeaderValueAttribute : Attribute
{
protected MediaTypeHeaderValueAttribute(MediaTypeHeaderValue contentType)
{
ContentType = contentType;
}

public MediaTypeHeaderValue ContentType { get; }
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class ContentTypeAttribute : MediaTypeHeaderValueAttribute
{
public ContentTypeAttribute(string contentType) : base(new MediaTypeHeaderValue(contentType))
{
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class JsonContentTypeAttribute : MediaTypeHeaderValueAttribute
{
public static readonly MediaTypeHeaderValue Json = new MediaTypeHeaderValue("application/json") { CharSet = Encoding.UTF8.WebName };

public JsonContentTypeAttribute() : base(Json)
{
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class XmlContentTypeAttribute : MediaTypeHeaderValueAttribute
{
public static readonly MediaTypeHeaderValue Xml = new MediaTypeHeaderValue("application/xml");

public XmlContentTypeAttribute() : base(Xml)
{
}
}
}
62 changes: 62 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/HeaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace Norns.Urd.Http
{
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)]
public class HeaderAttribute : HttpRequestMessageSettingsAttribute
{
private readonly string name;
private readonly string value;

public HeaderAttribute(string name, string value)
{
this.name = name;
this.value = value;
}

public override void SetRequest(HttpRequestMessage request, AspectContext context)
{
request.Headers.Add(name, value);
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)]
public class AcceptAttribute : HttpRequestMessageSettingsAttribute
{
private readonly string value;

public AcceptAttribute(string value)
{
this.value = value;
}

public override void SetRequest(HttpRequestMessage request, AspectContext context)
{
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(value));
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)]
public class AcceptJsonAttribute : HttpRequestMessageSettingsAttribute
{
private static readonly MediaTypeWithQualityHeaderValue Json = MediaTypeWithQualityHeaderValue.Parse(JsonContentTypeAttribute.Json.MediaType);

public override void SetRequest(HttpRequestMessage request, AspectContext context)
{
request.Headers.Accept.Add(Json);
}
}

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)]
public class AcceptXmlAttribute : HttpRequestMessageSettingsAttribute
{
private static readonly MediaTypeWithQualityHeaderValue Xml = MediaTypeWithQualityHeaderValue.Parse(XmlContentTypeAttribute.Xml.MediaType);

public override void SetRequest(HttpRequestMessage request, AspectContext context)
{
request.Headers.Accept.Add(Xml);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Net.Http;

namespace Norns.Urd.Http
{
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method)]
public class HttpCompletionOptionAttribute : Attribute
{
public HttpCompletionOptionAttribute(HttpCompletionOption option)
{
Option = option;
}

public HttpCompletionOption Option { get; }
}
}
86 changes: 86 additions & 0 deletions src/Norns.Urd.HttpClient/Attributes/HttpMethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Net.Http;

namespace Norns.Urd.Http
{

public abstract class HttpMethodAttribute : Attribute, IHttpRequestMessageSettings
{
private readonly string path;
private readonly HttpMethod method;

protected HttpMethodAttribute(string path, HttpMethod method)
{
this.path = path;
this.method = method;
}

public void SetRequest(HttpRequestMessage request, AspectContext context)
{
if (!string.IsNullOrEmpty(path))
{
Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var uri);
request.RequestUri = uri;
}
request.Method = method;
}
}

[AttributeUsage(AttributeTargets.Method)]
public class GetAttribute : HttpMethodAttribute
{
public GetAttribute(string path = null) : base(path, HttpMethod.Get)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class PostAttribute : HttpMethodAttribute
{
public PostAttribute(string path = null) : base(path, HttpMethod.Post)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class PutAttribute : HttpMethodAttribute
{
public PutAttribute(string path = null) : base(path, HttpMethod.Put)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class DeleteAttribute : HttpMethodAttribute
{
public DeleteAttribute(string path = null) : base(path, HttpMethod.Delete)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class PatchAttribute : HttpMethodAttribute
{
public static readonly HttpMethod Patch = new HttpMethod("PATCH");

public PatchAttribute(string path = null) : base(path, Patch)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class OptionsAttribute : HttpMethodAttribute
{
public OptionsAttribute(string path = null) : base(path, HttpMethod.Options)
{
}
}

[AttributeUsage(AttributeTargets.Method)]
public class HeadAttribute : HttpMethodAttribute
{
public HeadAttribute(string path = null) : base(path, HttpMethod.Head)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Net.Http;

namespace Norns.Urd.Http
{
public abstract class HttpRequestMessageSettingsAttribute : Attribute, IHttpRequestMessageSettings
{
public abstract void SetRequest(HttpRequestMessage request, AspectContext context);
}
}
Loading

0 comments on commit bc0b1ca

Please sign in to comment.