Skip to content

Commit

Permalink
Make ResponseExceptionExtensions, RetriableStream shared source (#7913)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Oct 3, 2019
1 parent 8004f67 commit 681e160
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" />
<Compile Include="$(AzureCoreSharedSources)ContentTypeUtilities.cs" />
<Compile Include="$(AzureCoreSharedSources)ConditionalRequestOptionsExtensions.cs" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" />
<Compile Include="$(AzureCoreSharedSources)NoBodyResponse{T}.cs" />
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" />
<Compile Include="$(AzureCoreSharedSources)ResponseExceptionExtensions.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private Request CreateSetRequest(ConfigurationSetting setting, ConditionalReques
request.Headers.Add(s_mediaTypeKeyValueApplicationHeader);
request.Headers.Add(HttpHeader.Common.JsonContentType);

if (requestOptions != default)
if (requestOptions != null)
{
ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions);
}
Expand Down Expand Up @@ -484,7 +484,7 @@ private Request CreateDeleteRequest(string key, string label, ConditionalRequest
request.Method = RequestMethod.Delete;
BuildUriForKvRoute(request.Uri, key, label);

if (requestOptions != default)
if (requestOptions != null)
{
ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions);
}
Expand Down Expand Up @@ -680,7 +680,7 @@ private Request CreateGetRequest(string key, string label, DateTimeOffset accept
request.Headers.Add(AcceptDatetimeHeader, dateTime);
}

if (requestOptions != default)
if (requestOptions != null)
{
ConditionalRequestOptionsExtensions.ApplyHeaders(request, requestOptions);
}
Expand Down
11 changes: 8 additions & 3 deletions sdk/core/Azure.Core/src/Shared/ContentTypeUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable
#nullable disable

using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Azure.Core.Pipeline
{
#if !TESTFRAMEWORK
internal static class ContentTypeUtilities
#else
#pragma warning disable SA1649 // File name should match first type name
internal static class TestFrameworkContentTypeUtilities
#pragma warning restore SA1649 // File name should match first type name
#endif
{
public static bool TryGetTextEncoding(string? contentType, [NotNullWhen(true)] out Encoding? encoding)
public static bool TryGetTextEncoding(string contentType, out Encoding encoding)
{
const string charsetMarker = "; charset=";
const string utf8Charset = "utf-8";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
Expand All @@ -9,16 +10,16 @@

namespace Azure.Core
{
public static class ResponseExceptionExtensions
internal static class ResponseExceptionExtensions
{
private const string DefaultMessage = "Service request failed.";

public static Task<RequestFailedException> CreateRequestFailedExceptionAsync(this Response response)
public static ValueTask<RequestFailedException> CreateRequestFailedExceptionAsync(this Response response)
{
return CreateRequestFailedExceptionAsync(response, DefaultMessage);
}

public static Task<RequestFailedException> CreateRequestFailedExceptionAsync(this Response response, string message)
public static ValueTask<RequestFailedException> CreateRequestFailedExceptionAsync(this Response response, string message)
{
return CreateRequestFailedExceptionAsync(message, response, true);
}
Expand All @@ -30,16 +31,18 @@ public static RequestFailedException CreateRequestFailedException(this Response

public static RequestFailedException CreateRequestFailedException(this Response response, string message)
{
return CreateRequestFailedExceptionAsync(message, response, false).EnsureCompleted();
ValueTask<RequestFailedException> messageTask = CreateRequestFailedExceptionAsync(message, response, false);
Debug.Assert(messageTask.IsCompleted);
return messageTask.GetAwaiter().GetResult();
}

public static async Task<RequestFailedException> CreateRequestFailedExceptionAsync(string message, Response response, bool async)
public static async ValueTask<RequestFailedException> CreateRequestFailedExceptionAsync(string message, Response response, bool async)
{
message = await CreateRequestFailedMessageAsync(message, response, async).ConfigureAwait(false);
return new RequestFailedException(response.Status, message);
}

public static async Task<string> CreateRequestFailedMessageAsync(string message, Response response, bool async)
public static async ValueTask<string> CreateRequestFailedMessageAsync(string message, Response response, bool async)
{
StringBuilder messageBuilder = new StringBuilder()
.AppendLine(message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

namespace Azure.Core.Pipeline
{
public static class RetriableStream
internal static class RetriableStream
{
public static Stream Create(
Func<long, Stream> responseFactory,
Expand Down Expand Up @@ -40,7 +43,7 @@ public static Stream Create(
return new RetriableStreamImpl(initialResponse, streamFactory, asyncResponseFactory, responseClassifier, maxRetries);
}

private class RetriableStreamImpl : ReadOnlyStream
private class RetriableStreamImpl : Stream
{
private readonly ResponseClassifier _responseClassifier;

Expand All @@ -58,7 +61,7 @@ private class RetriableStreamImpl : ReadOnlyStream

private int _retryCount;

private List<Exception>? _exceptions;
private List<Exception> _exceptions;

public RetriableStreamImpl(Stream initialStream, Func<long, Stream> streamFactory, Func<long, ValueTask<Stream>> asyncStreamFactory, ResponseClassifier responseClassifier, int maxRetries)
{
Expand Down Expand Up @@ -129,7 +132,9 @@ public override int Read(byte[] buffer, int offset, int count)
}
catch (Exception e)
{
RetryAsync(e, false).EnsureCompleted();
Task task = RetryAsync(e, false);
Debug.Assert(task.IsCompleted);
task.GetAwaiter().GetResult();
}
}
}
Expand All @@ -153,6 +158,23 @@ private static Stream EnsureStream(Stream stream)

return stream;
}

public override bool CanWrite => false;

public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}

public override void SetLength(long value)
{
throw new NotSupportedException();
}

public override void Flush()
{
throw new NotSupportedException();
}
}
}
}
6 changes: 5 additions & 1 deletion sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ProjectGuid>{84491222-6C36-4FA7-BBAE-1FA804129151}</ProjectGuid>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<DefineConstants>$(DefineConstants);HAS_INTERNALS_VISIBLE_CORE</DefineConstants>
<DefineConstants>$(DefineConstants);TESTFRAMEWORK;HAS_INTERNALS_VISIBLE_CORE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,4 +21,8 @@
<Name>Azure.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\src\Shared\ContentTypeUtilities.cs" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions sdk/core/Azure.Core/tests/TestFramework.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project ToolsVersion="15.0">
<PropertyGroup>
<SessionRecordsDirectory>SessionRecords</SessionRecordsDirectory>
<DefineConstants>$(DefineConstants);TESTFRAMEWORK</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/TestFramework/RecordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static bool IsTextContentType(IDictionary<string, string[]> requestHeade
{
encoding = null;
return TryGetContentType(requestHeaders, out string contentType) &&
ContentTypeUtilities.TryGetTextEncoding(contentType, out encoding);
TestFrameworkContentTypeUtilities.TryGetTextEncoding(contentType, out encoding);
}

public void Sanitize(RecordedTestSanitizer sanitizer)
Expand Down
2 changes: 2 additions & 0 deletions sdk/identity/Azure.Identity/src/Azure.Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" />
<Compile Include="$(AzureCoreSharedSources)ContentTypeUtilities.cs" />
<Compile Include="$(AzureCoreSharedSources)ResponseExceptionExtensions.cs" />
</ItemGroup>

<!-- Import the Azure.Base project -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

<!-- Import the Azure.Core project -->
<Import Project="$(MSBuildThisFileDirectory)..\..\..\core\Azure.Core\src\Azure.Core.props" />
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)Argument.cs" />
</ItemGroup>

<Import Project="..\..\Azure.Security.KeyVault.Shared\Azure.Security.KeyVault.Shared.projitems" Label="Shared" />

Expand All @@ -37,7 +34,10 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)Argument.cs" />
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" />
<Compile Include="$(AzureCoreSharedSources)ContentTypeUtilities.cs" />
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" />
<Compile Include="$(AzureCoreSharedSources)ResponseExceptionExtensions.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" />
<Compile Include="$(AzureCoreSharedSources)ContentTypeUtilities.cs" />
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" />
<Compile Include="$(AzureCoreSharedSources)ResponseExceptionExtensions.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" />
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" />
<Compile Include="$(AzureCoreSharedSources)ContentTypeUtilities.cs" />
<Compile Include="$(AzureCoreSharedSources)PageResponseEnumerator.cs" />
<Compile Include="$(AzureCoreSharedSources)ResponseExceptionExtensions.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Azure.Storage.Common\src\Azure.Storage.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)RetriableStream.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\Azure.Storage.Common\src\Azure.Storage.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)RetriableStream.cs" />
</ItemGroup>
</Project>

0 comments on commit 681e160

Please sign in to comment.