-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from fs7744/httpclient
start http client #14
- Loading branch information
Showing
38 changed files
with
947 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
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
16 changes: 16 additions & 0 deletions
16
src/Norns.Urd.Extensions.Polly/PollyServiceCollectionExtensions.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,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; | ||
} | ||
} | ||
} |
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,9 @@ | ||
using System; | ||
|
||
namespace Norns.Urd.Http | ||
{ | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public class BodyAttribute : Attribute | ||
{ | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Norns.Urd.HttpClient/Attributes/ClientNameAttribute.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,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
58
src/Norns.Urd.HttpClient/Attributes/ClientSettingsAttribute.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,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
44
src/Norns.Urd.HttpClient/Attributes/ContentTypeAttribute.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,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) | ||
{ | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Norns.Urd.HttpClient/Attributes/HttpCompletionOptionAttribute.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,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
86
src/Norns.Urd.HttpClient/Attributes/HttpMethodAttribute.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,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) | ||
{ | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Norns.Urd.HttpClient/Attributes/HttpRequestMessageSettingsAttribute.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,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); | ||
} | ||
} |
Oops, something went wrong.