Exclude System.Runtime.CompilerServices.NullableContextAttribute (#66)

* HttpClient

* .

* .

* .
This commit is contained in:
Stef Heyenrath
2023-12-06 21:32:07 +01:00
committed by GitHub
parent 77f28ffacc
commit 059886d1ca
34 changed files with 1266 additions and 132 deletions
@@ -32,6 +32,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumerForAk
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumerViaNuGet", "src-examples\ProxyInterfaceConsumerViaNuGet\ProxyInterfaceConsumerViaNuGet.csproj", "{1EA000E4-6103-4577-8D98-BDDA3BE458A2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IHttpClient", "src\IHttpClient\IHttpClient.csproj", "{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppIHttpClient", "src-examples\ConsoleAppIHttpClient\ConsoleAppIHttpClient.csproj", "{3E93C092-0E42-4200-B71A-5EEE410FE1F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -74,6 +78,18 @@ Global
{1EA000E4-6103-4577-8D98-BDDA3BE458A2}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU
{1EA000E4-6103-4577-8D98-BDDA3BE458A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EA000E4-6103-4577-8D98-BDDA3BE458A2}.Release|Any CPU.Build.0 = Release|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.DebugAttach|Any CPU.ActiveCfg = Debug|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}.Release|Any CPU.Build.0 = Release|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.DebugAttach|Any CPU.ActiveCfg = Debug|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.DebugAttach|Any CPU.Build.0 = Debug|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E93C092-0E42-4200-B71A-5EEE410FE1F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -85,6 +101,8 @@ Global
{5F7DA2C5-B908-4B57-9F5F-BADF1216D89C} = {38BA087F-EDA1-4F8A-A140-85B84791B815}
{590908DF-A813-467A-94E4-3500020D0D54} = {38BA087F-EDA1-4F8A-A140-85B84791B815}
{1EA000E4-6103-4577-8D98-BDDA3BE458A2} = {38BA087F-EDA1-4F8A-A140-85B84791B815}
{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3} = {ED3DA9DD-1E07-444B-A2D7-2DBA280F96D4}
{3E93C092-0E42-4200-B71A-5EEE410FE1F5} = {38BA087F-EDA1-4F8A-A140-85B84791B815}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {585F071D-051D-441C-9C6B-226D9E15A1F5}
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\IHttpClient\IHttpClient.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,13 @@
using System.Net.Http;
using System.Net.Http.Json;
var httpClient = new HttpClient();
var httpClientProxy = new HttpClientProxy(httpClient);
var result = await httpClientProxy.GetAsync("https://www.google.nl");
var todo = await httpClientProxy.GetFromJsonAsync<Todo>("https://jsonplaceholder.typicode.com/todos/1");
var postResult = await httpClientProxy.PostAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 });
var patchResult = await httpClientProxy.PatchAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 400 });
var putResult = await httpClientProxy.PutAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 444 });
int x = 0;
@@ -0,0 +1,4 @@
public class Todo
{
public int Id { get; set; }
}
@@ -1,6 +1,6 @@
using System;
namespace DifferentNamespace
namespace ProxyInterfaceConsumer
{
public class Address
{
@@ -1,14 +1,13 @@
namespace ProxyInterfaceConsumer.Http
using System.Net.Http;
namespace ProxyInterfaceConsumer.Http;
[ProxyInterfaceGenerator.Proxy(typeof(HttpClient), true)]
public partial interface IHttpClient : IHttpMessageInvoker
{
[ProxyInterfaceGenerator.Proxy(typeof(System.Net.Http.HttpClient), true)]
public partial interface IHttpClient : IHttpMessageInvoker
{
}
}
[ProxyInterfaceGenerator.Proxy(typeof(System.Net.Http.HttpMessageInvoker))]
public partial interface IHttpMessageInvoker
{
}
[ProxyInterfaceGenerator.Proxy(typeof(HttpMessageInvoker))]
public partial interface IHttpMessageInvoker
{
}
@@ -0,0 +1,108 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
namespace ProxyInterfaceConsumer.Http;
public static class IHttpClientExtensions
{
#region PostAsJsonAsync
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
#endregion
#region GetFromJsonAsync
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, options, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, options, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, cancellationToken);
}
#endregion
}
@@ -1,5 +1,3 @@
using DifferentNamespace;
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(Address))]
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DifferentNamespace;
namespace ProxyInterfaceConsumer
{
@@ -71,12 +70,12 @@ namespace ProxyInterfaceConsumer
c++;
}
public int In_Out_Ref2(in Address a, out Address b, ref Address c)
{
b = new Address();
c.HouseNumber = 11;
return 404;
}
//public int In_Out_Ref2(in Address a, out Address b, ref Address c)
//{
// b = new Address();
// c.HouseNumber = 11;
// return 404;
//}
public void Void()
{
+78 -63
View File
@@ -1,86 +1,101 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using DifferentNamespace;
using System.Threading.Tasks;
using ProxyInterfaceConsumer.Http;
namespace ProxyInterfaceConsumer
namespace ProxyInterfaceConsumer;
public class Program
{
public class Program
private static JsonSerializerOptions JsonSerializerOptions = new ()
{
private static JsonSerializerOptions JsonSerializerOptions = new ()
WriteIndented = true
};
public static async Task Main()
{
var h = new HttpClient();
var ph = new HttpClientProxy(h);
var result = await ph.GetAsync("https://www.google.nl");
var todo = await ph.GetFromJsonAsync<Todo>("https://jsonplaceholder.typicode.com/todos/1");
var postResult = await h.PostAsJsonAsync<Todo>("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 });
var t = new TestProxy(new Test());
IPersonT<int> pT = new PersonTProxy<int>(new PersonT<int>());
pT.TVal = 1;
Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions));
Console.WriteLine(new string('-', 80));
//IPersonTT<int, Program> pTT = new PersonTTProxy<int, Program>(new PersonTT<int, Program>());
//pTT.TVal1 = 42;
//pTT.TVal2 = new Program();
//Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions));
//Console.WriteLine(new string('-', 80));
var ap = new AddressProxy(new Address { HouseNumber = 42 });
ap.HouseNumber = -1;
ap.MyEvent += delegate (object x, EventArgs a)
{
WriteIndented = true
};
public static void Main()
{
var t = new TestProxy(new Test());
IPerson p = new PersonProxy(new Person());
p.Name = "test";
p.HelloWorld("stef");
// p.Address = ap;
IPersonT<int> pT = new PersonTProxy<int>(new PersonT<int>());
pT.TVal = 1;
Console.WriteLine(JsonSerializer.Serialize(pT, JsonSerializerOptions));
Console.WriteLine(new string('-', 80));
Console.WriteLine("DefaultValue " + p.DefaultValue());
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
//IPersonTT<int, Program> pTT = new PersonTTProxy<int, Program>(new PersonTT<int, Program>());
//pTT.TVal1 = 42;
//pTT.TVal2 = new Program();
//Console.WriteLine(JsonSerializer.Serialize(pTT, JsonSerializerOptions));
//Console.WriteLine(new string('-', 80));
var ap = new AddressProxy(new Address { HouseNumber = 42 });
ap.HouseNumber = -1;
ap.MyEvent += delegate (object x, EventArgs a)
{
};
IPerson p = new PersonProxy(new Person());
p.Name = "test";
p.HelloWorld("stef");
// p.Address = ap;
Console.WriteLine("DefaultValue " + p.DefaultValue());
Console.WriteLine("DefaultValue " + p.DefaultValue(42));
Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions));
}
Console.WriteLine(JsonSerializer.Serialize(p, JsonSerializerOptions));
}
}
public class Test
public class Todo
{
public int Id { get; set; }
}
public class Test
{
public int Id { get; set; }
public Clazz C { get; }
public IList<Clazz> Cs { get; set; }
public int AddString(string s)
{
public int Id { get; set; }
public Clazz C { get; }
public IList<Clazz> Cs { get; set; }
public int AddString(string s)
{
return 600;
}
public Test AddTest(Test t)
{
return new Test();
}
public Clazz AddClazz(Clazz c)
{
return new Clazz();
}
return 600;
}
public sealed class Clazz
public Test AddTest(Test t)
{
public string Name { get; set; }
return new Test();
}
[ProxyInterfaceGenerator.Proxy(typeof(Test))]
public partial interface ITest
public Clazz AddClazz(Clazz c)
{
return new Clazz();
}
}
[ProxyInterfaceGenerator.Proxy(typeof(Clazz))]
public partial interface IClazz
{
}
public sealed class Clazz
{
public string Name { get; set; }
}
[ProxyInterfaceGenerator.Proxy(typeof(Test))]
public partial interface ITest
{
}
[ProxyInterfaceGenerator.Proxy(typeof(Clazz))]
public partial interface IClazz
{
}
@@ -1,33 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="IPersonTT.cs" />
<Compile Remove="PersonTT.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ProxyInterfaceSourceGenerator\ProxyInterfaceSourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<Folder Include="NewFolder\" />
</ItemGroup>
</Project>
+7
View File
@@ -0,0 +1,7 @@
// ReSharper disable once CheckNamespace
namespace System.Net.Http;
[ProxyInterfaceGenerator.Proxy(typeof(HttpClient), true)]
public partial interface IHttpClient : IHttpMessageInvoker
{
}
+54
View File
@@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.0.1-preview-01</Version>
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ProjectGuid>{38C2BB6E-EE23-4C4F-B8D5-A2AD592DE5E3}</ProjectGuid>
<Authors>Stef Heyenrath</Authors>
<Description></Description>
<Title>IHttpClient</Title>
<PackageId>IHttpClient</PackageId>
<Description>This project uses source generation to generate an IHttpClient interface and HttpClientProxy from the HttpClient to make it injectable and unit-testable.</Description>
<PackageTags>HttpClient;interface;IHttpClient;Proxy;HttpClientProxy</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<!--<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../../PackageReleaseNotes.txt"))</PackageReleaseNotes>-->
<PackageProjectUrl>https://github.com/StefH/ProxyInterfaceGenerator/src/IHttpClient</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/StefH/ProxyInterfaceGenerator/src/IHttpClient</RepositoryUrl>
<PackageReadmeFile>PackageReadme.md</PackageReadmeFile>
<PackageIcon>ihttpclient-icon.png</PackageIcon>
<!--<IncludeBuildOutput>false</IncludeBuildOutput>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
<DevelopmentDependency>true</DevelopmentDependency>
<ImplicitUsings>enable</ImplicitUsings>
<Configurations>Debug;Release</Configurations>-->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<None Include="../../resources/ihttpclient-icon.png" Pack="true" PackagePath="" />
<None Include="PackageReadme.md" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
<PackageReference Include="StringSyntaxPolyfill" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProxyInterfaceSourceGenerator\ProxyInterfaceSourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
@@ -0,0 +1,173 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable once CheckNamespace
namespace System.Net.Http.Json;
/// <inheritdoc cref="HttpClientJsonExtensions"/>
public static class IHttpClientJsonExtensions
{
#region PostAsJsonAsync
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PostAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
#endregion
#if NET7_0_OR_GREATER
#region PatchAsJsonAsync
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, cancellationToken);
}
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, cancellationToken);
}
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PatchAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
#endregion
#endif
#region PutAsJsonAsync
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PutAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
return client._Instance.PutAsJsonAsync(requestUri, value, options, cancellationToken);
}
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PutAsJsonAsync(requestUri, value, cancellationToken);
}
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
{
return client._Instance.PutAsJsonAsync(requestUri, value, cancellationToken);
}
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PutAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.PutAsJsonAsync(requestUri, value, jsonTypeInfo, cancellationToken);
}
#endregion
#region GetFromJsonAsync
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, options, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, options, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, context, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, jsonTypeInfo, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Type type, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);
}
public static Task<object?> GetFromJsonAsync(this IHttpClient client, Uri? requestUri, Type type, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, cancellationToken);
}
public static Task<TValue?> GetFromJsonAsync<TValue>(this IHttpClient client, Uri? requestUri, CancellationToken cancellationToken = default)
{
return client._Instance.GetFromJsonAsync<TValue>(requestUri, cancellationToken);
}
#endregion
}
+7
View File
@@ -0,0 +1,7 @@
// ReSharper disable once CheckNamespace
namespace System.Net.Http;
[ProxyInterfaceGenerator.Proxy(typeof(HttpMessageInvoker))]
public partial interface IHttpMessageInvoker
{
}
+17
View File
@@ -0,0 +1,17 @@
## Info
This project uses source generation to generate an `IHttpClient` interface and `HttpClientProxy` from the `HttpClient` to make it injectable and unit-testable.
All the methods and properties from the `HttpClient` are replicated to `IHttpClient`.
## Use it
``` c#
HttpClient httpClient = new HttpClient();
IHttpClient httpClientProxy = new HttpClientProxy(httpClient);
var result = await httpClientProxy.GetAsync("https://www.google.nl");
var todo = await httpClientProxy.GetFromJsonAsync<Todo>("https://jsonplaceholder.typicode.com/todos/1");
var postResult = await httpClientProxy.PostAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 123 });
var patchResult = await httpClientProxy.PatchAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 400 });
var putResult = await httpClientProxy.PutAsJsonAsync("https://jsonplaceholder.typicode.com/todos", new Todo { Id = 444 });
```
@@ -1,8 +0,0 @@
namespace ProxyInterfaceSourceGenerator.Constants;
internal static class InternalClassNames
{
public const string NullableAttribute = "System.Runtime.CompilerServices.NullableAttribute";
public const string AsyncStateMachineAttribute = "System.Runtime.CompilerServices.AsyncStateMachineAttribute";
}
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ProxyInterfaceSourceGenerator.Constants;
namespace ProxyInterfaceSourceGenerator.Extensions;
@@ -8,8 +7,9 @@ internal static class SymbolExtensions
{
private static readonly string[] ExcludedAttributes =
{
InternalClassNames.AsyncStateMachineAttribute ,
InternalClassNames.NullableAttribute
"System.Runtime.CompilerServices.NullableAttribute",
"System.Runtime.CompilerServices.NullableContextAttribute",
"System.Runtime.CompilerServices.AsyncStateMachineAttribute"
};
public static string GetAttributesPrefix(this ISymbol symbol)
@@ -198,7 +198,10 @@ using System;
private string GeneratePublicMethods(ClassSymbol targetClassSymbol, bool proxyBaseClasses)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses))
var methods = MemberHelper.GetPublicMethods(targetClassSymbol, proxyBaseClasses);
foreach (var method in methods)
{
var methodParameters = new List<string>();
var invokeParameters = new List<string>();
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.0.36</Version>
<Version>0.0.37</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<ProjectGuid>{12344228-91F4-4502-9595-39584E5ABB34}</ProjectGuid>
<LangVersion>10</LangVersion>
@@ -92,7 +92,7 @@ internal static class MemberHelper
}
var ownMembers = membersQuery.ToList();
var ownPropertyNames = ownMembers.Select(x => x.Name);
var ownMemberNames = ownMembers.Select(x => x.Name);
if (!proxyBaseClasses)
{
@@ -106,7 +106,7 @@ internal static class MemberHelper
{
var baseMembers = baseType.GetMembers().OfType<T>()
.Where(m => m.DeclaredAccessibility == Accessibility.Public)
.Where(x => !ownPropertyNames.Contains(x.Name));
.Where(x => !ownMemberNames.Contains(x.Name));
foreach (var filter in filters)
{
@@ -23,6 +23,11 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public void Dispose()
{
_Instance.Dispose();
}
@@ -0,0 +1,134 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IHttpClient
{
new System.Net.Http.HttpClient _Instance { get; }
System.Net.IWebProxy DefaultProxy { get; set; }
System.Net.Http.Headers.HttpRequestHeaders DefaultRequestHeaders { get; }
System.Version DefaultRequestVersion { get; set; }
System.Net.Http.HttpVersionPolicy DefaultVersionPolicy { get; set; }
System.Uri? BaseAddress { get; set; }
System.TimeSpan Timeout { get; set; }
long MaxResponseContentBufferSize { get; set; }
System.Threading.Tasks.Task<string> GetStringAsync(string? requestUri);
System.Threading.Tasks.Task<string> GetStringAsync(System.Uri? requestUri);
System.Threading.Tasks.Task<string> GetStringAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<string> GetStringAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri);
System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(System.Uri? requestUri);
System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(string? requestUri);
System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Uri? requestUri);
System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(string? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(string? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(string? requestUri);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(System.Uri? requestUri);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(string? requestUri, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken);
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request);
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption);
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken);
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken);
void CancelPendingRequests();
}
}
#nullable restore
@@ -0,0 +1,33 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IHttpMessageInvoker
{
System.Net.Http.HttpMessageInvoker _Instance { get; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken);
void Dispose();
}
}
#nullable restore
@@ -22,6 +22,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
void Dispose();
@@ -54,6 +54,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
System.Threading.Tasks.Task<string?> Method3Async();
void Dispose();
@@ -108,6 +108,11 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
return result__57684656;
}
public void Dispose()
{
_Instance.Dispose();
}
@@ -0,0 +1,410 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial class HttpClientProxy : ProxyInterfaceSourceGeneratorTests.Source.HttpMessageInvokerProxy, IHttpClient
{
public new System.Net.Http.HttpClient _Instance { get; }
public System.Net.Http.HttpMessageInvoker _InstanceHttpMessageInvoker { get; }
public System.Net.IWebProxy DefaultProxy { get => System.Net.Http.HttpClient.DefaultProxy; set => System.Net.Http.HttpClient.DefaultProxy = value; }
public System.Net.Http.Headers.HttpRequestHeaders DefaultRequestHeaders { get => _Instance.DefaultRequestHeaders; }
public System.Version DefaultRequestVersion { get => _Instance.DefaultRequestVersion; set => _Instance.DefaultRequestVersion = value; }
public System.Net.Http.HttpVersionPolicy DefaultVersionPolicy { get => _Instance.DefaultVersionPolicy; set => _Instance.DefaultVersionPolicy = value; }
public System.Uri? BaseAddress { get => _Instance.BaseAddress; set => _Instance.BaseAddress = value; }
public System.TimeSpan Timeout { get => _Instance.Timeout; set => _Instance.Timeout = value; }
public long MaxResponseContentBufferSize { get => _Instance.MaxResponseContentBufferSize; set => _Instance.MaxResponseContentBufferSize = value; }
public System.Threading.Tasks.Task<string> GetStringAsync(string? requestUri)
{
string? requestUri_ = requestUri;
var result_1347886741 = _Instance.GetStringAsync(requestUri_);
return result_1347886741;
}
public System.Threading.Tasks.Task<string> GetStringAsync(System.Uri? requestUri)
{
System.Uri? requestUri_ = requestUri;
var result_1347886741 = _Instance.GetStringAsync(requestUri_);
return result_1347886741;
}
public System.Threading.Tasks.Task<string> GetStringAsync(string? requestUri, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1347886741 = _Instance.GetStringAsync(requestUri_, cancellationToken_);
return result_1347886741;
}
public System.Threading.Tasks.Task<string> GetStringAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1347886741 = _Instance.GetStringAsync(requestUri_, cancellationToken_);
return result_1347886741;
}
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri)
{
string? requestUri_ = requestUri;
var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_);
return result__1359336953;
}
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(System.Uri? requestUri)
{
System.Uri? requestUri_ = requestUri;
var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_);
return result__1359336953;
}
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(string? requestUri, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_, cancellationToken_);
return result__1359336953;
}
public System.Threading.Tasks.Task<byte[]> GetByteArrayAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1359336953 = _Instance.GetByteArrayAsync(requestUri_, cancellationToken_);
return result__1359336953;
}
public System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(string? requestUri)
{
string? requestUri_ = requestUri;
var result_355326142 = _Instance.GetStreamAsync(requestUri_);
return result_355326142;
}
public System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(string? requestUri, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_355326142 = _Instance.GetStreamAsync(requestUri_, cancellationToken_);
return result_355326142;
}
public System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Uri? requestUri)
{
System.Uri? requestUri_ = requestUri;
var result_355326142 = _Instance.GetStreamAsync(requestUri_);
return result_355326142;
}
public System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_355326142 = _Instance.GetStreamAsync(requestUri_, cancellationToken_);
return result_355326142;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri)
{
string? requestUri_ = requestUri;
var result_1805284658 = _Instance.GetAsync(requestUri_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri)
{
System.Uri? requestUri_ = requestUri;
var result_1805284658 = _Instance.GetAsync(requestUri_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1805284658 = _Instance.GetAsync(requestUri_, cancellationToken_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1805284658 = _Instance.GetAsync(requestUri_, cancellationToken_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(string? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_, cancellationToken_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> GetAsync(System.Uri? requestUri, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_1805284658 = _Instance.GetAsync(requestUri_, completionOption_, cancellationToken_);
return result_1805284658;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result__1705712948 = _Instance.PostAsync(requestUri_, content_);
return result__1705712948;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result__1705712948 = _Instance.PostAsync(requestUri_, content_);
return result__1705712948;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1705712948 = _Instance.PostAsync(requestUri_, content_, cancellationToken_);
return result__1705712948;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1705712948 = _Instance.PostAsync(requestUri_, content_, cancellationToken_);
return result__1705712948;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(string? requestUri, System.Net.Http.HttpContent? content)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result_182918739 = _Instance.PutAsync(requestUri_, content_);
return result_182918739;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result_182918739 = _Instance.PutAsync(requestUri_, content_);
return result_182918739;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_182918739 = _Instance.PutAsync(requestUri_, content_, cancellationToken_);
return result_182918739;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_182918739 = _Instance.PutAsync(requestUri_, content_, cancellationToken_);
return result_182918739;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(string? requestUri, System.Net.Http.HttpContent? content)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result_910894592 = _Instance.PatchAsync(requestUri_, content_);
return result_910894592;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
var result_910894592 = _Instance.PatchAsync(requestUri_, content_);
return result_910894592;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(string? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_910894592 = _Instance.PatchAsync(requestUri_, content_, cancellationToken_);
return result_910894592;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PatchAsync(System.Uri? requestUri, System.Net.Http.HttpContent? content, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Net.Http.HttpContent? content_ = content;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_910894592 = _Instance.PatchAsync(requestUri_, content_, cancellationToken_);
return result_910894592;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(string? requestUri)
{
string? requestUri_ = requestUri;
var result_534537427 = _Instance.DeleteAsync(requestUri_);
return result_534537427;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(System.Uri? requestUri)
{
System.Uri? requestUri_ = requestUri;
var result_534537427 = _Instance.DeleteAsync(requestUri_);
return result_534537427;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(string? requestUri, System.Threading.CancellationToken cancellationToken)
{
string? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_534537427 = _Instance.DeleteAsync(requestUri_, cancellationToken_);
return result_534537427;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> DeleteAsync(System.Uri? requestUri, System.Threading.CancellationToken cancellationToken)
{
System.Uri? requestUri_ = requestUri;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result_534537427 = _Instance.DeleteAsync(requestUri_, cancellationToken_);
return result_534537427;
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request)
{
System.Net.Http.HttpRequestMessage request_ = request;
var result__989347188 = _Instance.Send(request_);
return result__989347188;
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
var result__989347188 = _Instance.Send(request_, completionOption_);
return result__989347188;
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__989347188 = _Instance.Send(request_, cancellationToken_);
return result__989347188;
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__989347188 = _Instance.Send(request_, completionOption_, cancellationToken_);
return result__989347188;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request)
{
System.Net.Http.HttpRequestMessage request_ = request;
var result__1161702976 = _Instance.SendAsync(request_);
return result__1161702976;
}
public override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1161702976 = _Instance.SendAsync(request_, cancellationToken_);
return result__1161702976;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
var result__1161702976 = _Instance.SendAsync(request_, completionOption_);
return result__1161702976;
}
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Net.Http.HttpCompletionOption completionOption_ = completionOption;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1161702976 = _Instance.SendAsync(request_, completionOption_, cancellationToken_);
return result__1161702976;
}
public void CancelPendingRequests()
{
_Instance.CancelPendingRequests();
}
public HttpClientProxy(System.Net.Http.HttpClient instance) : base(instance)
{
_Instance = instance;
_InstanceHttpMessageInvoker = instance;
}
}
}
#nullable restore
@@ -0,0 +1,59 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial class HttpMessageInvokerProxy : IHttpMessageInvoker
{
public System.Net.Http.HttpMessageInvoker _Instance { get; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public virtual System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__989347188 = _Instance.Send(request_, cancellationToken_);
return result__989347188;
}
public virtual System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
System.Net.Http.HttpRequestMessage request_ = request;
System.Threading.CancellationToken cancellationToken_ = cancellationToken;
var result__1161702976 = _Instance.SendAsync(request_, cancellationToken_);
return result__1161702976;
}
public void Dispose()
{
_Instance.Dispose();
}
public HttpMessageInvokerProxy(System.Net.Http.HttpMessageInvoker instance)
{
_Instance = instance;
}
}
}
#nullable restore
@@ -448,4 +448,86 @@ public class ProxyInterfaceSourceGeneratorTest
c.Should().Be(101);
}
[Fact]
public void GenerateFiles_HttpClient()
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
var interfaceIHttpClientFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHttpClient.g.cs";
var proxyClassIHttpClientFilename = "System.Net.Http.HttpClientProxy.g.cs";
var interfaceIHttpMessageInvokerFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHttpMessageInvoker.g.cs";
var proxyClassIHttpMessageInvokerFilename = "System.Net.Http.HttpMessageInvokerProxy.g.cs";
var pathIHttpClient = "./Source/IHttpClient.cs";
var sourceFileIHttpClient = new SourceFile
{
Path = pathIHttpClient,
Text = File.ReadAllText(pathIHttpClient),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(System.Net.Http.HttpClient)"
}
};
var pathIHttpMessageInvoker = "./Source/IHttpMessageInvoker.cs";
var sourceFileIHttpMessageInvoker = new SourceFile
{
Path = pathIHttpMessageInvoker,
Text = File.ReadAllText(pathIHttpMessageInvoker),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(System.Net.Http.HttpMessageInvoker)"
}
};
// Act
var result = _sut.Execute(new[] { sourceFileIHttpClient, sourceFileIHttpMessageInvoker });
// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(5);
// Assert attribute
var attribute = result.Files[0].SyntaxTree;
attribute.FilePath.Should().EndWith(attributeFilename);
// Assert interface IHttpClient
var interfaceIHttpClient = result.Files[1].SyntaxTree;
interfaceIHttpClient.FilePath.Should().EndWith(interfaceIHttpClientFilename);
var interfaceCodeIHttpClient = interfaceIHttpClient.ToString();
if (Write) File.WriteAllText($"../../../Destination/{interfaceIHttpClientFilename}", interfaceCodeIHttpClient);
interfaceCodeIHttpClient.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceIHttpClientFilename}"));
// Assert interface IHttpMessageInvoker
var interfaceIMessageInvoker = result.Files[2].SyntaxTree;
interfaceIMessageInvoker.FilePath.Should().EndWith(interfaceIHttpMessageInvokerFilename);
var interfaceCodeIMessageInvoker = interfaceIMessageInvoker.ToString();
if (Write) File.WriteAllText($"../../../Destination/{interfaceIHttpMessageInvokerFilename}", interfaceCodeIMessageInvoker);
interfaceCodeIMessageInvoker.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceIHttpMessageInvokerFilename}"));
// Assert Proxy IHttpClient
var proxyClassIHttpClient = result.Files[3].SyntaxTree;
proxyClassIHttpClient.FilePath.Should().EndWith(proxyClassIHttpClientFilename);
var proxyCodeIHttpClient = proxyClassIHttpClient.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassIHttpClientFilename}", proxyCodeIHttpClient);
proxyCodeIHttpClient.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassIHttpClientFilename}"));
// Assert Proxy IHttpMessageInvoker
var proxyClassIMessageInvoker = result.Files[4].SyntaxTree;
proxyClassIMessageInvoker.FilePath.Should().EndWith(proxyClassIHttpMessageInvokerFilename);
var proxyIMessageInvoker = proxyClassIMessageInvoker.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}", proxyIMessageInvoker);
proxyIMessageInvoker.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}"));
}
}
@@ -5,5 +5,10 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public bool IsAlive { get; set; }
public string GetterOnly => "x";
public void Dispose()
{
}
}
}
@@ -0,0 +1,5 @@
namespace ProxyInterfaceSourceGeneratorTests.Source;
public partial interface IHttpClient : IHttpMessageInvoker
{
}
@@ -0,0 +1,5 @@
namespace ProxyInterfaceSourceGeneratorTests.Source;
public partial interface IHttpMessageInvoker
{
}