From 311f8cbb0ffe623134896c696b71339fb1ab3954 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 23 Apr 2023 11:28:48 -0400 Subject: Rest cleint construction --- lib/Net.Rest.Client/src/Construction/Extensions.cs | 264 +++++++++++++++++++++ .../src/Construction/IRestEndpointAdapter.cs | 51 ++++ .../src/Construction/IRestEndpointBuilder.cs | 40 ++++ .../src/Construction/IRestEndpointDefinition.cs | 39 +++ .../src/Construction/IRestRequestBody.cs | 40 ++++ .../src/Construction/IRestRequestBuilder.cs | 60 +++++ .../src/Construction/IRestSingleEndpoint.cs | 57 +++++ .../src/Construction/IRestSiteAdapter.cs | 65 +++++ .../src/Construction/IRestSiteEndpointStore.cs | 46 ++++ .../src/Construction/RestSiteAdapterBase.cs | 72 ++++++ 10 files changed, 734 insertions(+) create mode 100644 lib/Net.Rest.Client/src/Construction/Extensions.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestEndpointAdapter.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestEndpointBuilder.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestEndpointDefinition.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestRequestBody.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestRequestBuilder.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestSingleEndpoint.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestSiteAdapter.cs create mode 100644 lib/Net.Rest.Client/src/Construction/IRestSiteEndpointStore.cs create mode 100644 lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs (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 new file mode 100644 index 0000000..3f0e79b --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/Extensions.cs @@ -0,0 +1,264 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: Extensions.cs +* +* Extensions.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using System.Collections.Generic; + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Construction extensions + /// + public static class Extensions + { + /// + /// Executes a request against the site by sending the request model parameter. An must be + /// defined to handle requests of the given model type. + /// + /// + /// + /// The request entity model to send to the server + /// A token to cancel the operation + /// A task that resolves the response message + public static async Task ExecuteAsync(this IRestSiteAdapter site, TModel entity, CancellationToken cancellation = default) + { + //Get the adapter for the model + IRestEndpointAdapter adapter = site.GetAdapter(); + + //Get new request on adapter + RestRequest request = adapter.GetRequest(entity); + + //Wait to exec operations if needed + await site.WaitAsync(cancellation); + + RestResponse response; + + //Get rest client + using (ClientContract contract = site.GetClient()) + { + //Exec response + response = await contract.Resource.ExecuteAsync(request, cancellation); + } + + //Site handler should not cause an exception + site.OnResponse(response); + + //invoke response handlers + adapter.OnResponse(entity, response); + + return response; + } + + /// + /// Executes a request using a model that defines its own endpoint information, on the current site and returns the response + /// + /// The request model type + /// + /// The entity model that defines itself as an endpoint and the request information + /// A token to cancel the operation + /// When completed, gets the + public static async Task ExecuteSingleAsync(this IRestSiteAdapter site, TModel model, CancellationToken cancellation = default) where TModel : IRestSingleEndpoint + { + //Init new request + RestRequest request = new(model.Url, model.Method); + model.OnRequest(request); + + //Wait to exec operations if needed + await site.WaitAsync(cancellation); + + RestResponse response; + + //Get rest client + using (ClientContract contract = site.GetClient()) + { + //Exec response + response = await contract.Resource.ExecuteAsync(request, cancellation); + } + + //Site handler should not cause an exception + site.OnResponse(response); + + //Allow model to handle a response + model.OnResponse(response); + + return response; + } + + /// + /// Sets the request method of a new request + /// + /// + /// + /// The callback method that will be invoked on every call to build a new request + /// The chainable + public static IRestRequestBuilder WithMethod(this IRestRequestBuilder builder, Func methodCb) + { + builder.WithModifier((m, r) => methodCb(m)); + return builder; + } + + /// + /// Sets a callback that will create a query string argument value + /// + /// The request entity type + /// + /// The callback method that gets the query value + /// The query paramter value to set + /// The chainable + public static IRestRequestBuilder WithQuery(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.AddQueryParameter(parameter, callback(m))); + return builder; + } + + /// + /// Specifies a model that will handle its own request body builder + /// + /// + /// + /// The chainable + public static IRestRequestBuilder WithBodyBuilder(this IRestRequestBuilder builder) where TModel : IRestRequestBody + { + builder.WithModifier(static (m, r) => m.AddBody(r)); + return builder; + } + + /// + /// Builds endpoints from an and stores them in the + /// + /// + /// + /// The endpoint definition to build endpoints from + /// A chainable + public static IRestSiteEndpointStore BuildEndpoints(this IRestSiteEndpointStore site, IRestEndpointDefinition endpoint) + { + EndpointAdapterBuilder builder = new(site); + + //Build endpoints + endpoint.BuildRequest(site.Site, builder); + + return site; + } + + /// + /// Converts a task that resolves a to a task that deserializes + /// the response data as json. + /// + /// The json response entity type + /// The response task + /// A task that resolves the deserialized entity type + public static async Task AsJson(this Task response) + { + RestResponse r = await response.ConfigureAwait(false); + return JsonSerializer.Deserialize(r.RawBytes); + } + + private record class EndpointAdapterBuilder(IRestSiteEndpointStore Site) : IRestEndpointBuilder + { + /// + public IRestRequestBuilder WithEndpoint() + { + //New adapter + EndpointAdapter adapter = new(); + + //Store adapter in site + Site.AddAdapter(adapter); + + //Builder + return new RequestBuilder(adapter); + } + + private record class RequestBuilder(EndpointAdapter Adapter) : IRestRequestBuilder + { + /// + public IRestRequestBuilder WithModifier(Action requestBuilder) + { + _ = requestBuilder ?? throw new ArgumentNullException(nameof(requestBuilder)); + //Add handler to handler chain + Adapter.RequestChain.AddLast(requestBuilder); + return this; + } + + /// + public IRestRequestBuilder WithUrl(Func uriBuilder) + { + _ = uriBuilder ?? throw new ArgumentNullException(nameof(uriBuilder)); + //Add get url handler + Adapter.GetUrl = uriBuilder; + return this; + } + + /// + public IRestRequestBuilder OnResponse(Action onResponseBuilder) + { + _ = onResponseBuilder ?? throw new ArgumentNullException(nameof(onResponseBuilder)); + //Add a response handler + Adapter.ResponseChain.AddLast(onResponseBuilder); + return this; + } + } + + private sealed class EndpointAdapter : IRestEndpointAdapter + { + internal Func? GetUrl { get; set; } + + internal LinkedList> RequestChain { get; } = new(); + internal LinkedList> ResponseChain { get; } = new(); + + RestRequest IRestEndpointAdapter.GetRequest(TModel entity) + { + //First we need to get the url for the entity + string? url = GetUrl?.Invoke(entity); + + //New request + RestRequest request = new(url); + + //Invoke request modifier chain + foreach (Action action in RequestChain) + { + action(entity, request); + } + + return request; + } + + void IRestEndpointAdapter.OnResponse(TModel model, RestResponse response) + { + //Invoke request modifier chain + foreach (Action action in ResponseChain) + { + action(model, response); + } + } + } + } + + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestEndpointAdapter.cs b/lib/Net.Rest.Client/src/Construction/IRestEndpointAdapter.cs new file mode 100644 index 0000000..2e9d26d --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestEndpointAdapter.cs @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestEndpointAdapter.cs +* +* IRestEndpointAdapter.cs is part of VNLib.Net.Rest.Client which is +* part of the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents a remote http endpoint that can create requests given an entity + /// to communicate with the remote endpoint. + /// + /// The request entity model + public interface IRestEndpointAdapter + { + /// + /// Gets a new for the given request entity/arguments + /// used to make a request against a rest endpoint + /// + /// The entity to get the new for + /// The configured to send to the endpoint + RestRequest GetRequest(TEntity entity); + + /// + /// Called when a request has successfully completed, may be used to validate a response message + /// + /// The original request entity that was sent + /// The response message that was received + void OnResponse(TEntity model, RestResponse response); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestEndpointBuilder.cs b/lib/Net.Rest.Client/src/Construction/IRestEndpointBuilder.cs new file mode 100644 index 0000000..9f62ef6 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestEndpointBuilder.cs @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestEndpointBuilder.cs +* +* IRestEndpointBuilder.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents an object used to define endpoint adapters for a + /// + public interface IRestEndpointBuilder + { + /// + /// Creates a new endpoint adapter of the given entity model type and gets a + /// that may be used to configure the request message. + /// + /// + /// A chainable used to define your request + IRestRequestBuilder WithEndpoint(); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestEndpointDefinition.cs b/lib/Net.Rest.Client/src/Construction/IRestEndpointDefinition.cs new file mode 100644 index 0000000..bdbe0f5 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestEndpointDefinition.cs @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestEndpointDefinition.cs +* +* IRestEndpointDefinition.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents an object that will define endpoint adapters for a + /// + public interface IRestEndpointDefinition + { + /// + /// Called to build endpoint adapters for the site + /// + /// The site that the adapters are build built for + /// The endpoint builder instance + void BuildRequest(IRestSiteAdapter site, IRestEndpointBuilder builder); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestRequestBody.cs b/lib/Net.Rest.Client/src/Construction/IRestRequestBody.cs new file mode 100644 index 0000000..7ce1a42 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestRequestBody.cs @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestRequestBody.cs +* +* IRestRequestBody.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Allows a request entity to configure its request message + /// + public interface IRestRequestBody + { + /// + /// Called when the request is being built to define a message body + /// + /// The request to configure + void AddBody(RestRequest request); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestRequestBuilder.cs b/lib/Net.Rest.Client/src/Construction/IRestRequestBuilder.cs new file mode 100644 index 0000000..be49ef3 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestRequestBuilder.cs @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestRequestBuilder.cs +* +* IRestRequestBuilder.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using System; + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// A type used to define operations required to generate requests to a REST endpoint + /// + /// The request entity/model type + public interface IRestRequestBuilder + { + /// + /// Defines a callback method that gets the request url for a given endpoint + /// + /// + /// The chainable + IRestRequestBuilder WithUrl(Func uriBuilder); + + /// + /// Adds a request message handler/converter, callback method used to modify + /// the request message programatically. + /// + /// + /// The chainable + IRestRequestBuilder WithModifier(Action requestBuilder); + + /// + /// Adds a response handler callback method that will be invoked when a response + /// is received from the endpoint. This method may be used to validate a response message + /// + /// + /// The chainable + IRestRequestBuilder OnResponse(Action onResponseBuilder); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestSingleEndpoint.cs b/lib/Net.Rest.Client/src/Construction/IRestSingleEndpoint.cs new file mode 100644 index 0000000..dd39445 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestSingleEndpoint.cs @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestSingleEndpoint.cs +* +* IRestSingleEndpoint.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Allows a request entity to configure its own endpoint definition + /// + public interface IRestSingleEndpoint + { + /// + /// Gets the endpoint url to execute the REST request against + /// + string Url { get; } + + /// + /// The request method type + /// + Method Method { get; } + + /// + /// Allows manually configuring the request before execution + /// + /// The request to configure + void OnRequest(RestRequest request); + + /// + /// Called when a response is received, and may be used to validate the response + /// message. + /// + /// The received response message + void OnResponse(RestResponse response); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestSiteAdapter.cs b/lib/Net.Rest.Client/src/Construction/IRestSiteAdapter.cs new file mode 100644 index 0000000..ee3c336 --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestSiteAdapter.cs @@ -0,0 +1,65 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestSiteAdapter.cs +* +* IRestSiteAdapter.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Threading; +using System.Threading.Tasks; + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents a remote REST api that defines endpoints to execute request/response + /// operations against, and configures RestClient's for use. + /// + public interface IRestSiteAdapter + { + /// + /// May be called during request execution to pause an operation to avoid + /// resource exhaustion and avoid dropped connections from overload + /// + /// + /// A task that will be awaited, when complete will continue the operation + Task WaitAsync(CancellationToken cancellation = default); + + /// + /// Gets a new that has a properly configured + /// + /// The new + ClientContract GetClient(); + + /// + /// Gets an for the given request entity type + /// + /// The request entity model + /// The used to build a request from an entity + IRestEndpointAdapter GetAdapter(); + + /// + /// Called after every successful REST operation. + /// + /// The response message returned from any endpoint + void OnResponse(RestResponse response); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/IRestSiteEndpointStore.cs b/lib/Net.Rest.Client/src/Construction/IRestSiteEndpointStore.cs new file mode 100644 index 0000000..11d648a --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/IRestSiteEndpointStore.cs @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: IRestSiteEndpointStore.cs +* +* IRestSiteEndpointStore.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents a type used to store instances + /// for a + /// + public interface IRestSiteEndpointStore + { + /// + /// The that adapters will be stored in + /// + IRestSiteAdapter Site { get; } + + /// + /// Adds an adapter for the given request entity type to be used fore + /// REST operations from the given entity model. + /// + /// The request entity model for the endpoint + /// The adapter to add to the store + void AddAdapter(IRestEndpointAdapter adapter); + } +} diff --git a/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs b/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs new file mode 100644 index 0000000..f7b234b --- /dev/null +++ b/lib/Net.Rest.Client/src/Construction/RestSiteAdapterBase.cs @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Net.Rest.Client +* File: RestSiteAdapterBase.cs +* +* RestSiteAdapterBase.cs is part of VNLib.Net.Rest.Client which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.Net.Rest.Client is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Net.Rest.Client is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Net.Rest.Client. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Collections.Generic; + +using RestSharp; + +namespace VNLib.Net.Rest.Client.Construction +{ + /// + /// Represents a remote REST site adapter, that handles the basic housekeeping of + /// creating and handling operations + /// + public abstract class RestSiteAdapterBase : IRestSiteAdapter, IRestSiteEndpointStore + { + /// + /// The collection of stored by their model type + /// + protected Dictionary Adapters { get; } = new(); + + /// + /// The internal client pool + /// + protected abstract RestClientPool Pool { get; } + + /// + public IRestSiteAdapter Site => this; + + /// + public virtual IRestEndpointAdapter GetAdapter() => (IRestEndpointAdapter)Adapters[typeof(TModel)]; + + /// + public virtual ClientContract GetClient() => Pool.Lease(); + + /// + public abstract void OnResponse(RestResponse response); + + /// + public abstract Task WaitAsync(CancellationToken cancellation = default); + + /// + /// Adds an endpoint adapter to the store with the given model type + /// + /// The endpoint model type + /// The model typed adapter to add to the store + public virtual void AddAdapter(IRestEndpointAdapter adapter) => Adapters[typeof(TModel)] = adapter; + } +} -- cgit