#@ template language="C#" inherits="JsonRPCStructureMarshaller" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#
AddLicenseHeader();
AddCommonUsingStatements();
#>
using ThirdParty.Json.LitJson;
namespace <#=this.Config.Namespace #>.Model.Internal.MarshallTransformations
{
///
/// <#=this.Operation.Name #> Request Marshaller
///
public class <#=this.Operation.Name #>RequestMarshaller : IMarshallerRequest> , IMarshaller
{
///
/// Marshaller the request object to the HTTP request.
///
///
///
public IRequest Marshall(AmazonWebServiceRequest input)
{
return this.Marshall((<#=this.Operation.Name #>Request)input);
}
///
/// Marshaller the request object to the HTTP request.
///
///
///
public IRequest Marshall(<#=this.Operation.Name #>Request publicRequest)
{
IRequest request = new DefaultRequest(publicRequest, "<#=this.Config.Namespace #>");
<# if (!string.IsNullOrEmpty(this.Config.ServiceModel.TargetPrefix))
{
#>
string target = "<#=this.Config.ServiceModel.TargetPrefix #>.<#=this.Operation.Name #>";
request.Headers["X-Amz-Target"] = target;
<#
}
if (this.Operation.HttpMethod != "GET" && this.Operation.HttpMethod != "DELETE")
{
if (this.Config.ServiceModel.Customizations.OverrideContentType != null)
{
#>
request.Headers["Content-Type"] = "<#=this.Config.ServiceModel.Customizations.OverrideContentType #>";
<#
}
else if (this.Config.ServiceModel.Type != ServiceType.Rest_Json)
{
#>
request.Headers["Content-Type"] = "application/x-amz-json-<#=this.Config.ServiceModel.JsonVersion #>";
<#
}
else
{
#>
request.Headers["Content-Type"] = "application/json";
<#
}
}
#>
request.Headers[Amazon.Util.HeaderKeys.XAmzApiVersion] = "<#=this.Config.ServiceModel.APIVersion #>";
request.HttpMethod = "<#=this.Operation.HttpMethod #>";
<#
var requestStructure = this.Operation.RequestStructure;
// Generates code to add members of the request to the request being created by the marshaller
ProcessRequestUri(this.Operation);
if (this.Config.ServiceModel.Type == ServiceType.Rest_Json)
{
ProcessUriMembers("publicRequest", this.Operation);
ProcessQueryStringMembers("publicRequest", this.Operation);
}
#>
request.ResourcePath = "<#=this.Operation.RequestUri #>";
<#
var payload = this.Operation.RequestPayloadMember;
var shouldMarshallPayload = (payload != null && !payload.IsMemoryStream && !payload.Shape.IsString);
// Process any members which are marshalled as part of the request body
if (this.Operation.RequestHasBodyMembers || shouldMarshallPayload)
{
#>
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
JsonWriter writer = new JsonWriter(stringWriter);
<#
if (shouldMarshallPayload)
{
#>
var context = new JsonMarshallerContext(request, writer);
<#
ProcessStructure(0, "publicRequest." + payload.PropertyName, payload.Shape);
}
else
{
#>
writer.WriteObjectStart();
var context = new JsonMarshallerContext(request, writer);
<#
ProcessMembers(1, "publicRequest", this.Operation.RequestBodyMembers);
#>
writer.WriteObjectEnd();
<#
}
#>
string snippet = stringWriter.ToString();
request.Content = System.Text.Encoding.UTF8.GetBytes(snippet);
<#
GenerateRequestChecksumHandling(this.Operation, "snippet");
#>
}
<#
}
else if (payload?.Shape.IsString == true)
{
#>
request.Content = System.Text.Encoding.UTF8.GetBytes(publicRequest.<#= payload.PropertyName #>);
<#
}
else if (payload?.IsMemoryStream == true)
{
#>
request.ContentStream = publicRequest.<#=payload.PropertyName#> ?? new MemoryStream();
<#
var requiresLength = payload.RequiresLength;
if (!requiresLength && payload.IsStreaming && this.Operation.AuthType == OperationAuthType.V4UnsignedBody)
{
#>
if (request.ContentStream.CanSeek)
{
request.Headers[Amazon.Util.HeaderKeys.ContentLengthHeader] =
request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
}
else
{
request.Headers[Amazon.Util.HeaderKeys.TransferEncodingHeader] = "chunked";
}
<#
}
else
{
if (payload.IsStreaming && requiresLength)
{
#>
if (!request.ContentStream.CanSeek)
{
throw new System.InvalidOperationException("Cannot determine stream length for the payload when content-length is required.");
}
<#
}
#>
request.Headers[Amazon.Util.HeaderKeys.ContentLengthHeader] =
request.ContentStream.Length.ToString(CultureInfo.InvariantCulture);
<#
}
#>
request.Headers[Amazon.Util.HeaderKeys.ContentTypeHeader] = "binary/octet-stream";
if (request.ContentStream != null && request.ContentStream.Length == 0)
{
request.Headers.Remove(Amazon.Util.HeaderKeys.ContentTypeHeader);
}
<#
}
else if (payload?.Shape.IsPrimitiveType == true)
{
// 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.");
}
else if (this.Config.ServiceModel.Type == ServiceType.Json)
{
#>
var content = "{}";
request.Content = System.Text.Encoding.UTF8.GetBytes(content);
<#
GenerateRequestChecksumHandling(this.Operation, "content");
#>
<#
}
if (this.Config.ServiceModel.Type == ServiceType.Rest_Json)
{
ProcessHeaderMembers("publicRequest", this.Operation.RequestHeaderMembers);
}
// If there aren't any members that are marshalled as part of the body or streamed
if (this.Operation.UseQueryString)
{
#>
request.UseQueryString = true;
<# }
// We skip endpoint host prefix handling for S3 and S3 Control as it's implemented by endpoint rules.
if (!string.IsNullOrEmpty(this.Operation.EndpointHostPrefix) && this.Config.ServiceId != "S3" && this.Config.ServiceId != "S3 Control")
{
ProcessEndpointHostPrefixMembers("publicRequest", this.Operation);
}
#>
return request;
}
<#
this.AddRequestSingletonMethod();
#>
}
}