Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/GeekLearning.RestKit.Core/ClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace GeekLearning.RestKit.Core
namespace GeekLearning.RestKit.Core
{
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

public abstract class ClientBase<TOptions>
where TOptions : class, new()
where TOptions : class, IProvideRequestFilters, new()
{
private IMediaFormatterProvider mediaFormatterProvider;
private IServiceProvider serviceProvider;
private Lazy<IRequestFilter[]> requestFilters;

public ClientBase(IOptions<TOptions> options, IMediaFormatterProvider mediaFormatterProvider)
public ClientBase(IOptions<TOptions> options,
IMediaFormatterProvider mediaFormatterProvider,
IServiceProvider serviceProvider)
{
this.Options = options.Value;
this.mediaFormatterProvider = mediaFormatterProvider;
this.serviceProvider = serviceProvider;
this.requestFilters = new Lazy<IRequestFilter[]>(() =>
this.Options.RequestFilters.Select(x=> ActivatorUtilities.CreateInstance(this.serviceProvider, x.Type, x.Arguments)).Cast<IRequestFilter>().ToArray()
);
}

protected TOptions Options { get; private set; }
Expand All @@ -30,6 +41,17 @@ protected Task<TTarget> TransformResponseAsync<TTarget>(HttpResponseMessage mess
return mediaFormatter.TransformAsync<TTarget>(message.Content);
}

protected HttpRequestMessage ApplyFilters(HttpRequestMessage requestMessage, params string[] securityDefinitions)
{
HttpRequestMessage finalMessage = requestMessage;
foreach (var filter in this.requestFilters.Value)
{
finalMessage = filter.Apply(requestMessage, securityDefinitions) ?? finalMessage;
}

return finalMessage;
}

protected HttpContent TransformRequestBody(object data, string mediaType)
{
IMediaFormatter mediaFormatter = this.mediaFormatterProvider.GetMediaFormatter(mediaType);
Expand Down
29 changes: 29 additions & 0 deletions src/GeekLearning.RestKit.Core/ClientOptionsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace GeekLearning.RestKit.Core
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public abstract class ClientOptionsBase : IProvideRequestFilters
{
private List<InjectionDescriptor> requestfilters = new List<InjectionDescriptor>();

public void AddFilter<TRequestFilter>(params object[] arguments)
where TRequestFilter: IRequestFilter
{
this.requestfilters.Add(new InjectionDescriptor {
Type = typeof(TRequestFilter),
Arguments = arguments
});
}

public IEnumerable<InjectionDescriptor> RequestFilters
{
get
{
return requestfilters;
}
}
}
}
12 changes: 12 additions & 0 deletions src/GeekLearning.RestKit.Core/IProvideRequestFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace GeekLearning.RestKit.Core
{
public interface IProvideRequestFilters
{
IEnumerable<InjectionDescriptor> RequestFilters { get; }
}
}
13 changes: 13 additions & 0 deletions src/GeekLearning.RestKit.Core/IRequestFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace GeekLearning.RestKit.Core
{
public interface IRequestFilter
{
HttpRequestMessage Apply(HttpRequestMessage requestMessage, string[] securityDefinitions);
}
}
14 changes: 14 additions & 0 deletions src/GeekLearning.RestKit.Core/InjectionDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace GeekLearning.RestKit.Core
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


public struct InjectionDescriptor
{
public Type Type;
public object[] Arguments;
}
}