diff --git a/appbuilder/core/_client.py b/appbuilder/core/_client.py index 016ab0f7..c07d5c4f 100644 --- a/appbuilder/core/_client.py +++ b/appbuilder/core/_client.py @@ -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 @@ -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", "") + diff --git a/appbuilder/core/components/asr/component.py b/appbuilder/core/components/asr/component.py index da6eca9a..53127786 100644 --- a/appbuilder/core/components/asr/component.py +++ b/appbuilder/core/components/asr/component.py @@ -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参数检查 参数: @@ -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"] + ) diff --git a/appbuilder/core/components/dish_recognize/component.py b/appbuilder/core/components/dish_recognize/component.py index 7bc90e46..22418e17 100644 --- a/appbuilder/core/components/dish_recognize/component.py +++ b/appbuilder/core/components/dish_recognize/component.py @@ -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) diff --git a/appbuilder/core/components/general_ocr/component.py b/appbuilder/core/components/general_ocr/component.py index f78aa109..6211f528 100644 --- a/appbuilder/core/components/general_ocr/component.py +++ b/appbuilder/core/components/general_ocr/component.py @@ -86,14 +86,14 @@ 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返回 @@ -101,5 +101,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") + ) diff --git a/appbuilder/core/components/landmark_recognize/component.py b/appbuilder/core/components/landmark_recognize/component.py index 3b4c57eb..65325f5c 100644 --- a/appbuilder/core/components/landmark_recognize/component.py +++ b/appbuilder/core/components/landmark_recognize/component.py @@ -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参数检查 参数: @@ -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") + ) diff --git a/appbuilder/core/components/object_recognize/component.py b/appbuilder/core/components/object_recognize/component.py index 88cb81b0..e789d2ae 100644 --- a/appbuilder/core/components/object_recognize/component.py +++ b/appbuilder/core/components/object_recognize/component.py @@ -86,14 +86,14 @@ 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返回 @@ -101,5 +101,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") + ) diff --git a/appbuilder/core/components/text_to_image/component.py b/appbuilder/core/components/text_to_image/component.py index 701715c6..8287c2f8 100644 --- a/appbuilder/core/components/text_to_image/component.py +++ b/appbuilder/core/components/text_to_image/component.py @@ -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 @@ -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 @@ -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参数检查 参数: @@ -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")) \ No newline at end of file + raise AppBuilderServerException( + request_id=request_id, + service_err_code=data.get("error_code"), + service_err_message=data.get("error_msg") + ) diff --git a/appbuilder/core/components/translate/component.py b/appbuilder/core/components/translate/component.py index 5148a28f..732f5ac0 100644 --- a/appbuilder/core/components/translate/component.py +++ b/appbuilder/core/components/translate/component.py @@ -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)) diff --git a/appbuilder/core/components/tts/component.py b/appbuilder/core/components/tts/component.py index 55fc8207..ca9026bd 100644 --- a/appbuilder/core/components/tts/component.py +++ b/appbuilder/core/components/tts/component.py @@ -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检查 参数: @@ -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", ""), diff --git a/appbuilder/tests/test_asr.py b/appbuilder/tests/test_asr.py index 82fc4c05..60ab9661 100644 --- a/appbuilder/tests/test_asr.py +++ b/appbuilder/tests/test_asr.py @@ -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__': diff --git a/appbuilder/tests/test_text_to_image.py b/appbuilder/tests/test_text_to_image.py index 744517fa..41881075 100644 --- a/appbuilder/tests/test_text_to_image.py +++ b/appbuilder/tests/test_text_to_image.py @@ -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__':