#!/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.#fromgs_interactive.api_responseimportApiResponsefromgs_interactive.exceptionsimportApiExceptionfromgs_interactive.exceptionsimportBadRequestExceptionfromgs_interactive.exceptionsimportForbiddenExceptionfromgs_interactive.exceptionsimportNotFoundExceptionfromgs_interactive.exceptionsimportServiceExceptionfromurllib3.exceptionsimportMaxRetryErrorfromgs_interactive.client.generated.interactive_pb2importCodeasStatusCodefromgs_interactive.models.api_response_with_codeimportAPIResponseWithCode
[docs]classStatus:""" This class represents the status of an operation. It contains the status code and the message. """
[docs]def__init__(self,status:StatusCode,message:str):""" Construct a new Status object with the specified status code and message. Args: status (StatusCode): the returnd code of the operation. message (str): the message returned by the operation. """self.status=statusself.message=message
[docs]defis_ok(self)->bool:""" Whether the operation is successful. """returnself.status==StatusCode.OK
[docs]defis_error(self)->bool:""" Whether the operation is failed. """returnself.status!=StatusCode.OK
[docs]defget_code(self):""" Get the status code returned by the operation. """returnself.status
@propertydefget_message(self):""" Get the message returned by the operation. """returnself.message# static method create a server internal error object
[docs]@staticmethoddefserver_internal_error(message:str):""" Create a server internal error object with the specified message. """returnStatus(StatusCode.INTERNAL_ERROR,message)
[docs]@staticmethoddeffrom_exception(exception:ApiException):""" Create a Status object from an ApiException. """# mapping from ApiException to StatusCodeprint("exception: ",exception)ifisinstance(exception,BadRequestException):returnStatus(StatusCode.BAD_REQUEST,exception.body)elifisinstance(exception,ForbiddenException):returnStatus(StatusCode.PERMISSION_DENIED,exception.body)elifisinstance(exception,NotFoundException):returnStatus(StatusCode.NOT_FOUND,exception.body)elifisinstance(exception,ServiceException):ifexception.status==503:returnStatus(StatusCode.SERVICE_UNAVAILABLE,exception.body)else:returnStatus(StatusCode.INTERNAL_ERROR,exception.body)elifisinstance(exception,MaxRetryError):returnStatus(StatusCode.INTERNAL_ERROR,exception)returnStatus(StatusCode.UNKNOWN,"Unknown Error from exception "+exception.body)
[docs]@staticmethoddeffrom_response(response:ApiResponse):""" Create a Status object from an ApiResponse. """# mapping from ApiResponse to StatusCodeifresponse.status_code==200:returnStatus(StatusCode.OK,"OK")else:# If the status_code is not 200, we expect APIReponseWithCode returned from serverapi_response_with_code=response.dataifisinstance(api_response_with_code,APIResponseWithCode):returnStatus(api_response_with_code.code,api_response_with_code.message)returnStatus(StatusCode.UNKNOWN,"Unknown Error")
[docs]@staticmethoddefok():""" Create a successful status object. """returnStatus(StatusCode.OK,"OK")