Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Add initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
reisenberger committed Mar 26, 2018
1 parent ea55c82 commit 0ebd9bf
Show file tree
Hide file tree
Showing 18 changed files with 654 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Reflection;

[assembly: AssemblyProduct("Polly.Extensions.Http")]
[assembly: AssemblyCompany("App vNext")]
[assembly: AssemblyDescription("Polly.Extensions.Http is an extensions package containing opinionated convenience methods for configuring Polly policies to handle transient faults typical of calls through HttpClient.")]
[assembly: AssemblyCopyright("Copyright (c) 2018, App vNext")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<OutputTypeEx>library</OutputTypeEx>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE;RELEASE;NETCOREAPP1_1</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Polly-Signed" Version="5.8.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Polly.Extensions.Http.NetStandard11-Signed\Polly.Extensions.Http.NetStandard11-Signed.csproj" />
</ItemGroup>
<Import Project="..\Polly.Extensions.Http.SharedSpecs\Polly.Extensions.Http.SharedSpecs.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Xunit.CollectionBehavior(DisableTestParallelization = false)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Polly.Extensions.Http</AssemblyName>
<AssemblyOriginatorKeyFile></AssemblyOriginatorKeyFile>
<DefineConstants>TRACE;PORTABLE</DefineConstants>
<DefaultLanguage>en-US</DefaultLanguage>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>PolicyExtensions</RootNamespace>
<TargetFramework>netstandard1.1</TargetFramework>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<NuSpecFile Include="$(SolutionDir)Polly.Extensions.Http.nuspec">
<Visible>False</Visible>
</NuSpecFile>
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Polly-Signed" Version="5.8.0" />
</ItemGroup>
<Import Project="..\Polly.Extensions.Http.Shared\Polly.Extensions.Http.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Polly.Extensions.Http")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: CLSCompliant(true)]

[assembly: InternalsVisibleTo("Polly.Extensions.Http.NetStandard11-Signed.Specs")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<OutputTypeEx>library</OutputTypeEx>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE;RELEASE;NETCOREAPP1_1</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Polly" Version="5.8.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Polly.Extensions.Http.NetStandard11\Polly.Extensions.Http.NetStandard11.csproj" />
</ItemGroup>
<Import Project="..\Polly.Extensions.Http.SharedSpecs\Polly.Extensions.Http.SharedSpecs.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: Xunit.CollectionBehavior(DisableTestParallelization = false)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Polly.Extensions.Http</AssemblyName>
<AssemblyOriginatorKeyFile></AssemblyOriginatorKeyFile>
<DefineConstants>TRACE;PORTABLE</DefineConstants>
<DefaultLanguage>en-US</DefaultLanguage>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>PolicyExtensions</RootNamespace>
<TargetFramework>netstandard1.1</TargetFramework>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<NuSpecFile Include="$(SolutionDir)Polly.Extensions.Http.nuspec">
<Visible>False</Visible>
</NuSpecFile>
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Polly" Version="5.8.0" />
</ItemGroup>
<Import Project="..\Polly.Extensions.Http.Shared\Polly.Extensions.Http.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Polly.Extensions.Http")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: CLSCompliant(true)]

[assembly: InternalsVisibleTo("Polly.Extensions.Http.NetStandard11.Specs")]
115 changes: 115 additions & 0 deletions src/Polly.Extensions.Http.Shared/HttpPolicyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Net;
using System.Net.Http;

namespace Polly.Extensions.Http
{
/// <summary>
/// Contains opinionated convenience methods for configuring policies to handle conditions typically representing transient faults when making <see cref="HttpClient"/> requests.
/// </summary>
public static class HttpPolicyExtensions
{
private static readonly Func<HttpResponseMessage, bool> TransientHttpStatusCodePredicate = (response) =>
{
return (int)response.StatusCode >= 500 || response.StatusCode == HttpStatusCode.RequestTimeout;
};

/// <summary>
/// Builds a <see cref="PolicyBuilder{HttpResponseMessage}"/> to configure a <see cref="Policy{HttpResponseMessage}"/> which will handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure.
/// <para>The conditions configured to be handled are:
/// <list type="bullet">
/// <item><description>Network failures (as <see cref="HttpRequestException"/>)</description></item>
/// <item><description>HTTP 5XX status codes (server errors)</description></item>
/// <item><description>HTTP 408 status code (request timeout)</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>The <see cref="PolicyBuilder{HttpResponseMessage}"/> pre-configured to handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure. </returns>
public static PolicyBuilder<HttpResponseMessage> HandleTransientHttpError()
{
return Policy<HttpResponseMessage>.Handle<HttpRequestException>().OrTransientHttpStatusCode();
}

/// <summary>
/// Configures the <see cref="PolicyBuilder{HttpResponseMessage}"/> to handle <see cref="HttpClient"/> requests that fail with <see cref="HttpStatusCode"/>s indicating a transient failure.
/// <para>The <see cref="HttpStatusCode"/>s configured to be handled are:
/// <list type="bullet">
/// <item><description>HTTP 5XX status codes (server errors)</description></item>
/// <item><description>HTTP 408 status code (request timeout)</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>The <see cref="PolicyBuilder{HttpResponseMessage}"/> pre-configured to handle <see cref="HttpClient"/> requests that fail with <see cref="HttpStatusCode"/>s indicating a transient failure. </returns>
public static PolicyBuilder<HttpResponseMessage> OrTransientHttpStatusCode(this PolicyBuilder policyBuilder)
{
if (policyBuilder == null)
{
throw new ArgumentNullException(nameof(policyBuilder));
}

return policyBuilder.OrResult(TransientHttpStatusCodePredicate);
}

/// <summary>
/// Configures the <see cref="PolicyBuilder{HttpResponseMessage}"/> to handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure.
/// <para>The conditions configured to be handled are:
/// <list type="bullet">
/// <item><description>Network failures (as <see cref="HttpRequestException"/>)</description></item>
/// <item><description>HTTP 5XX status codes (server errors)</description></item>
/// <item><description>HTTP 408 status code (request timeout)</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>The <see cref="PolicyBuilder{HttpResponseMessage}"/> pre-configured to handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure. </returns>
public static PolicyBuilder<HttpResponseMessage> OrTransientHttpError(this PolicyBuilder policyBuilder)
{
if (policyBuilder == null)
{
throw new ArgumentNullException(nameof(policyBuilder));
}

return policyBuilder.Or<HttpRequestException>().OrTransientHttpStatusCode();
}

/// <summary>
/// Configures the <see cref="PolicyBuilder{HttpResponseMessage}"/> to handle <see cref="HttpClient"/> requests that fail with <see cref="HttpStatusCode"/>s indicating a transient failure.
/// <para>The <see cref="HttpStatusCode"/>s configured to be handled are:
/// <list type="bullet">
/// <item><description>HTTP 5XX status codes (server errors)</description></item>
/// <item><description>HTTP 408 status code (request timeout)</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>The <see cref="PolicyBuilder{HttpResponseMessage}"/> pre-configured to handle <see cref="HttpClient"/> requests that fail with <see cref="HttpStatusCode"/>s indicating a transient failure. </returns>
public static PolicyBuilder<HttpResponseMessage> OrTransientHttpStatusCode(this PolicyBuilder<HttpResponseMessage> policyBuilder)
{
if (policyBuilder == null)
{
throw new ArgumentNullException(nameof(policyBuilder));
}

return policyBuilder.OrResult(TransientHttpStatusCodePredicate);
}

/// <summary>
/// Configures the <see cref="PolicyBuilder{HttpResponseMessage}"/> to handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure.
/// <para>The conditions configured to be handled are:
/// <list type="bullet">
/// <item><description>Network failures (as <see cref="HttpRequestException"/>)</description></item>
/// <item><description>HTTP 5XX status codes (server errors)</description></item>
/// <item><description>HTTP 408 status code (request timeout)</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>The <see cref="PolicyBuilder{HttpResponseMessage}"/> pre-configured to handle <see cref="HttpClient"/> requests that fail with conditions indicating a transient failure. </returns>
public static PolicyBuilder<HttpResponseMessage> OrTransientHttpError(this PolicyBuilder<HttpResponseMessage> policyBuilder)
{
if (policyBuilder == null)
{
throw new ArgumentNullException(nameof(policyBuilder));
}

return policyBuilder.Or<HttpRequestException>().OrTransientHttpStatusCode();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>23fa87f9-c77d-4c67-a0b0-2901de51b3ff</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>Polly.Extensions.Http.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)HttpPolicyExtensions.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>23fa87f9-c77d-4c67-a0b0-2901de51b3ff</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="Polly.Extensions.Http.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
Loading

0 comments on commit 0ebd9bf

Please sign in to comment.