From 75c1d0cbf9a5a7856c544671a45f1b4312ffe7ce Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 11 Jun 2024 22:11:45 -0400 Subject: feat: Add a default site adapater and interceptors --- lib/Net.Rest.Client/src/Construction/Extensions.cs | 72 ++++++++++++++-------- .../src/Construction/RestSiteAdapterBase.cs | 46 ++++++++++++++ 2 files changed, 91 insertions(+), 27 deletions(-) (limited to 'lib/Net.Rest.Client/src') diff --git a/lib/Net.Rest.Client/src/Construction/Extensions.cs b/lib/Net.Rest.Client/src/Construction/Extensions.cs index 93a1365..7285b84 100644 --- a/lib/Net.Rest.Client/src/Construction/Extensions.cs +++ b/lib/Net.Rest.Client/src/Construction/Extensions.cs @@ -30,6 +30,7 @@ using System.Threading.Tasks; using System.Collections.Generic; using RestSharp; +using RestSharp.Interceptors; namespace VNLib.Net.Rest.Client.Construction { @@ -301,13 +302,8 @@ namespace VNLib.Net.Rest.Client.Construction /// /// Specifies the uri for the request builder /// The chainable - public static IRestRequestBuilder WithUrl(this IRestRequestBuilder builder, Func withUri) - { - //Use the supplied method to convert the uri to a string - builder.WithUrl((m) => withUri(m).ToString()); - return builder; - } - + public static IRestRequestBuilder WithUrl(this IRestRequestBuilder builder, Func withUri) + => builder.WithUrl((m) => withUri(m).ToString()); /// /// Sets the uri for all new request messages @@ -330,12 +326,8 @@ namespace VNLib.Net.Rest.Client.Construction /// /// Specifies the uri for the request builder /// The chainable - public static IRestRequestBuilder WithUrl(this IRestRequestBuilder builder, string url) - { - //Use the supplied method to convert the uri to a string - builder.WithUrl((m) => url); - return builder; - } + public static IRestRequestBuilder WithUrl(this IRestRequestBuilder builder, string url) + => builder.WithUrl((m) => url); /// /// Specifies a connection header to set for every request @@ -345,12 +337,8 @@ namespace VNLib.Net.Rest.Client.Construction /// The name of the header to set /// The callback function to get the request header value /// The chainable - public static IRestRequestBuilder WithHeader(this IRestRequestBuilder builder, string header, Func func) - { - //Specify a header by the given name - builder.WithModifier((m, req) => req.AddHeader(header, func(m))); - return builder; - } + public static IRestRequestBuilder WithHeader(this IRestRequestBuilder builder, string header, Func func) + => builder.WithModifier((m, req) => req.AddHeader(header, func(m))); /// /// Specifies a static connection header to set for every request @@ -360,11 +348,43 @@ namespace VNLib.Net.Rest.Client.Construction /// The name of the header to set /// The static header value to set for all requests /// The chainable - public static IRestRequestBuilder WithHeader(this IRestRequestBuilder builder, string header, string value) + public static IRestRequestBuilder WithHeader(this IRestRequestBuilder builder, string header, string value) + => builder.WithModifier((m, req) => req.AddHeader(header, value)); + + /// + /// Adds a client interceptor instance to the request builder + /// + /// + /// + /// A callback function to get an interceptor instance for the builder + /// The chainable + public static IRestRequestBuilder WithInterceptor(this IRestRequestBuilder builder, Func interceptor) { - //Specify a header by the given name - builder.WithModifier((m, req) => req.AddHeader(header, value)); - return builder; + ArgumentNullException.ThrowIfNull(interceptor); + + return builder.WithModifier((m, r) => + { + r.Interceptors ??= []; + r.Interceptors.Add(interceptor(m)); + }); + } + + /// + /// Adds a client interceptor instance to the request builder + /// + /// + /// + /// The interceptor instance to add to the request + /// The chainable + public static IRestRequestBuilder WithInterceptor(this IRestRequestBuilder builder, Interceptor interceptor) + { + ArgumentNullException.ThrowIfNull(interceptor); + + return builder.WithModifier((m, r) => + { + r.Interceptors ??= []; + r.Interceptors.Add(interceptor); + }); } /// @@ -378,8 +398,7 @@ namespace VNLib.Net.Rest.Client.Construction public static IRestRequestBuilder WithParameter(this IRestRequestBuilder builder, string parameter, Func callback) { //Get a query item string value from the callback and sets the query paremter - builder.WithModifier((m, r) => r.AddParameter(parameter, callback(m), ParameterType.GetOrPost)); - return builder; + return builder.WithModifier((m, r) => r.AddParameter(parameter, callback(m), ParameterType.GetOrPost)); } /// @@ -393,8 +412,7 @@ namespace VNLib.Net.Rest.Client.Construction public static IRestRequestBuilder WithParameter(this IRestRequestBuilder builder, string parameter, string value) { //Get a query item string value from the callback and sets the query paremter - builder.WithModifier((m, r) => r.AddParameter(parameter, value, ParameterType.GetOrPost)); - return builder; + return builder.WithModifier((m, r) => r.AddParameter(parameter, value, ParameterType.GetOrPost)); } diff --git a/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs b/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs index f7b234b..4383d51 100644 --- a/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs +++ b/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs @@ -68,5 +68,51 @@ namespace VNLib.Net.Rest.Client.Construction /// The endpoint model type /// The model typed adapter to add to the store public virtual void AddAdapter(IRestEndpointAdapter adapter) => Adapters[typeof(TModel)] = adapter; + + /// + /// Creats a very simple site adapter for simple REST operations with + /// your custom rest configuration + /// + /// Your custom rest client options + /// The maximum number of clients to keep in cached + /// The preconfigured site adapter + public static RestSiteAdapterBase CreateSimpleAdapter(RestClientOptions options, int maxClients = 1) + => new SimpleSiteAdapter(maxClients, options); + + /// + /// Creats a very simple site adapter for simple REST operations with + /// some basic default options + /// + /// The site base url + /// The maximum number of clients to keep in cached + /// The preconfigured site adapter + public static RestSiteAdapterBase CreateSimpleAdapter(string? baseUrl = null, int maxClients = 1) + => new SimpleSiteAdapter(maxClients, options: new () + { + Encoding = System.Text.Encoding.UTF8, + MaxRedirects = 5, + BaseUrl = baseUrl is not null ? new Uri(baseUrl) : null, + Timeout = TimeSpan.FromSeconds(10), + AutomaticDecompression = System.Net.DecompressionMethods.All, + AllowMultipleDefaultParametersWithSameName = true, + }); + + /// + /// Implements an over simplified site adapter for simple REST operations + /// + /// The maximum nuber of clients in the connection pool + /// The shared client options + protected class SimpleSiteAdapter(int maxClients, RestClientOptions options) : RestSiteAdapterBase + { + /// + protected override RestClientPool Pool { get; } = new RestClientPool(maxClients, options); + + /// + public override void OnResponse(RestResponse response) + { } + + /// + public override Task WaitAsync(CancellationToken cancellation = default) => Task.CompletedTask; + } } } -- cgit