#@ template language="C#" inherits="BaseResponseUnmarshaller" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#
AddLicenseHeader();
AddCommonUsingStatements();
#>
namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
{
///
/// Response Unmarshaller for <#=this.UnmarshallerBaseName #> operation
///
public class <#=this.UnmarshallerBaseName #>ResponseUnmarshaller : XmlResponseUnmarshaller
{
///
/// Unmarshaller the response from the service to the response class.
///
///
///
public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
{
<#=this.UnmarshallerBaseName #>Response response = new <#=this.Operation.Name #>Response();
<#
var payload= this.Operation.ResponsePayloadMember;
var shouldMarshallPayload = (payload != null && payload.IsStructure);
var payloadIsStream = (payload != null && !shouldMarshallPayload);
if (this.Operation.ResponseHasBodyMembers || shouldMarshallPayload)
{
if (this.Operation.AllowEmptyResult)
{
#>
if (context.ResponseData.IsSuccessStatusCode && context.ResponseData.ContentLength == 0)
return response;
<#
}
foreach (var marshallName in this.Structure.FindMarshallNamesWithoutMembers())
{
#>
context.AllowEmptyElementLookup.Add("<#=marshallName#>");
<#
}
#>
UnmarshallResult(context,response);
<#
}
else if (payloadIsStream)
{
if (payload.IsStreaming)
{
#>
response.<#=payload.PropertyName#> = context.Stream;
<#
}
else if (payload.ModelShape.IsString)
{
#>
using (var sr = new StreamReader(context.Stream))
{
response.<#=payload.PropertyName#> = sr.ReadToEnd();
}
<#
}
else if (payload.ModelShape.IsMemoryStream)
{
#>
var ms = new MemoryStream();
Amazon.Util.AWSSDKUtils.CopyStream(context.Stream, ms);
ms.Seek(0, SeekOrigin.Begin);
response.<#=payload.PropertyName#> = ms;
<#
}
else
{
// At this point, all valid configurations have been handled. Valid use of payload is defined:
// https://awslabs.github.io/smithy/1.0/spec/core/http-traits.html#httppayload-trait
throw new Exception(
$"{payload.PropertyName} can not be a Payload as Type {payload.Shape.Type} is not a valid target for the httpPayload trait. " +
"The httpPayload trait can be applied to structure members that target a string, blob, structure, union, document, set, map, or list.");
}
}
UnmarshallHeaders();
ProcessStatusCode();
#>
return response;
}
<#
if ( this.Operation.ResponseHasBodyMembers || shouldMarshallPayload)
{
#>
<#
if (this.Operation.ResponseBodyMembers.Count == 0 && !shouldMarshallPayload)
{
#>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId="response")]
<#
}
#>
private static void UnmarshallResult(XmlUnmarshallerContext context, <#=this.Operation.Name #>Response response)
{
int originalDepth = context.CurrentDepth;
int targetDepth = originalDepth + 1;
<#
if (payload == null)
{
#>
if (context.IsStartOfDocument)
targetDepth += 1;
<#
}
#>
while (context.Read())
{
if (context.IsStartElement || context.IsAttribute)
{
<#
foreach (var member in this.Operation.ResponseBodyMembers)
{
if (member.Shape.IsList)
{
var listMarshallName = member.Shape.ListMarshallName ?? "member";
if (member.Shape.IsFlattened)
{
#>
if (context.TestExpression("<#=listMarshallName#>", targetDepth))
{
var unmarshaller = <#= member.DetermineTypeUnmarshallerInstantiate() #>;
response.<#=member.PropertyName#>.Add(unmarshaller.Unmarshall(context));
continue;
}
<#
}
else
{
#>
if (context.TestExpression("<#=member.MarshallName#>/<#=listMarshallName#>", targetDepth))
{
var unmarshaller = <#= member.DetermineTypeUnmarshallerInstantiate() #>;
response.<#=member.PropertyName#>.Add(unmarshaller.Unmarshall(context));
continue;
}
<#
}
}
else
{
#>
if (context.TestExpression("<#=member.MarshallName#>", targetDepth))
{
var unmarshaller = <#= member.DetermineTypeUnmarshallerInstantiate() #>;
response.<#=member.PropertyName#> = unmarshaller.Unmarshall(context);
continue;
}
<#
}
}
if (shouldMarshallPayload)
{
#>
if (context.TestExpression("<#=payload.MarshallName#>", targetDepth))
{
var unmarshaller = <#= payload.DetermineTypeUnmarshallerInstantiate() #>;
response.<#=payload.PropertyName#> = unmarshaller.Unmarshall(context);
continue;
}
<#
}
#>
}
else if (context.IsEndElement && context.CurrentDepth < originalDepth)
{
return;
}
}
return;
}
<#
}
#>
///
/// Unmarshaller error response to exception.
///
///
///
///
///
public override AmazonServiceException UnmarshallException(XmlUnmarshallerContext context, Exception innerException, HttpStatusCode statusCode)
{
ErrorResponse errorResponse = ErrorResponseUnmarshaller.GetInstance().Unmarshall(context);
errorResponse.InnerException = innerException;
errorResponse.StatusCode = statusCode;
var responseBodyBytes = context.GetResponseBodyBytes();
using (var streamCopy = new MemoryStream(responseBodyBytes))
using (var contextCopy = new XmlUnmarshallerContext(streamCopy, false, null))
{
<#
foreach (var exception in this.Operation.Exceptions)
{
#>
if (errorResponse.Code != null && errorResponse.Code.Equals("<#=exception.Code #>"))
{
return <#=exception.Name#>Unmarshaller.Instance.Unmarshall(contextCopy, errorResponse);
}
<#
}
#>
}
return new <#=this.BaseException#>(errorResponse.Message, innerException, errorResponse.Type, errorResponse.Code, errorResponse.RequestId, statusCode);
}
<#
this.AddResponseSingletonMethod();
#>
}
}