import { HttpRequest as __HttpRequest , HttpResponse as __HttpResponse } from "@aws-sdk/protocol-http" ; import { expectInt32 as __expectInt32 , expectNonNull as __expectNonNull , expectObject as __expectObject , expectString as __expectString , extendedEncodeURIComponent as __extendedEncodeURIComponent , } from "@aws-sdk/smithy-client" ; import { Endpoint as __Endpoint , MetadataBearer as __MetadataBearer , ResponseMetadata as __ResponseMetadata , SerdeContext as __SerdeContext , SmithyException as __SmithyException , } from "@aws-sdk/types" ; import { InvokeEndpointAsyncCommandInput , InvokeEndpointAsyncCommandOutput , } from "../commands/InvokeEndpointAsyncCommand" ; import { InvokeEndpointCommandInput , InvokeEndpointCommandOutput } from "../commands/InvokeEndpointCommand" ; import { InternalDependencyException , InternalFailure , ModelError , ModelNotReadyException , ServiceUnavailable , ValidationError , } from "../models/models_0" ; export const serializeAws_restJson1InvokeEndpointCommand = async ( input , context ) : Promise < __HttpRequest > => { const { hostname , protocol = "https" , port , path : basePath } = await context . endpoint ( ) ; const headers = { "content-type" : "application/octet-stream" , ... ( isSerializableHeaderValue ( input . ContentType ) && { "content-type" : input . ContentType ! } ) , ... ( isSerializableHeaderValue ( input . Accept ) && { accept : input . Accept ! } ) , ... ( isSerializableHeaderValue ( input . CustomAttributes ) && { "x-amzn-sagemaker-custom-attributes" : input . CustomAttributes ! , } ) , ... ( isSerializableHeaderValue ( input . TargetModel ) && { "x-amzn-sagemaker-target-model" : input . TargetModel ! } ) , ... ( isSerializableHeaderValue ( input . TargetVariant ) && { "x-amzn-sagemaker-target-variant" : input . TargetVariant ! } ) , ... ( isSerializableHeaderValue ( input . TargetContainerHostname ) && { "x-amzn-sagemaker-target-container-hostname" : input . TargetContainerHostname ! , } ) , ... ( isSerializableHeaderValue ( input . InferenceId ) && { "x-amzn-sagemaker-inference-id" : input . InferenceId ! } ) , } ; let resolvedPath = `${ basePath ?. endsWith ( "/" ) ? basePath . slice ( 0 , - 1 ) : basePath || "" }` + "/endpoints/{EndpointName}/invocations" ; if ( input . EndpointName !== undefined ) { const labelValue = input . EndpointName ; if ( labelValue . length <= 0 ) { throw new Error ( "Empty value provided for input HTTP label: EndpointName." ) ; } resolvedPath = resolvedPath . replace ( "{EndpointName}" , __extendedEncodeURIComponent ( labelValue ) ) ; } else { throw new Error ( "No value provided for input HTTP label: EndpointName." ) ; } let body ; if ( input . Body !== undefined ) { body = input . Body ; } return new __HttpRequest ( { protocol , hostname , port , method : "POST" , headers , path : resolvedPath , body , } ) ; } ; export const serializeAws_restJson1InvokeEndpointAsyncCommand = async ( input , context ) : Promise < __HttpRequest > => { const { hostname , protocol = "https" , port , path : basePath } = await context . endpoint ( ) ; const headers = { ... ( isSerializableHeaderValue ( input . ContentType ) && { "x-amzn-sagemaker-content-type" : input . ContentType ! } ) , ... ( isSerializableHeaderValue ( input . Accept ) && { "x-amzn-sagemaker-accept" : input . Accept ! } ) , ... ( isSerializableHeaderValue ( input . CustomAttributes ) && { "x-amzn-sagemaker-custom-attributes" : input . CustomAttributes ! , } ) , ... ( isSerializableHeaderValue ( input . InferenceId ) && { "x-amzn-sagemaker-inference-id" : input . InferenceId ! } ) , ... ( isSerializableHeaderValue ( input . InputLocation ) && { "x-amzn-sagemaker-inputlocation" : input . InputLocation ! } ) , ... ( isSerializableHeaderValue ( input . RequestTTLSeconds ) && { "x-amzn-sagemaker-requestttlseconds" : input . RequestTTLSeconds ! . toString ( ) , } ) , } ; let resolvedPath = `${ basePath ?. endsWith ( "/" ) ? basePath . slice ( 0 , - 1 ) : basePath || "" }` + "/endpoints/{EndpointName}/async-invocations" ; if ( input . EndpointName !== undefined ) { const labelValue = input . EndpointName ; if ( labelValue . length <= 0 ) { throw new Error ( "Empty value provided for input HTTP label: EndpointName." ) ; } resolvedPath = resolvedPath . replace ( "{EndpointName}" , __extendedEncodeURIComponent ( labelValue ) ) ; } else { throw new Error ( "No value provided for input HTTP label: EndpointName." ) ; } let body ; return new __HttpRequest ( { protocol , hostname , port , method : "POST" , headers , path : resolvedPath , body , } ) ; } ; export const deserializeAws_restJson1InvokeEndpointCommand = async ( output , context ) : Promise < InvokeEndpointCommandOutput > => { if ( output . statusCode !== 200 && output . statusCode >= 300 ) { return deserializeAws_restJson1InvokeEndpointCommandError ( output , context ) ; } const contents = { $metadata : deserializeMetadata ( output ) , Body : undefined , ContentType : undefined , CustomAttributes : undefined , InvokedProductionVariant : undefined , } ; if ( output . headers [ "content-type" ] !== undefined ) { contents . ContentType = output . headers [ "content-type" ] ; } if ( output . headers [ "x-amzn-invoked-production-variant" ] !== undefined ) { contents . InvokedProductionVariant = output . headers [ "x-amzn-invoked-production-variant" ] ; } if ( output . headers [ "x-amzn-sagemaker-custom-attributes" ] !== undefined ) { contents . CustomAttributes = output . headers [ "x-amzn-sagemaker-custom-attributes" ] ; } const data = await collectBody ( output . body , context ) ; contents . Body = data ; return Promise . resolve ( contents ) ; } ; const deserializeAws_restJson1InvokeEndpointCommandError = async ( output , context ) : Promise < InvokeEndpointCommandOutput > => { const parsedOutput = { ... output , body : await parseBody ( output . body , context ) , } ; let response ; let errorCode = "UnknownError" ; errorCode = loadRestJsonErrorCode ( output , parsedOutput . body ) ; switch ( errorCode ) { case "InternalDependencyException" : case "com.amazonaws.sagemakerruntime#InternalDependencyException" : response = { ... ( await deserializeAws_restJson1InternalDependencyExceptionResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "InternalFailure" : case "com.amazonaws.sagemakerruntime#InternalFailure" : response = { ... ( await deserializeAws_restJson1InternalFailureResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ModelError" : case "com.amazonaws.sagemakerruntime#ModelError" : response = { ... ( await deserializeAws_restJson1ModelErrorResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ModelNotReadyException" : case "com.amazonaws.sagemakerruntime#ModelNotReadyException" : response = { ... ( await deserializeAws_restJson1ModelNotReadyExceptionResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ServiceUnavailable" : case "com.amazonaws.sagemakerruntime#ServiceUnavailable" : response = { ... ( await deserializeAws_restJson1ServiceUnavailableResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ValidationError" : case "com.amazonaws.sagemakerruntime#ValidationError" : response = { ... ( await deserializeAws_restJson1ValidationErrorResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; default : const parsedBody = parsedOutput . body ; errorCode = parsedBody . code || parsedBody . Code || errorCode ; response = { ... parsedBody , name : `${ errorCode }` , message : parsedBody . message || parsedBody . Message || errorCode , $fault : "client" , $metadata : deserializeMetadata ( output ) , } as any ; } const message = response . message || response . Message || errorCode ; response . message = message ; delete response . Message ; return Promise . reject ( Object . assign ( new Error ( message ) , response ) ) ; } ; export const deserializeAws_restJson1InvokeEndpointAsyncCommand = async ( output , context ) : Promise < InvokeEndpointAsyncCommandOutput > => { if ( output . statusCode !== 202 && output . statusCode >= 300 ) { return deserializeAws_restJson1InvokeEndpointAsyncCommandError ( output , context ) ; } const contents = { $metadata : deserializeMetadata ( output ) , InferenceId : undefined , OutputLocation : undefined , } ; if ( output . headers [ "x-amzn-sagemaker-outputlocation" ] !== undefined ) { contents . OutputLocation = output . headers [ "x-amzn-sagemaker-outputlocation" ] ; } const data = __expectNonNull ( __expectObject ( await parseBody ( output . body , context ) ) , "body" ) ; if ( data . InferenceId !== undefined && data . InferenceId !== null ) { contents . InferenceId = __expectString ( data . InferenceId ) ; } return Promise . resolve ( contents ) ; } ; const deserializeAws_restJson1InvokeEndpointAsyncCommandError = async ( output , context ) : Promise < InvokeEndpointAsyncCommandOutput > => { const parsedOutput = { ... output , body : await parseBody ( output . body , context ) , } ; let response ; let errorCode = "UnknownError" ; errorCode = loadRestJsonErrorCode ( output , parsedOutput . body ) ; switch ( errorCode ) { case "InternalFailure" : case "com.amazonaws.sagemakerruntime#InternalFailure" : response = { ... ( await deserializeAws_restJson1InternalFailureResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ServiceUnavailable" : case "com.amazonaws.sagemakerruntime#ServiceUnavailable" : response = { ... ( await deserializeAws_restJson1ServiceUnavailableResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; case "ValidationError" : case "com.amazonaws.sagemakerruntime#ValidationError" : response = { ... ( await deserializeAws_restJson1ValidationErrorResponse ( parsedOutput , context ) ) , name : errorCode , $metadata : deserializeMetadata ( output ) , } ; break ; default : const parsedBody = parsedOutput . body ; errorCode = parsedBody . code || parsedBody . Code || errorCode ; response = { ... parsedBody , name : `${ errorCode }` , message : parsedBody . message || parsedBody . Message || errorCode , $fault : "client" , $metadata : deserializeMetadata ( output ) , } as any ; } const message = response . message || response . Message || errorCode ; response . message = message ; delete response . Message ; return Promise . reject ( Object . assign ( new Error ( message ) , response ) ) ; } ; const deserializeAws_restJson1InternalDependencyExceptionResponse = async ( parsedOutput , context ) : Promise < InternalDependencyException > => { const contents = { name : "InternalDependencyException" , $fault : "server" , $metadata : deserializeMetadata ( parsedOutput ) , Message : undefined , } ; const data = parsedOutput . body ; if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } return contents ; } ; const deserializeAws_restJson1InternalFailureResponse = async ( parsedOutput , context ) : Promise < InternalFailure > => { const contents = { name : "InternalFailure" , $fault : "server" , $metadata : deserializeMetadata ( parsedOutput ) , Message : undefined , } ; const data = parsedOutput . body ; if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } return contents ; } ; const deserializeAws_restJson1ModelErrorResponse = async ( parsedOutput , context ) : Promise < ModelError > => { const contents = { name : "ModelError" , $fault : "client" , $metadata : deserializeMetadata ( parsedOutput ) , LogStreamArn : undefined , Message : undefined , OriginalMessage : undefined , OriginalStatusCode : undefined , } ; const data = parsedOutput . body ; if ( data . LogStreamArn !== undefined && data . LogStreamArn !== null ) { contents . LogStreamArn = __expectString ( data . LogStreamArn ) ; } if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } if ( data . OriginalMessage !== undefined && data . OriginalMessage !== null ) { contents . OriginalMessage = __expectString ( data . OriginalMessage ) ; } if ( data . OriginalStatusCode !== undefined && data . OriginalStatusCode !== null ) { contents . OriginalStatusCode = __expectInt32 ( data . OriginalStatusCode ) ; } return contents ; } ; const deserializeAws_restJson1ModelNotReadyExceptionResponse = async ( parsedOutput , context ) : Promise < ModelNotReadyException > => { const contents = { name : "ModelNotReadyException" , $fault : "client" , $metadata : deserializeMetadata ( parsedOutput ) , Message : undefined , } ; const data = parsedOutput . body ; if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } return contents ; } ; const deserializeAws_restJson1ServiceUnavailableResponse = async ( parsedOutput , context ) : Promise < ServiceUnavailable > => { const contents = { name : "ServiceUnavailable" , $fault : "server" , $metadata : deserializeMetadata ( parsedOutput ) , Message : undefined , } ; const data = parsedOutput . body ; if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } return contents ; } ; const deserializeAws_restJson1ValidationErrorResponse = async ( parsedOutput , context ) : Promise < ValidationError > => { const contents = { name : "ValidationError" , $fault : "client" , $metadata : deserializeMetadata ( parsedOutput ) , Message : undefined , } ; const data = parsedOutput . body ; if ( data . Message !== undefined && data . Message !== null ) { contents . Message = __expectString ( data . Message ) ; } return contents ; } ; const deserializeMetadata = ( output ) : __ResponseMetadata => ( { httpStatusCode : output . statusCode , requestId : output . headers [ "x-amzn-requestid" ] ?? output . headers [ "x-amzn-request-id" ] , extendedRequestId : output . headers [ "x-amz-id-2" ] , cfId : output . headers [ "x-amz-cf-id" ] , } ) ; const collectBody = ( streamBody = new Uint8Array ( ) , context ) : Promise < Uint8Array > => { if ( streamBody instanceof Uint8Array ) { return Promise . resolve ( streamBody ) ; } return context . streamCollector ( streamBody ) || Promise . resolve ( new Uint8Array ( ) ) ; } ; const collectBodyString = ( streamBody , context ) : Promise < string > => collectBody ( streamBody , context ) . then ( ( body ) => context . utf8Encoder ( body ) ) ; const isSerializableHeaderValue = ( value ) : boolean => value !== undefined && value !== null && value !== "" && ( ! Object . getOwnPropertyNames ( value ) . includes ( "length" ) || value . length != 0 ) && ( ! Object . getOwnPropertyNames ( value ) . includes ( "size" ) || value . size != 0 ) ; const parseBody = ( streamBody , context ) : any => collectBodyString ( streamBody , context ) . then ( ( encoded ) => { if ( encoded . length ) { return JSON . parse ( encoded ) ; } return { } ; } ) ; /**
 * Load an error code for the aws.rest-json-1.1 protocol.
 */ const loadRestJsonErrorCode = ( output , data ) : string => { const findKey = ( object , key ) => Object . keys ( object ) . find ( ( k ) => k . toLowerCase ( ) === key . toLowerCase ( ) ) ; const sanitizeErrorCode = ( rawValue ) : string => { let cleanValue = rawValue ; if ( cleanValue . indexOf ( ":" ) >= 0 ) { cleanValue = cleanValue . split ( ":" ) [ 0 ] ; } if ( cleanValue . indexOf ( "#" ) >= 0 ) { cleanValue = cleanValue . split ( "#" ) [ 1 ] ; } return cleanValue ; } ; const headerKey = findKey ( output . headers , "x-amzn-errortype" ) ; if ( headerKey !== undefined ) { return sanitizeErrorCode ( output . headers [ headerKey ] ) ; } if ( data . code !== undefined ) { return sanitizeErrorCode ( data . code ) ; } if ( data [ "__type" ] !== undefined ) { return sanitizeErrorCode ( data [ "__type" ] ) ; } return "" ; } ;