|
| 1 | +using System.Linq.Expressions; |
| 2 | +using System.Net.Http.Headers; |
| 3 | +using System.Reflection; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | + |
| 6 | +using static Hyperbee.Expressions.ExpressionExtensions; |
| 7 | + |
| 8 | +namespace Hyperbee.Expressions.Lab; |
| 9 | + |
| 10 | +public class FetchExpression : Expression, IDependencyInjectionExpression |
| 11 | +{ |
| 12 | + public Expression Url { get; } |
| 13 | + public Expression Method { get; } |
| 14 | + public Expression Headers { get; } |
| 15 | + public Expression Content { get; } |
| 16 | + public Expression ClientName { get; } |
| 17 | + |
| 18 | + private IServiceProvider _serviceProvider; |
| 19 | + |
| 20 | + public FetchExpression( |
| 21 | + Expression url, |
| 22 | + Expression method, |
| 23 | + Expression headers = null, |
| 24 | + Expression content = null, |
| 25 | + Expression clientName = null ) |
| 26 | + { |
| 27 | + ArgumentNullException.ThrowIfNull( url, nameof( url ) ); |
| 28 | + ArgumentNullException.ThrowIfNull( method, nameof( method ) ); |
| 29 | + |
| 30 | + if ( url.Type != typeof( string ) ) |
| 31 | + throw new ArgumentException( "Url must be of type string.", nameof( url ) ); |
| 32 | + |
| 33 | + if ( method.Type != typeof( HttpMethod ) ) |
| 34 | + throw new ArgumentException( "Method must be of type HttpMethod.", nameof( method ) ); |
| 35 | + |
| 36 | + if ( headers != null && headers.Type != typeof( Dictionary<string, string> ) ) |
| 37 | + throw new ArgumentException( "Headers must be of type Dictionary<string, string>.", nameof( headers ) ); |
| 38 | + |
| 39 | + Url = url; |
| 40 | + Method = method; |
| 41 | + Headers = headers; |
| 42 | + Content = content; |
| 43 | + ClientName = clientName; |
| 44 | + } |
| 45 | + |
| 46 | + public override Type Type => typeof( Task<HttpResponseMessage> ); |
| 47 | + public override ExpressionType NodeType => ExpressionType.Extension; |
| 48 | + public override bool CanReduce => true; |
| 49 | + |
| 50 | + private static readonly ConstructorInfo RequestConstructorInfo = typeof( HttpRequestMessage ).GetConstructor( [typeof( HttpMethod ), typeof( string )] ); |
| 51 | + |
| 52 | + public override Expression Reduce() |
| 53 | + { |
| 54 | + if ( _serviceProvider == null ) |
| 55 | + throw new InvalidOperationException( "IServiceProvider has not been set." ); |
| 56 | + |
| 57 | + // Create service provider constant |
| 58 | + var providerConst = Constant( _serviceProvider ); |
| 59 | + |
| 60 | + Expression resolveHttpClient; |
| 61 | + |
| 62 | + if ( ClientName != null ) |
| 63 | + { |
| 64 | + // Resolve IHttpClientFactory and call CreateClient(name) |
| 65 | + var getFactory = Call( |
| 66 | + typeof( ServiceProviderServiceExtensions ), |
| 67 | + nameof( ServiceProviderServiceExtensions.GetRequiredService ), |
| 68 | + [typeof( IHttpClientFactory )], |
| 69 | + providerConst |
| 70 | + ); |
| 71 | + |
| 72 | + var factoryVar = Variable( typeof( IHttpClientFactory ), "factory" ); |
| 73 | + |
| 74 | + var assignFactory = Assign( factoryVar, getFactory ); |
| 75 | + var createClient = Call( factoryVar, nameof( IHttpClientFactory.CreateClient ), null, ClientName ); |
| 76 | + |
| 77 | + resolveHttpClient = Block( |
| 78 | + [factoryVar], |
| 79 | + assignFactory, |
| 80 | + createClient |
| 81 | + ); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + // Resolve HttpClient directly |
| 86 | + resolveHttpClient = Call( |
| 87 | + typeof( ServiceProviderServiceExtensions ), |
| 88 | + nameof( ServiceProviderServiceExtensions.GetRequiredService ), |
| 89 | + [typeof( HttpClient )], |
| 90 | + providerConst |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + var clientVar = Variable( typeof( HttpClient ), "client" ); |
| 95 | + var assignClient = Assign( clientVar, resolveHttpClient ); |
| 96 | + |
| 97 | + // Create HttpRequestMessage |
| 98 | + var requestVar = Variable( typeof( HttpRequestMessage ), "request" ); |
| 99 | + |
| 100 | + var requestCtor = New( |
| 101 | + RequestConstructorInfo, |
| 102 | + Method, |
| 103 | + Url |
| 104 | + ); |
| 105 | + var assignRequest = Assign( requestVar, requestCtor ); |
| 106 | + |
| 107 | + var variables = new List<ParameterExpression> { clientVar, requestVar }; |
| 108 | + var block = new List<Expression> { |
| 109 | + assignClient, |
| 110 | + assignRequest |
| 111 | + }; |
| 112 | + |
| 113 | + // Optional headers |
| 114 | + if ( Headers != null ) |
| 115 | + { |
| 116 | + var headersVar = Variable( typeof( Dictionary<string, string> ), "headers" ); |
| 117 | + variables.Add( headersVar ); |
| 118 | + block.Add( Assign( headersVar, Headers ) ); |
| 119 | + |
| 120 | + var kvp = Parameter( typeof( KeyValuePair<string, string> ), "kvp" ); |
| 121 | + |
| 122 | + var addHeader = Call( |
| 123 | + Property( requestVar, nameof( HttpRequestMessage.Headers ) ), |
| 124 | + nameof( HttpRequestHeaders.Add ), |
| 125 | + null, |
| 126 | + Property( kvp, nameof( KeyValuePair<string, string>.Key ) ), |
| 127 | + Property( kvp, nameof( KeyValuePair<string, string>.Value ) ) |
| 128 | + ); |
| 129 | + |
| 130 | + block.Add( ForEach( headersVar, kvp, addHeader ) ); |
| 131 | + } |
| 132 | + |
| 133 | + // Optional content |
| 134 | + if ( Content != null ) |
| 135 | + { |
| 136 | + block.Add( Assign( |
| 137 | + Property( requestVar, nameof( HttpRequestMessage.Content ) ), |
| 138 | + Content |
| 139 | + ) ); |
| 140 | + } |
| 141 | + |
| 142 | + // SendAsync call |
| 143 | + var sendCall = Call( clientVar, nameof( HttpClient.SendAsync ), null, requestVar ); |
| 144 | + |
| 145 | + return Block( |
| 146 | + variables, |
| 147 | + block.Append( sendCall ) |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + public void SetServiceProvider( IServiceProvider serviceProvider ) |
| 152 | + { |
| 153 | + _serviceProvider = serviceProvider; |
| 154 | + } |
| 155 | + |
| 156 | + protected override Expression VisitChildren( ExpressionVisitor visitor ) |
| 157 | + { |
| 158 | + return new FetchExpression( |
| 159 | + visitor.Visit( Url ), |
| 160 | + visitor.Visit( Method ), |
| 161 | + Headers != null ? visitor.Visit( Headers ) : null, |
| 162 | + Content != null ? visitor.Visit( Content ) : null, |
| 163 | + ClientName |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +public static partial class ExpressionExtensions |
| 169 | +{ |
| 170 | + public static FetchExpression Fetch( |
| 171 | + Expression clientName, |
| 172 | + Expression url ) |
| 173 | + { |
| 174 | + return new FetchExpression( url, Expression.Constant( HttpMethod.Get ), null, null, clientName ); |
| 175 | + } |
| 176 | + |
| 177 | + public static FetchExpression Fetch( Expression clientName, |
| 178 | + Expression url, |
| 179 | + Expression method, |
| 180 | + Expression content = null, |
| 181 | + Expression headers = null ) |
| 182 | + { |
| 183 | + return new FetchExpression( url, method, headers, content, clientName ); |
| 184 | + } |
| 185 | +} |
0 commit comments