Skip to content

Commit

Permalink
put Message on Response instead of classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
annelo-msft committed Aug 23, 2021
1 parent ff0c952 commit 2338b56
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
4 changes: 2 additions & 2 deletions sdk/core/Azure.Core/src/Pipeline/HttpPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ValueTask SendAsync(HttpMessage message, CancellationToken cancellationTo
message.CancellationToken = cancellationToken;
AddHttpMessageProperties(message);
var value = _pipeline.Span[0].ProcessAsync(message, _pipeline.Slice(1));
message.Response.ResponseClassifier = ResponseClassifier;
message.Response.Message = message;
return value;
}

Expand All @@ -85,7 +85,7 @@ public void Send(HttpMessage message, CancellationToken cancellationToken)
message.CancellationToken = cancellationToken;
AddHttpMessageProperties(message);
_pipeline.Span[0].Process(message, _pipeline.Slice(1));
message.Response.ResponseClassifier = ResponseClassifier;
message.Response.Message = message;
}
/// <summary>
/// Invokes the pipeline asynchronously with the provided request.
Expand Down
14 changes: 11 additions & 3 deletions sdk/core/Azure.Core/src/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,22 @@ public virtual BinaryData Content
/// <returns>The <see cref="IEnumerable{T}"/> enumerating <see cref="HttpHeader"/> in the response.</returns>
protected internal abstract IEnumerable<HttpHeader> EnumerateHeaders();

internal ResponseClassifier? ResponseClassifier { get; set; }
internal HttpMessage? Message { get; set; }

/// <summary>
/// </summary>
/// <returns></returns>
public bool IsError()
{
return this.Message!.ResponseClassifier.IsErrorResponse(this.Message);
}

/// <summary>
/// Throw a RequestFailedException appropriate to the Response.
/// </summary>
public void Throw()
{
throw this.ResponseClassifier!.CreateRequestFailedException(this);
throw this.Message!.ResponseClassifier.CreateRequestFailedException(this);
//throw new RequestFailedException("<error message>");
}

Expand All @@ -129,7 +137,7 @@ public void Throw()
/// </summary>
public async Task ThrowAsync()
{
throw await this.ResponseClassifier!.CreateRequestFailedExceptionAsync(this).ConfigureAwait(false);
throw await this.Message!.ResponseClassifier.CreateRequestFailedExceptionAsync(this).ConfigureAwait(false);
//throw new RequestFailedException("<error message>");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public static implicit operator RequestContent(SynapseRoleAssignment value) => R

public static implicit operator SynapseRoleAssignment(Response response)
{
switch (response.Status)
if (!response.IsError())
{
case 200:
return DeserializeResponse(response);
default:
return DeserializeResponse(response);
}
else
{
response.Throw(); // What to do about Async here? Can you put awaits in operators?
}

// we should never get here, since we throw in the else statement above.
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new NotImplementedException();
throw new NotSupportedException();
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations

//response.Throw();
//break;
}
}

private static SynapseRoleAssignment DeserializeResponse(Response response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,29 @@ public async Task CreateRoleAssignment_ModelTypeParameterOverload()

SynapseRoleAssignment roleAssignment = new SynapseRoleAssignment(roleId, principalId, scope);

SynapseRoleAssignment returnedRoleAssignment = await client.CreateRoleAssignmentAsync(roleAssignmentId, roleAssignment, new RequestOptions()
// Calling the LLC directly:
Response response = await client.CreateRoleAssignmentAsync(roleAssignmentId, roleAssignment,
new RequestOptions()
{
StatusOption = ResponseStatusOption.NoThrow
});

// This is the implicit cast -- it will throw if the response is an error
// according to the classifier
SynapseRoleAssignment returnedRoleAssignment = response;

// But since we suppressed the error, we might want to think
// about it the following way:
if (response.IsError())
{
StatusOption = ResponseStatusOption.NoThrow
});

// TODO: Finish this test and figure out the rest.

await using DisposableClientRole role = await DisposableClientRole.Create(client, TestEnvironment);

Assert.NotNull(role.Assignment.Id);
Assert.NotNull(role.Assignment.Properties.RoleDefinitionId);
Assert.NotNull(role.Assignment.Properties.PrincipalId);
await response.ThrowAsync();
}
else
{
Assert.NotNull(returnedRoleAssignment.Id);
Assert.NotNull(returnedRoleAssignment.Properties.RoleDefinitionId);
Assert.NotNull(returnedRoleAssignment.Properties.PrincipalId);
}
}

[Test]
Expand Down

0 comments on commit 2338b56

Please sign in to comment.