From f2f1edf0b5f3065f60af3707346201a18f54e093 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Sat, 13 Aug 2022 10:54:00 +0300 Subject: [PATCH 1/3] Updated file to handle non json responses --- .../Authentication/Cmdlets/InvokeMgGraphRequest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index b0332a028f7..01a9e3ea211 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -469,6 +469,11 @@ internal async Task ProcessResponseAsync(HttpResponseMessage response) ErrorConstants.Codes.InvokeGraphContentTypeException, returnType); ThrowIfError(errorRecord); } + else if (returnType == RestReturnType.Detect) + { + var responseString = await response.Content.ReadAsStringAsync(); + WriteObject(responseString); + } else if (returnType == RestReturnType.OctetStream) { var errorRecord = @@ -594,6 +599,12 @@ private async Task GetResponseAsync(HttpClient client, Http var cancellationToken = _cancellationTokenSource.Token; var response = await client.SendAsync(request, cancellationToken); + var responseType = response.GetType(); + var contentType = response.GetContentType(); + if (responseType != typeof(HttpResponseMessage) || (contentType.Equals("text/plain") && String.IsNullOrEmpty(OutputFilePath) && !OutputType.Equals(OutputType.HttpResponseMessage))) + { + throw new ArgumentException("missing -OutputFilePath parameter"); + } return response; } } From 6aa856e2ed214402fd8294a383835b1e85ba042e Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 17 Aug 2022 14:53:03 +0300 Subject: [PATCH 2/3] Update src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs Co-authored-by: Peter Ombwa --- .../Authentication/Cmdlets/InvokeMgGraphRequest.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index 01a9e3ea211..e36545b49da 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -599,9 +599,8 @@ private async Task GetResponseAsync(HttpClient client, Http var cancellationToken = _cancellationTokenSource.Token; var response = await client.SendAsync(request, cancellationToken); - var responseType = response.GetType(); var contentType = response.GetContentType(); - if (responseType != typeof(HttpResponseMessage) || (contentType.Equals("text/plain") && String.IsNullOrEmpty(OutputFilePath) && !OutputType.Equals(OutputType.HttpResponseMessage))) + if ((contentType.Equals("text/plain") && String.IsNullOrEmpty(OutputFilePath) && !OutputType.Equals(OutputType.HttpResponseMessage))) { throw new ArgumentException("missing -OutputFilePath parameter"); } From 9fa0e4b0daf822ab374fc93d0872a64e051ce1d7 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa <10947120+timayabi2020@users.noreply.github.com> Date: Thu, 18 Aug 2022 16:42:27 +0300 Subject: [PATCH 3/3] Refactored initial solution --- .../Cmdlets/InvokeMgGraphRequest.cs | 87 +++++++++---------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index e36545b49da..01aeef031b3 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -436,50 +436,48 @@ internal async Task ProcessResponseAsync(HttpResponseMessage response) if (ShouldWriteToPipeline) { var returnType = response.CheckReturnType(); - if (returnType == RestReturnType.Json) + switch (returnType) { - var responseString = await response.Content.ReadAsStringAsync(); - ErrorRecord error; - switch (OutputType) - { - case OutputType.HashTable: - var hashTable = responseString.ConvertFromJson(true, null, out error); - ThrowIfError(error); - WriteObject(hashTable); - break; - case OutputType.PSObject: - var psObject = responseString.ConvertFromJson(false, null, out error); - ThrowIfError(error); - WriteObject(psObject, true); - break; - case OutputType.HttpResponseMessage: + case RestReturnType.Json: + ErrorRecord error; + string responseString; + switch (OutputType) + { + case OutputType.HashTable: + responseString = await response.Content.ReadAsStringAsync(); + var hashTable = responseString.ConvertFromJson(true, null, out error); + ThrowIfError(error); + WriteObject(hashTable); + break; + case OutputType.PSObject: + responseString = await response.Content.ReadAsStringAsync(); + var psObject = responseString.ConvertFromJson(false, null, out error); + ThrowIfError(error); + WriteObject(psObject, true); + break; + case OutputType.HttpResponseMessage: + WriteObject(response); + break; + case OutputType.Json: + responseString = await response.Content.ReadAsStringAsync(); + WriteObject(responseString); + break; + default: + throw new ArgumentOutOfRangeException(nameof(OutputType)); + } + break; + case RestReturnType.OctetStream: + if (OutputType == OutputType.HttpResponseMessage) WriteObject(response); - break; - case OutputType.Json: - WriteObject(responseString); - break; - default: - throw new ArgumentOutOfRangeException(); - } - } - else if (returnType == RestReturnType.Image) - { - var errorRecord = - GetValidationError(Resources.NonJsonResponseWithoutOutputFilePath, - ErrorConstants.Codes.InvokeGraphContentTypeException, returnType); - ThrowIfError(errorRecord); - } - else if (returnType == RestReturnType.Detect) - { - var responseString = await response.Content.ReadAsStringAsync(); - WriteObject(responseString); - } - else if (returnType == RestReturnType.OctetStream) - { - var errorRecord = - GetValidationError(Resources.NonJsonResponseWithoutInfer, - ErrorConstants.Codes.InvokeGraphContentTypeException, returnType, response.Content.Headers.ContentDisposition); - ThrowIfError(errorRecord); + else + ThrowIfError(GetValidationError(Resources.NonJsonResponseWithoutInfer, ErrorConstants.Codes.InvokeGraphContentTypeException, returnType, response.Content.Headers.ContentDisposition)); + break; + default: + if (OutputType == OutputType.HttpResponseMessage) + WriteObject(response); + else + ThrowIfError(GetValidationError(Resources.NonJsonResponseWithoutOutputFilePath, ErrorConstants.Codes.InvokeGraphContentTypeException, returnType)); + break; } } if (ShouldSaveToOutFile) @@ -599,11 +597,6 @@ private async Task GetResponseAsync(HttpClient client, Http var cancellationToken = _cancellationTokenSource.Token; var response = await client.SendAsync(request, cancellationToken); - var contentType = response.GetContentType(); - if ((contentType.Equals("text/plain") && String.IsNullOrEmpty(OutputFilePath) && !OutputType.Equals(OutputType.HttpResponseMessage))) - { - throw new ArgumentException("missing -OutputFilePath parameter"); - } return response; } }