All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.aws.smithy.kotlin.runtime.http.operation.HttpSerde.kt Maven / Gradle / Ivy

There is a newer version: 1.3.25
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
package aws.smithy.kotlin.runtime.http.operation

import aws.smithy.kotlin.runtime.InternalApi
import aws.smithy.kotlin.runtime.http.HttpCall
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
import aws.smithy.kotlin.runtime.http.response.HttpResponse
import aws.smithy.kotlin.runtime.operation.ExecutionContext

/**
 * Implemented by types that know how to serialize to the HTTP protocol.
 */
@InternalApi
public sealed interface HttpSerializer {

    /**
     * Serializer for streaming operations that need full control over serialization of the body
     */
    @InternalApi
    public interface Streaming : HttpSerializer {
        public suspend fun serialize(context: ExecutionContext, input: T): HttpRequestBuilder
    }

    /**
     * Serializer for non-streaming (simple) operations that don't need to ever suspend.
     */
    @InternalApi
    public interface NonStreaming : HttpSerializer {
        public fun serialize(context: ExecutionContext, input: T): HttpRequestBuilder
    }
}

/**
 * Implemented by types that know how to deserialize from the HTTP protocol.
 */
@InternalApi
public sealed interface HttpDeserializer {

    /**
     * Deserializer for streaming operations that need full control over deserialization of the body
     */
    @InternalApi
    public interface Streaming : HttpDeserializer {
        public suspend fun deserialize(context: ExecutionContext, call: HttpCall): T
    }

    /**
     * Deserializer for non-streaming (simple) operations that don't need to ever suspend. These
     * operations are handed the full payload if it exists.
     */
    @InternalApi
    public interface NonStreaming : HttpDeserializer {
        public fun deserialize(context: ExecutionContext, call: HttpCall, payload: ByteArray?): T
    }
}

/**
 * Implemented by types that know how to serialize to the HTTP protocol.
 */
@Deprecated("use HttpSerializer.Streaming")
@InternalApi
public fun interface HttpSerialize {
    public suspend fun serialize(context: ExecutionContext, input: T): HttpRequestBuilder
}

@Suppress("DEPRECATION")
private class LegacyHttpSerializeAdapter(val serializer: HttpSerialize) : HttpSerializer.Streaming {
    override suspend fun serialize(context: ExecutionContext, input: T): HttpRequestBuilder =
        serializer.serialize(context, input)
}

@Suppress("DEPRECATION")
internal fun  HttpSerialize.intoSerializer(): HttpSerializer = LegacyHttpSerializeAdapter(this)

/**
 * Implemented by types that know how to deserialize from the HTTP protocol.
 */
@Deprecated("use HttpDeserializer.Streaming")
@InternalApi
public fun interface HttpDeserialize {
    public suspend fun deserialize(context: ExecutionContext, call: HttpCall): T
}

@Suppress("DEPRECATION")
private class LegacyHttpDeserializeAdapter(val deserializer: HttpDeserialize) : HttpDeserializer.Streaming {
    override suspend fun deserialize(context: ExecutionContext, call: HttpCall): T =
        deserializer.deserialize(context, call)
}

@Suppress("DEPRECATION")
internal fun  HttpDeserialize.intoDeserializer(): HttpDeserializer = LegacyHttpDeserializeAdapter(this)

/**
 * Convenience deserialize implementation for a type with no output type
 */
@Suppress("DEPRECATION")
@InternalApi
public object UnitDeserializer : HttpDeserialize {
    override suspend fun deserialize(context: ExecutionContext, call: HttpCall) {}
}

/**
 * Convenience serialize implementation for a type with no input type
 */
@Suppress("DEPRECATION")
@InternalApi
public object UnitSerializer : HttpSerialize {
    override suspend fun serialize(context: ExecutionContext, input: Unit): HttpRequestBuilder = HttpRequestBuilder()
}

/**
 * Convenience deserialize implementation that returns the response without modification
 */
@Suppress("DEPRECATION")
@InternalApi
public object IdentityDeserializer : HttpDeserialize {
    override suspend fun deserialize(context: ExecutionContext, call: HttpCall): HttpResponse = call.response
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy