Skip to content

Updated file to handle non json responses #1457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 40 additions & 37 deletions src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,45 +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.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)
Expand Down