Skip to content

Commit

Permalink
Make Response.IsError functional for clients (#24247)
Browse files Browse the repository at this point in the history
* use the Response.IsError property in an E2E test

* add ResponsePropertiesPolicy into pipeline in generated code.

* add test for model cast directly
  • Loading branch information
annelo-msft authored Sep 27, 2021
1 parent 7296cbd commit f1f4cb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Text.Json;
using Azure.Core.Pipeline;

namespace Azure.Core.Experimental.Tests.Models
{
Expand Down Expand Up @@ -33,15 +34,14 @@ internal Pet(string name, string species)
public static implicit operator Pet(Response response)
{
// [X] TODO: Add in HLC error semantics
// [ ] TODO: Use response.IsError
// [X] TODO: Use response.IsError
// [ ] TODO: Use throw new ResponseFailedException(response);
switch (response.Status)
if (response.IsError())
{
case 200:
return DeserializePet(JsonDocument.Parse(response.Content.ToMemory()));
default:
throw new RequestFailedException("Received a non-success status code.");
throw new RequestFailedException("Received a non-success status code.");
}

return DeserializePet(JsonDocument.Parse(response.Content.ToMemory()));
}

private static Pet DeserializePet(JsonDocument document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public PetStoreClient(Uri endpoint, TokenCredential credential, PetStoreClientOp
_clientDiagnostics = new ClientDiagnostics(options);
_tokenCredential = credential;
var authPolicy = new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes);
Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, new HttpPipelinePolicy[] { authPolicy }, new ResponseClassifier());

// When we move the IsError functionality into Core, we'll move the addition of ResponsePropertiesPolicy to the pipeline into HttpPipelineBuilder.
Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, new HttpPipelinePolicy[] { authPolicy, new ResponsePropertiesPolicy() }, new ResponseClassifier());
this.endpoint = endpoint;
apiVersion = options.Version;
}
Expand Down
14 changes: 14 additions & 0 deletions sdk/core/Azure.Core.Experimental/tests/LowLevelClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public async Task CanGetOutputModelOnSuccessCodeAsync()
Assert.AreEqual("beagle", pet.Species);
}

[Test]
public async Task ModelCastThrowsOnErrorCodeAsync()
{
var mockResponse = new MockResponse(404);

// Send the response through the pipeline so IsError is set.
var mockTransport = new MockTransport(mockResponse);
PetStoreClient client = CreateClient(mockTransport);

Response response = await client.GetPetAsync("pet1", ResponseStatusOption.NoThrow);

Assert.Throws<RequestFailedException>(() => { Pet pet = response; });
}

[Test]
public void CannotGetOutputModelOnFailureCodeAsync()
{
Expand Down

0 comments on commit f1f4cb9

Please sign in to comment.