-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Rest classes to Management project
- Loading branch information
1 parent
2dff80e
commit 33f0813
Showing
15 changed files
with
137 additions
and
20 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/Microsoft.Azure.SignalR.Common/Auth/MicrosoftEntra/MicrosoftEntraAccessKey.Rest.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,117 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.SignalR.Common; | ||
|
||
namespace Microsoft.Azure.SignalR; | ||
|
||
#nullable enable | ||
|
||
internal partial class MicrosoftEntraAccessKey | ||
{ | ||
internal sealed class RestApiEndpoint | ||
{ | ||
public string Audience { get; } | ||
|
||
public string Token { get; } | ||
|
||
public RestApiEndpoint(string endpoint, string token) | ||
{ | ||
Audience = endpoint; | ||
Token = token; | ||
} | ||
} | ||
|
||
internal sealed class RestClient | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public RestClient(IHttpClientFactory httpClientFactory) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
public Task SendAsync( | ||
RestApiEndpoint api, | ||
HttpMethod httpMethod, | ||
Func<HttpResponseMessage, Task<bool>>? handleExpectedResponseAsync = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return SendAsyncCore(Constants.HttpClientNames.UserDefault, api, httpMethod, handleExpectedResponseAsync, cancellationToken); | ||
} | ||
|
||
private async Task ThrowExceptionOnResponseFailureAsync(HttpResponseMessage response) | ||
{ | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
return; | ||
} | ||
|
||
var detail = await response.Content.ReadAsStringAsync(); | ||
|
||
#if NET5_0_OR_GREATER | ||
var innerException = new HttpRequestException( | ||
$"Response status code does not indicate success: {(int)response.StatusCode} ({response.ReasonPhrase})", null, response.StatusCode); | ||
#else | ||
var innerException = new HttpRequestException( | ||
$"Response status code does not indicate success: {(int)response.StatusCode} ({response.ReasonPhrase})"); | ||
#endif | ||
throw response.StatusCode switch | ||
{ | ||
HttpStatusCode.BadRequest => new AzureSignalRInvalidArgumentException(response.RequestMessage?.RequestUri?.ToString(), innerException, detail), | ||
HttpStatusCode.Unauthorized => new AzureSignalRUnauthorizedException(response.RequestMessage?.RequestUri?.ToString(), innerException), | ||
HttpStatusCode.NotFound => new AzureSignalRInaccessibleEndpointException(response.RequestMessage?.RequestUri?.ToString(), innerException), | ||
_ => new AzureSignalRRuntimeException(response.RequestMessage?.RequestUri?.ToString(), innerException), | ||
}; | ||
} | ||
|
||
private async Task SendAsyncCore( | ||
string httpClientName, | ||
RestApiEndpoint api, | ||
HttpMethod httpMethod, | ||
Func<HttpResponseMessage, Task<bool>>? handleExpectedResponseAsync = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
using var httpClient = _httpClientFactory.CreateClient(httpClientName); | ||
using var request = BuildRequest(api, httpMethod); | ||
|
||
try | ||
{ | ||
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); | ||
if (handleExpectedResponseAsync == null) | ||
{ | ||
await ThrowExceptionOnResponseFailureAsync(response); | ||
} | ||
else | ||
{ | ||
if (!await handleExpectedResponseAsync(response)) | ||
{ | ||
await ThrowExceptionOnResponseFailureAsync(response); | ||
} | ||
} | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
throw new AzureSignalRException($"An error happened when making request to {request.RequestUri}", ex); | ||
} | ||
} | ||
|
||
private HttpRequestMessage BuildRequest(RestApiEndpoint api, HttpMethod httpMethod) | ||
{ | ||
return GenerateHttpRequest(api.Audience, httpMethod, api.Token); | ||
} | ||
|
||
private HttpRequestMessage GenerateHttpRequest(string url, HttpMethod httpMethod, string tokenString) | ||
{ | ||
var request = new HttpRequestMessage(httpMethod, new Uri(url)); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenString); | ||
return request; | ||
} | ||
} | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
...R.Common/Utilities/Rest/PayloadMessage.cs → ...SignalR.Management/Rest/PayloadMessage.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
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
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