#!/usr/bin/env python3# -*- coding: utf-8 -*-## Copyright 2020 Alibaba Group Holding Limited. All Rights Reserved.## 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.#fromtypingimportGeneric,TypeVarfrompydanticimportFieldfromgs_interactive.api_responseimportApiResponsefromgs_interactive.client.statusimportStatusfromgs_interactive.exceptionsimportApiException# Define a generic type placeholderT=TypeVar("T")
[docs]classResult(Generic[T]):""" This is a generic class that wraps the result of an operation. It contains the status of the operation and the value returned by the operation. """
[docs]def__init__(self,status:Status,value:T):""" Construct a new Result object with the specified status and value. Args: status: the status of the operation. value: the value returned by the operation. """self.status=statusself.value=value
[docs]defis_ok(self):""" Whether the operation is successful. """returnself.status.is_ok()
[docs]defis_error(self):""" Whether the operation is failed. """returnself.status.is_error()
[docs]defget_value(self):""" Get the value returned by the operation. """returnself.value
[docs]defget_status(self):""" Get the status of the operation. """returnself.status
[docs]defget_status_message(self):""" Get the detail message of the status. """returnself.status.message
[docs]@staticmethoddefok(value):""" A static method to create a successful result. """returnResult(Status.ok(),value)
[docs]@staticmethoddeferror(status:Status,msg:str):""" A static method to create a failed result. """returnResult(status,msg)
[docs]@staticmethoddeffrom_exception(exception:ApiException):""" A static method create a Result object from an ApiException. """returnResult(Status.from_exception(exception),None)
[docs]@staticmethoddeffrom_response(response:ApiResponse):""" A static method create a Result object from an successful ApiResponse. """returnResult(Status.from_response(response),response.data)