Summary

Class:CBAM.HTTP.Implementation.HTTPNetworkConnectionPoolProvider`1
Assembly:CBAM.HTTP.Implementation
File(s):/repo-dir/contents/Source/Code/CBAM.HTTP.Implementation/ConnectionFactory.cs
Covered lines:32
Uncovered lines:13
Coverable lines:45
Total lines:209
Line coverage:71.1%
Branch coverage:0%

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.cctor()101%0%
.ctor()100%0%
TransformFactoryParameters(...)800%0%
CreateFactory()100%0%

File(s)

/repo-dir/contents/Source/Code/CBAM.HTTP.Implementation/ConnectionFactory.cs

#LineLine coverage
 1/*
 2 * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
 3 *
 4 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 5 * you may not use  this file  except in  compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 *   http://www.apache.org/licenses/LICENSE-2.0
 9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed  under the  License is distributed on an "AS IS" BASIS,
 12 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 13 * implied.
 14 *
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18using CBAM.Abstractions.Implementation;
 19using CBAM.Abstractions.Implementation.NetworkStream;
 20using CBAM.HTTP;
 21using CBAM.HTTP.Implementation;
 22using IOUtils.Network.Configuration;
 23using ResourcePooling.Async.Abstractions;
 24using System;
 25using System.Collections.Generic;
 26using System.IO;
 27using System.Net;
 28using System.Text;
 29using System.Threading;
 30using System.Threading.Tasks;
 31using UtilPack;
 32
 33namespace CBAM.HTTP.Implementation
 34{
 35   /// <summary>
 36   /// This class provides static <see cref="Factory"/> property to create connection pools that provide instances of <s
 37   /// This class also can be used dynamically by some other code, and then use <see cref="AsyncResourceFactoryProvider.
 38   /// The configuration type for this factory class is <see cref="HTTPNetworkCreationInfo"/>.
 39   /// For simpler configuration support, see <see cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/>
 40   /// </summary>
 41   /// <typeparam name="TRequestMetaData">The type of metadata associated with each request, used in identifying the req
 42   /// <seealso cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/>
 43   public sealed class HTTPNetworkConnectionPoolProvider<TRequestMetaData> : AbstractAsyncResourceFactoryProvider<HTTPCo
 44   {
 45
 46      /// <summary>
 47      /// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances 
 48      /// </summary>
 49      /// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="HTTPConnection{T
 50      /// <remarks>
 51      /// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound 
 52      /// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical 
 53      /// </remarks>
 554      public static AsyncResourceFactory<HTTPConnection<TRequestMetaData>, HTTPNetworkCreationInfo> Factory { get; } = n
 355            .NewFactoryParametrizer<HTTPNetworkCreationInfo, HTTPNetworkCreationInfoData, HTTPConnectionConfiguration, H
 356            .BindPublicConnectionType<HTTPConnection<TRequestMetaData>>()
 357            .CreateStatelessDelegatingConnectionFactory(
 358               Encoding.ASCII.CreateDefaultEncodingInfo(),
 359               ( parameters, encodingInfo, stringPool, stringPoolIsDedicated ) =>
 360               {
 561                  return TaskUtils.TaskFromBoolean( ( parameters.CreationData?.Connection?.ConnectionSSLMode ?? Connecti
 362               },
 363               null,
 364               null,
 365               null,
 366               null,
 367               null,
 368               ( parameters, encodingInfo, stringPool, stringPoolIsDedicated, stream, socketOrNull, token ) =>
 369               {
 570                  return new ValueTask<HTTPConnectionFunctionalityImpl<TRequestMetaData>>(
 571                     new HTTPConnectionFunctionalityImpl<TRequestMetaData>(
 572                        HTTPConnectionVendorImpl<TRequestMetaData>.Instance,
 573                        new ClientProtocolIOState(
 574                           stream,
 575                           stringPool,
 576                           encodingInfo,
 577                           new WriteState(),
 578                           new ReadState()
 579                           ) )
 580                        );
 381               },
 582               functionality => new ValueTask<HTTPConnectionImpl<TRequestMetaData>>( new HTTPConnectionImpl<TRequestMeta
 583               ( functionality, connection ) => new StatelessConnectionAcquireInfo<HTTPConnectionImpl<TRequestMetaData>,
 384               ( functionality, connection, token, error ) => functionality?.Stream
 385               ) );
 86
 87      /// <summary>
 88      /// Creates a new instance of <see cref="HTTPNetworkConnectionPoolProvider{TRequestMetaData}"/>.
 89      /// </summary>
 90      /// <remarks>
 91      /// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically lo
 92      /// </remarks>
 93      public HTTPNetworkConnectionPoolProvider()
 094         : base( typeof( HTTPNetworkCreationInfoData ) )
 95      {
 096      }
 97
 98      /// <inheritdoc />
 99      protected override HTTPNetworkCreationInfo TransformFactoryParameters( Object creationParameters )
 100      {
 0101         ArgumentValidator.ValidateNotNull( nameof( creationParameters ), creationParameters );
 102
 103         HTTPNetworkCreationInfo retVal;
 0104         switch ( creationParameters )
 105         {
 106            case HTTPNetworkCreationInfoData creationData:
 0107               retVal = new HTTPNetworkCreationInfo( creationData );
 0108               break;
 109            case HTTPNetworkCreationInfo creationInfo:
 0110               retVal = creationInfo;
 0111               break;
 112            case SimpleHTTPConfiguration simpleConfig:
 0113               retVal = simpleConfig.CreateNetworkCreationInfo();
 0114               break;
 115            default:
 0116               throw new ArgumentException( $"The {nameof( creationParameters )} must be instance of {typeof( HTTPNetwor
 117         }
 0118         return retVal;
 119      }
 120
 121      /// <summary>
 122      /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}.
 123      /// </summary>
 124      /// <returns>The value of <see cref="Factory"/> static property.</returns>
 125      protected override AsyncResourceFactory<HTTPConnection<TRequestMetaData>, HTTPNetworkCreationInfo> CreateFactory()
 0126         => Factory;
 127   }
 128
 129   /// <summary>
 130   /// This class provides static <see cref="Factory"/> property to create connection pools that provide instances of <s
 131   /// This class also can be used dynamically by some other code, and then use <see cref="AsyncResourceFactoryProvider.
 132   /// The configuration type for this factory class is <see cref="SimpleHTTPConfiguration"/>.
 133   /// For more advanced and customizable configuration support, see <see cref="HTTPNetworkConnectionPoolProvider{TReque
 134   /// </summary>
 135   /// <typeparam name="TRequestMetaData">The type of metadata associated with each request, used in identifying the req
 136   /// <seealso cref="HTTPNetworkConnectionPoolProvider{TRequestMetaData}"/>
 137   public sealed class HTTPSimpleConfigurationPoolProvider<TRequestMetaData> : AbstractAsyncResourceFactoryProvider<HTTP
 138   {
 139      /// <summary>
 140      /// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances 
 141      /// </summary>
 142      /// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="HTTPConnection{T
 143      /// <remarks>
 144      /// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound 
 145      /// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical 
 146      /// </remarks>
 147      public static AsyncResourceFactory<HTTPConnection<TRequestMetaData>, SimpleHTTPConfiguration> Factory { get; } = n
 148
 149      /// <summary>
 150      /// Creates a new instance of <see cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/>.
 151      /// </summary>
 152      /// <remarks>
 153      /// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically lo
 154      /// </remarks>
 155      public HTTPSimpleConfigurationPoolProvider()
 156         : base( typeof( SimpleHTTPConfiguration ) )
 157      {
 158      }
 159
 160      /// <inheritdoc />
 161      protected override SimpleHTTPConfiguration TransformFactoryParameters( Object creationParameters )
 162      {
 163         ArgumentValidator.ValidateNotNull( nameof( creationParameters ), creationParameters );
 164
 165         SimpleHTTPConfiguration retVal;
 166         switch ( creationParameters )
 167         {
 168            case SimpleHTTPConfiguration simpleConfig:
 169               retVal = simpleConfig;
 170               break;
 171            default:
 172               throw new ArgumentException( $"The {nameof( creationParameters )} must be instance of { typeof( SimpleHTT
 173         }
 174         return retVal;
 175      }
 176
 177      /// <summary>
 178      /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}.
 179      /// </summary>
 180      /// <returns>The value of <see cref="Factory"/> static property.</returns>
 181      protected override AsyncResourceFactory<HTTPConnection<TRequestMetaData>, SimpleHTTPConfiguration> CreateFactory()
 182         => Factory;
 183   }
 184
 185
 186}
 187
 188public static partial class E_CBAM
 189{
 190   /// <summary>
 191   /// This is ease-of-life method to asynchronously create <see cref="HTTPConnection{TRequestMetaData}"/> pool, send on
 192   /// </summary>
 193   /// <param name="config">This <see cref="SimpleHTTPConfiguration"/>.</param>
 194   /// <param name="request">The request to send.</param>
 195   /// <param name="defaultEncoding">The default encoding to use when stringifying the response contents. If not specifi
 196   /// <returns>Asynchronously returns the <see cref="HTTPTextualResponseInfo"/> of the first response that server sends
 197   /// <exception cref="NullReferenceException">If this <see cref="SimpleHTTPConfiguration"/> is <c>null</c>.</exception
 198   /// <exception cref="ArgumentNullException">If <paramref name="request"/> is <c>null</c>.</exception>
 199   public static Task<HTTPTextualResponseInfo> CreatePoolAndReceiveTextualResponseAsync( this SimpleHTTPConfiguration co
 200   {
 201      ArgumentValidator.ValidateNotNullReference( config );
 202      ArgumentValidator.ValidateNotNull( nameof( request ), request );
 203      return HTTPSimpleConfigurationPoolProvider<Int64>
 204         .Factory
 205         .BindCreationParameters( config )
 206         .CreateOneTimeUseResourcePool()
 207         .UseResourceAsync( async conn => { return await conn.ReceiveOneTextualResponseAsync( request, defaultEncoding: 
 208   }
 209}