Skip to content

Commit

Permalink
Throw Exception with request_id
Browse files Browse the repository at this point in the history
  • Loading branch information
ide-rea committed Dec 28, 2023
1 parent cca1f30 commit 36ccaca
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 38 deletions.
9 changes: 4 additions & 5 deletions appbuilder/core/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self,
if not self.secret_key:
raise ValueError("secret_key is empty, please pass a nonempty secret_key "
"or set a secret_key in environment variable")
if not self.secret_key.startswith("Bearer"):
self.secret_key = "Bearer {}".format(self.secret_key)

if not gateway and not os.getenv("GATEWAY_URL"):
self.gateway = GATEWAY_URL
Expand Down Expand Up @@ -101,13 +103,10 @@ def check_response_json(data: dict):

def auth_header(self):
r"""auth_header is a helper method return auth info"""

if self.secret_key.startswith("Bearer "):
return {"X-Appbuilder-Authorization": self.secret_key}
else:
return {"X-Appbuilder-Authorization": "Bearer {}".format(self.secret_key)}
return {"X-Appbuilder-Authorization": self.secret_key}

@staticmethod
def response_request_id(response: requests.Response):
r"""response_request_id is a helper method get unique request id"""
return response.headers.get("X-Appbuilder-Request-Id", "")

10 changes: 7 additions & 3 deletions appbuilder/core/components/asr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ def _recognize(self, request: ShortSpeechRecognitionRequest, timeout: float = No
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id,data)
response = ShortSpeechRecognitionResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -110,4 +110,8 @@ def _check_service_error(data: dict):
"""
if "err_no" in data and "err_msg" in data:
if data["err_no"] != 0:
raise AppBuilderServerException(service_err_code=data["err_no"], service_err_message=data["err_msg"])
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data["err_no"],
service_err_message=data["err_msg"]
)
6 changes: 3 additions & 3 deletions appbuilder/core/components/dish_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def _recognize(self, request: DishRecognitionRequest, timeout: float = None,

url = self.http_client.service_url("/v1/bce/aip/image-classify/v2/dish")
response = self.http_client.session.post(url, headers=headers, data=request_data, timeout=timeout)

self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
request_id = self.http_client.response_request_id(response)
if "error_code" in data and "error_msg" in data:
raise AppBuilderServerException(service_err_code=data["error_code"], service_err_message=data["error_msg"])
return DishRecognitionResponse(data, request_id=self.http_client.response_request_id(response))
raise AppBuilderServerException(request_id=request_id, service_err_code=data["error_code"], service_err_message=data["error_msg"])
return DishRecognitionResponse(data)
11 changes: 7 additions & 4 deletions appbuilder/core/components/general_ocr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ def _recognize(self, request: GeneralOCRRequest, timeout: float = None,
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id, data)
ocr_response = GeneralOCRResponse.from_json(payload=json.dumps(data))
ocr_response.request_id = request_id
return ocr_response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
request (dict) : 通用文字识别body返回
返回:
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
14 changes: 9 additions & 5 deletions appbuilder/core/components/landmark_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ def __recognize(self, request: LandmarkRecognitionRequest, timeout: float = None
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.__check_service_error(data)
return LandmarkRecognitionResponse(data, request_id=self.http_client.response_request_id(response))
request_id = self.http_client.response_request_id(response)
self.__class__.__check_service_error(request_id, data)
return LandmarkRecognitionResponse(data, request_id=request_id)

@staticmethod
def __check_service_error(data: dict):
def __check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -99,5 +100,8 @@ def __check_service_error(data: dict):
"""

if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
11 changes: 7 additions & 4 deletions appbuilder/core/components/object_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ def _recognize(self, request: ObjectRecognitionRequest, timeout: float = None,
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__._check_service_error(data)
request_id = self.http_client.response_request_id(response)
self.__class__._check_service_error(request_id,data)
object_response = ObjectRecognitionResponse.from_json(payload=json.dumps(data))
object_response.request_id = request_id
return object_response

@staticmethod
def _check_service_error(data: dict):
def _check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
request (dict) : 通用物体与场景识别body返回
返回:
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
17 changes: 10 additions & 7 deletions appbuilder/core/components/text_to_image/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def submitText2ImageTask(self, request: Text2ImageSubmitRequest, timeout: float
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.check_service_error(data)
request_id = response.headers.get('X-Appbuilder-Request-Id')
request_id = self.http_client.response_request_id(response)
self.__class__.check_service_error(request_id, data)
response = Text2ImageSubmitResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response
Expand Down Expand Up @@ -135,8 +135,8 @@ def queryText2ImageData(self, request: Text2ImageQueryRequest, timeout: float =
self.http_client.check_response_header(response)
data = response.json()
self.http_client.check_response_json(data)
self.__class__.check_service_error(data)
request_id = response.headers.get('X-Appbuilder-Request-Id')
request_id = self.http_client.response_request_id(response)
self.__class__.check_service_error(request_id, data)
response = Text2ImageQueryResponse.from_json(payload=json.dumps(data))
response.request_id = request_id
return response
Expand All @@ -161,7 +161,7 @@ def extract_img_urls(self, response: Text2ImageQueryResponse):
return img_urls

@staticmethod
def check_service_error(data: dict):
def check_service_error(request_id: str, data: dict):
r"""个性化服务response参数检查
参数:
Expand All @@ -170,5 +170,8 @@ def check_service_error(data: dict):
"""
if "error_code" in data or "error_msg" in data:
raise AppBuilderServerException(service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg"))
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("error_code"),
service_err_message=data.get("error_msg")
)
3 changes: 2 additions & 1 deletion appbuilder/core/components/translate/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ def _translate(self, request: TranslateRequest, timeout: float = None,

self.http_client.check_response_header(response)
data = response.json()
request_id = self.http_client.response_request_id(response)
self.http_client.check_response_json(data)
if "error_code" in data and "error_msg" in data:
raise AppBuilderServerException(service_err_code=data["error_code"], service_err_message=data["error_msg"])
raise AppBuilderServerException(request_id=request_id, service_err_code=data["error_code"], service_err_message=data["error_msg"])

json_str = json.dumps(data)
return TranslateResponse(TranslateResponse.from_json(json_str))
8 changes: 5 additions & 3 deletions appbuilder/core/components/tts/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,19 @@ def __synthesis(self,
response = self.http_client.session.post(url, json=TTSRequest.to_dict(request), timeout=timeout, headers=auth_header)
self.http_client.check_response_header(response)
content_type = response.headers.get("Content-Type", "application/json")
request_id = self.http_client.response_request_id(response)
if content_type.find("application/json") != -1:
data = response.json()
self.http_client.check_response_json(data)
self.__class__.__check_service_error(data)
self.__class__.__check_service_error(request_id, data)
return TTSResponse(
binary=response.content,
request_id=self.http_client.response_request_id(response),
request_id=request_id,
aue=request.aue
)

@staticmethod
def __check_service_error(data: dict):
def __check_service_error(request_id: str, data: dict):
r"""个性化服务response检查
参数:
Expand All @@ -175,6 +176,7 @@ def __check_service_error(data: dict):

if "err_no" in data or "err_msg" in data or 'sn' in data or 'idx' in data:
raise AppBuilderServerException(
request_id=request_id,
service_err_code=data.get("err_no", 0),
service_err_message="{} . {} . {}]".
format(data.get("err_msg", ""),
Expand Down
4 changes: 2 additions & 2 deletions appbuilder/tests/test_asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def test_check_service_error(self):
"""
data = {'err_msg': 'Error', 'err_no': 1}
with self.assertRaises(appbuilder.AppBuilderServerException):
self.asr._check_service_error(data)
self.asr._check_service_error("",data)
data = {'err_msg': 'No Error', 'err_no': 0}
self.assertIsNone(self.asr._check_service_error(data))
self.assertIsNone(self.asr._check_service_error("", data))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion appbuilder/tests/test_text_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_check_service_error(self):
"""
data = {"error_code": "ERROR", "error_msg": "Error message"}
with self.assertRaises(appbuilder.AppBuilderServerException):
self.text2Image.check_service_error(data)
self.text2Image.check_service_error("", data)


if __name__ == '__main__':
Expand Down

0 comments on commit 36ccaca

Please sign in to comment.