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

build.buf.connect.ResponseMessage.kt Maven / Gradle / Ivy

There is a newer version: 0.1.10
Show newest version
// Copyright 2022-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package build.buf.connect

/**
 * Typed unary response from an RPC.
 */
sealed class ResponseMessage(
    // The status code of the response.
    open val code: Code,
    // Response headers specified by the server.
    open val headers: Headers,
    // Trailers provided by the server.
    open val trailers: Trailers,
) {
    class Success(
        // The message.
        val message: Output,
        // The status code of the response.
        override val code: Code,
        // Response headers specified by the server.
        override val headers: Headers,
        // Trailers provided by the server.
        override val trailers: Trailers,
    ) : ResponseMessage(code, headers, trailers)

    class Failure(
        // The error.
        val error: ConnectError,
        // The status code of the response.
        override val code: Code,
        // Response headers specified by the server.
        override val headers: Headers,
        // Trailers provided by the server.
        override val trailers: Trailers,
    ) : ResponseMessage(code, headers, trailers)

    fun  failure(function: (Failure) -> E?): E? {
        if (this is Failure) {
            return function(this)
        }
        return null
    }

    fun  success(function: (Success) -> E?): E? {
        if (this is Success) {
            return function(this)
        }
        return null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy