Skip to content

Commit

Permalink
Merge pull request #900 from microsoftgraph/fix/async-suffix
Browse files Browse the repository at this point in the history
fix: corrects async suffixes where wrongly used
  • Loading branch information
baywet authored Aug 28, 2024
2 parents 75fb847 + 0e37d8a commit 0364e8c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 25 deletions.
21 changes: 21 additions & 0 deletions src/Microsoft.Graph.Core/Extensions/ITokenValidableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.Graph
using System.Linq;
using System.Threading.Tasks;
using System.IdentityModel.Tokens.Jwt;
using System.ComponentModel;

/// <summary>
/// Contains extension methods for <see cref="ITokenValidableExtension"/>
Expand All @@ -31,7 +32,27 @@ public static class ITokenValidableExtension
/// If you are not using the public cloud you need to pass the value corresponding to your national deployment.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="tenantIds"/> or <paramref name="appIds"/> is null or empty</exception>
/// <returns>Are tokens valid or not.</returns>
[Obsolete("This method is obsolete. Use the async version instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
public static async Task<bool> AreTokensValid<T1,T2>(this ITokenValidable<T1, T2> collection, IEnumerable<Guid> tenantIds, IEnumerable<Guid> appIds,
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
string wellKnownUri = "https://login.microsoftonline.com/common/.well-known/openid-configuration",
IEnumerable<string> issuerFormats = null) where T1 : IEncryptedContentBearer<T2> where T2 : IDecryptableContent
=> await AreTokensValidAsync(collection, tenantIds, appIds, wellKnownUri, issuerFormats).ConfigureAwait(false);
/// <summary>
/// Validates tokens attached with the notification collection. If the result is false, the notification collection should be discarded.
/// </summary>
/// <param name="collection">Collection instance of <see cref="ITokenValidable{T1,T2}"/></param>
/// <param name="tenantIds">List of tenant ids that notifications might be originating from.</param>
/// <param name="appIds">List of application id (client ids) that subscriptions have been created from.</param>
/// <param name="wellKnownUri">Well known URL to get the signing certificates for the tokens.
/// If you are not using the public cloud you need to pass the value corresponding to your national deployment.</param>
/// <param name="issuerFormats">Issuer formats for the "aud" claim in the tokens.
/// If you are not using the public cloud you need to pass the value corresponding to your national deployment.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="tenantIds"/> or <paramref name="appIds"/> is null or empty</exception>
/// <returns>Are tokens valid or not.</returns>
public static async Task<bool> AreTokensValidAsync<T1,T2>(this ITokenValidable<T1, T2> collection, IEnumerable<Guid> tenantIds, IEnumerable<Guid> appIds,
string wellKnownUri = "https://login.microsoftonline.com/common/.well-known/openid-configuration",
IEnumerable<string> issuerFormats = null) where T1 : IEncryptedContentBearer<T2> where T2 : IDecryptableContent
{
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.11.3" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.12.2" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.11.3" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="[6.0,9.0)" />
Expand Down
20 changes: 10 additions & 10 deletions src/Microsoft.Graph.Core/Requests/DeltaResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public async Task<ModelType> HandleResponseAsync<NativeResponseType, ModelType>(
{
// Gets the response string with response headers and status code
// set on the response body object.
var responseString = await GetResponseString(responseMessage).ConfigureAwait(false);
var responseString = await GetResponseStringAsync(responseMessage).ConfigureAwait(false);

// Get the response body object with the change list
// set on each response item.
var responseWithChangeList = await GetResponseBodyWithChangelist(responseString).ConfigureAwait(false);
var responseWithChangeList = await GetResponseBodyWithChangelistAsync(responseString).ConfigureAwait(false);
using var responseWithChangeListStream = new MemoryStream(Encoding.UTF8.GetBytes(responseWithChangeList));

var responseParseNode = await parseNodeFactory.GetRootParseNodeAsync(CoreConstants.MimeTypeNames.Application.Json, responseWithChangeListStream);
Expand All @@ -73,7 +73,7 @@ public async Task<ModelType> HandleResponseAsync<NativeResponseType, ModelType>(
/// </summary>
/// <param name="hrm">The response object</param>
/// <returns>The full response string to return</returns>
private async Task<string> GetResponseString(HttpResponseMessage hrm)
private async Task<string> GetResponseStringAsync(HttpResponseMessage hrm)
{
var responseContent = "";

Expand Down Expand Up @@ -106,7 +106,7 @@ private async Task<string> GetResponseString(HttpResponseMessage hrm)
/// </summary>
/// <param name="deltaResponseBody">The entire response body as a string.</param>
/// <returns>A task with a string representation of the response body. The changes are set on each response item.</returns>
private async Task<string> GetResponseBodyWithChangelist(string deltaResponseBody)
private async Task<string> GetResponseBodyWithChangelistAsync(string deltaResponseBody)
{
// This is the JsonDocument that we will replace. We should probably
// return a string instead.
Expand All @@ -123,7 +123,7 @@ private async Task<string> GetResponseBodyWithChangelist(string deltaResponseBod

foreach (var deltaObject in pageOfDeltaObjects.EnumerateArray())
{
var updatedObjectJsonDocument = await DiscoverChangedProperties(deltaObject).ConfigureAwait(false);
var updatedObjectJsonDocument = await DiscoverChangedPropertiesAsync(deltaObject).ConfigureAwait(false);
updatedObjectsWithChangeList.Add(updatedObjectJsonDocument.RootElement);
}

Expand All @@ -145,13 +145,13 @@ private async Task<string> GetResponseBodyWithChangelist(string deltaResponseBod
/// </summary>
/// <param name="responseItem">The item to inspect for properties.</param>
/// <returns>The item with the 'changes' property set on it.</returns>
private async Task<JsonDocument> DiscoverChangedProperties(JsonElement responseItem)
private async Task<JsonDocument> DiscoverChangedPropertiesAsync(JsonElement responseItem)
{
// List of changed properties.
var changes = new List<string>();

// Get the list of changed properties on the item.
await GetObjectProperties(responseItem, changes).ConfigureAwait(false);
await GetObjectPropertiesAsync(responseItem, changes).ConfigureAwait(false);

// Add the changes object to the response item.
#if NET5_0_OR_GREATER
Expand All @@ -170,7 +170,7 @@ private async Task<JsonDocument> DiscoverChangedProperties(JsonElement responseI
/// <param name="changes">The list of properties returned in the response.</param>
/// <param name="parentName">The parent object of this changed object.</param>
/// <returns></returns>
private async Task GetObjectProperties(JsonElement changedObject, List<string> changes, string parentName = "")
private async Task GetObjectPropertiesAsync(JsonElement changedObject, List<string> changes, string parentName = "")
{
if (!string.IsNullOrEmpty(parentName))
{
Expand All @@ -184,7 +184,7 @@ private async Task GetObjectProperties(JsonElement changedObject, List<string> c
case JsonValueKind.Object:
{
string parent = parentName + property.Name;
await GetObjectProperties(property.Value, changes, parent).ConfigureAwait(false);
await GetObjectPropertiesAsync(property.Value, changes, parent).ConfigureAwait(false);
break;
}
case JsonValueKind.Array:
Expand All @@ -207,7 +207,7 @@ private async Task GetObjectProperties(JsonElement changedObject, List<string> c

if (arrayItem.ValueKind == JsonValueKind.Object)
{
await GetObjectProperties(arrayItem, changes, parentWithIndex).ConfigureAwait(false);
await GetObjectPropertiesAsync(arrayItem, changes, parentWithIndex).ConfigureAwait(false);
}
else // Assuming that this is a Value item.
{
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Graph.Core/Requests/ResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<ModelType> HandleResponseAsync<NativeResponseType, ModelType>(
{
if (response is HttpResponseMessage responseMessage && responseMessage.Content != null && typeof(T).IsAssignableFrom(typeof(ModelType)))
{
await ValidateSuccessfulResponse(responseMessage, errorMappings).ConfigureAwait(false);
await ValidateSuccessfulResponseAsync(responseMessage, errorMappings).ConfigureAwait(false);
using var responseStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
var jsonParseNode = await _jsonParseNodeFactory.GetRootParseNodeAsync(responseMessage.Content.Headers?.ContentType?.MediaType?.ToLowerInvariant(), responseStream);
return (ModelType)(object)jsonParseNode.GetObjectValue<T>((parsable) => new T());
Expand All @@ -56,7 +56,7 @@ public async Task<ModelType> HandleResponseAsync<NativeResponseType, ModelType>(
/// </summary>
/// <param name="httpResponseMessage">The <see cref="HttpResponseMessage"/> to validate</param>
/// <param name="errorMapping">The errorMappings to use in the event of failed requests</param>
private async Task ValidateSuccessfulResponse(HttpResponseMessage httpResponseMessage, Dictionary<string, ParsableFactory<IParsable>> errorMapping)
private async Task ValidateSuccessfulResponseAsync(HttpResponseMessage httpResponseMessage, Dictionary<string, ParsableFactory<IParsable>> errorMapping)
{
if (httpResponseMessage.IsSuccessStatusCode)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public UploadResponseHandler(IParseNodeFactory parseNodeFactory = null)
/// <typeparam name="T">The type to return</typeparam>
/// <param name="response">The HttpResponseMessage to handle.</param>
/// <returns></returns>
public async Task<UploadResult<T>> HandleResponse<T>(HttpResponseMessage response) where T : IParsable,new()
public async Task<UploadResult<T>> HandleResponseAsync<T>(HttpResponseMessage response) where T : IParsable,new()
{
if (response.Content == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task<IUploadSession> GetAsync(CancellationToken cancellationToken =
var nativeResponseHandler = new NativeResponseHandler();
requestInformation.SetResponseHandler(nativeResponseHandler);
await this.requestAdapter.SendNoContentAsync(requestInformation, cancellationToken: cancellationToken).ConfigureAwait(false);
var uploadResult = await this.responseHandler.HandleResponse<UploadSession>(nativeResponseHandler.Value as HttpResponseMessage).ConfigureAwait(false);
var uploadResult = await this.responseHandler.HandleResponseAsync<UploadSession>(nativeResponseHandler.Value as HttpResponseMessage).ConfigureAwait(false);
return uploadResult.UploadSession;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Graph
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Serialization;
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -71,18 +72,28 @@ public UploadSliceRequestBuilder(
/// is true, then the item has completed, and the value is the created item from the server.</returns>
public async Task<UploadResult<T>> PutAsync(Stream stream, CancellationToken cancellationToken = default)
{
var requestInformation = this.CreatePutRequestInformationAsync(stream);
var requestInformation = this.CreatePutRequestInformation(stream);
var nativeResponseHandler = new NativeResponseHandler();
requestInformation.SetResponseHandler(nativeResponseHandler);
await this.RequestAdapter.SendNoContentAsync(requestInformation, cancellationToken: cancellationToken).ConfigureAwait(false);
return await this.ResponseHandler.HandleResponse<T>(nativeResponseHandler.Value as HttpResponseMessage).ConfigureAwait(false);
return await this.ResponseHandler.HandleResponseAsync<T>(nativeResponseHandler.Value as HttpResponseMessage).ConfigureAwait(false);
}

/// <summary>
/// Create <see cref="RequestInformation"/> instance to upload the file slice
/// <param name="stream">The <see cref="Stream"/> to upload</param>
/// </summary>
public RequestInformation CreatePutRequestInformationAsync(Stream stream)
[Obsolete("Use CreatePutRequestInformation instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
public RequestInformation CreatePutRequestInformationAsync(Stream stream) => CreatePutRequestInformation(stream);
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods

/// <summary>
/// Create <see cref="RequestInformation"/> instance to upload the file slice
/// <param name="stream">The <see cref="Stream"/> to upload</param>
/// </summary>
public RequestInformation CreatePutRequestInformation(Stream stream)
{
_ = stream ?? throw new ArgumentNullException(nameof(stream));
var requestInfo = new RequestInformation
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Graph.Core/Tasks/PageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static PageIterator<TEntity, TCollectionPage> CreatePageIterator(IRequest
/// <returns>A boolean value that indicates whether the callback cancelled
/// iterating across the page results or whether there are more pages to page.
/// A return value of false indicates that the iterator should stop iterating.</returns>
private async Task<bool> IntrapageIterate()
private async Task<bool> IntrapageIterateAsync()
{
State = PagingState.IntrapageIteration;

Expand Down Expand Up @@ -304,7 +304,7 @@ public async Task IterateAsync(CancellationToken token)
// Iterate over the contents of queue. The queue could be from the initial page
// results passed to the iterator, the results of a delta query, or from a
// previously cancelled iteration that gets resumed.
bool shouldContinueInterpageIteration = await IntrapageIterate();
bool shouldContinueInterpageIteration = await IntrapageIterateAsync();

// Request more pages if they are available.
while (shouldContinueInterpageIteration && !token.IsCancellationRequested)
Expand All @@ -314,7 +314,7 @@ public async Task IterateAsync(CancellationToken token)

// Iterate over items added to the queue by InterpageIterateAsync and
// determine whether there are more pages to request.
shouldContinueInterpageIteration = await IntrapageIterate();
shouldContinueInterpageIteration = await IntrapageIterateAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task GetDriveItemOnCompletedUpload(HttpStatusCode statusCode)
};

// Act
var uploadResult = await responseHandler.HandleResponse<TestDriveItem>(hrm);
var uploadResult = await responseHandler.HandleResponseAsync<TestDriveItem>(hrm);
var driveItem = uploadResult.ItemResponse;

//Assert
Expand All @@ -65,7 +65,7 @@ public async Task GetFileAttachmentLocationItemOnCompletedUpload()
hrm.StatusCode = HttpStatusCode.Created;//upload successful!

// Act
var uploadResult = await responseHandler.HandleResponse<TestDriveItem>(hrm);
var uploadResult = await responseHandler.HandleResponseAsync<TestDriveItem>(hrm);
var fileAttachment = uploadResult.ItemResponse;

//Assert
Expand All @@ -92,7 +92,7 @@ public async Task GetUploadSessionOnProgressingUpload()
};

// Act
var uploadResult = await responseHandler.HandleResponse<TestDriveItem>(hrm);
var uploadResult = await responseHandler.HandleResponseAsync<TestDriveItem>(hrm);
var uploadSession = uploadResult.UploadSession;

//Assert
Expand Down Expand Up @@ -126,7 +126,7 @@ public async Task ThrowsServiceExceptionOnErrorResponse()
};

// Act
var serviceException = await Assert.ThrowsAsync<ServiceException>(() => responseHandler.HandleResponse<TestDriveItem>(hrm));
var serviceException = await Assert.ThrowsAsync<ServiceException>(() => responseHandler.HandleResponseAsync<TestDriveItem>(hrm));

//Assert
Assert.NotNull(serviceException);
Expand Down Expand Up @@ -158,7 +158,7 @@ public async Task ThrowsSerializationErrorOnInvalidJson()
};

// Act
var serviceException = await Assert.ThrowsAsync<ServiceException>(() => responseHandler.HandleResponse<TestDriveItem>(hrm));
var serviceException = await Assert.ThrowsAsync<ServiceException>(() => responseHandler.HandleResponseAsync<TestDriveItem>(hrm));

//Assert
Assert.NotNull(serviceException);
Expand Down

0 comments on commit 0364e8c

Please sign in to comment.