-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adapters to convert Azure.Core Response to SCM ClientResult to su…
…pport SSE streams using SCM CollectionResult types (#46827) * WIP on adapter * add response headers adapter * updates * updates
- Loading branch information
1 parent
7b3bff7
commit ee76588
Showing
6 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
sdk/ai/Azure.AI.Projects/src/Custom/Utility/ResponseAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.ClientModel.Primitives; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace Azure.AI.Projects; | ||
|
||
/// <summary> | ||
/// Adapts an Azure.Core Response to an SCM PipelineResponse. | ||
/// </summary> | ||
internal class ResponseAdapter : PipelineResponse | ||
{ | ||
private readonly Response _azureResponse; | ||
private PipelineResponseHeaders? _headers; | ||
|
||
public ResponseAdapter(Response azureResponse) | ||
{ | ||
_azureResponse = azureResponse; | ||
} | ||
|
||
public override int Status => _azureResponse.Status; | ||
|
||
public override string ReasonPhrase => _azureResponse.ReasonPhrase; | ||
|
||
public override Stream? ContentStream | ||
{ | ||
get => _azureResponse?.ContentStream; | ||
set => _azureResponse.ContentStream = value; | ||
} | ||
|
||
public override BinaryData Content => _azureResponse.Content; | ||
|
||
protected override PipelineResponseHeaders HeadersCore => | ||
_headers ??= new ResponseHeadersAdapter(_azureResponse.Headers); | ||
|
||
public override BinaryData BufferContent(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotSupportedException("Content buffering is not supported for SSE response streams."); | ||
} | ||
|
||
public override ValueTask<BinaryData> BufferContentAsync(CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotSupportedException("Content buffering is not supported for SSE response streams."); | ||
} | ||
|
||
public override void Dispose() => _azureResponse?.Dispose(); | ||
} |
37 changes: 37 additions & 0 deletions
37
sdk/ai/Azure.AI.Projects/src/Custom/Utility/ResponseHeadersAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using Azure.Core; | ||
|
||
#nullable enable | ||
|
||
namespace Azure.AI.Projects; | ||
|
||
/// <summary> | ||
/// Adapts an Azure.Core ResponseHeaders to an SCM PipelineResponseHeaders. | ||
/// </summary> | ||
internal class ResponseHeadersAdapter : PipelineResponseHeaders | ||
{ | ||
private readonly ResponseHeaders _azureHeaders; | ||
|
||
public ResponseHeadersAdapter(ResponseHeaders azureHeaders) | ||
{ | ||
_azureHeaders = azureHeaders; | ||
} | ||
|
||
public override IEnumerator<KeyValuePair<string, string>> GetEnumerator() | ||
{ | ||
foreach (HttpHeader header in _azureHeaders) | ||
{ | ||
yield return new KeyValuePair<string, string>(header.Name, header.Value); | ||
} | ||
} | ||
|
||
public override bool TryGetValue(string name, out string? value) | ||
=> _azureHeaders.TryGetValue(name, out value); | ||
|
||
public override bool TryGetValues(string name, out IEnumerable<string>? values) | ||
=> _azureHeaders.TryGetValue(name, out values); | ||
} |