def _parse_recognition_response( response: DetailedResponse) -> List[Tuple[str, float]]: """ Return the transcription result """ if response.status_code != 200: raise RecognitionError("There was an error in the recognition service") alt_dicts = response.get_result().get('results', []) transcripts = [(alt.get('transcript'), alt.get('confidence')) for alt_dict in alt_dicts for alt in alt_dict.get('alternatives')] return transcripts
def _process_transcribed_json(self, transcribed_data: DetailedResponse) -> str: response = transcribed_data.get_result() assert response.ok try: transcript = response["results"][0]["alternatives"][0][ "transcript"] return cast(str, transcript) except Exception as e: raise Exception(e)
def prepare_api_request( self, method: str, url: str, data: Optional[Union[str, dict]] = None) -> DetailedResponse: """ Prepare the API call request Args: method: Method for the request url: Url of the request data: data to be send. Returns: return the DetailedResponse. """ headers = {} if data and isinstance(data, dict): data = self.__remove_null_values(data) headers['Content-Type'] = 'application/json' headers['User-Agent'] = '{0}/{1}'.format(config_constants.SDK_NAME, __version__) data = json_import.dumps(data) try: request = self.prepare_request(method=method, url=url, headers=headers, data=data) response = self.send(request) return response except ApiException as api_exception: return DetailedResponse(response=None, headers=None, status_code=api_exception.code) except Exception as exception: Logger.debug(f'Error in service API call {str(exception)}') return DetailedResponse(status_code=400)
def test_detailed_response(): responses.add(responses.GET, 'https://test.com', status=200, body=json.dumps({'foobar': 'baz'}), content_type='application/json') mock_response = requests.get('https://test.com') detailed_response = DetailedResponse(mock_response.json(), mock_response.headers, mock_response.status_code) assert detailed_response is not None assert detailed_response.get_result() == {'foobar': 'baz'} assert detailed_response.get_headers() == { u'Content-Type': 'application/json' } assert detailed_response.get_status_code() == 200 dict_repr = detailed_response._to_dict() assert dict_repr['result'] == {'foobar': 'baz'} detailed_response.__str__()
def test_detailed_response_list(): responses.add(responses.GET, 'https://test.com', status=200, body=json.dumps(['foobar', 'baz']), content_type='application/json') mock_response = requests.get('https://test.com') detailed_response = DetailedResponse(mock_response.json(), mock_response.headers, mock_response.status_code) assert detailed_response is not None assert detailed_response.get_result() == ['foobar', 'baz'] assert detailed_response.get_headers() == { u'Content-Type': 'application/json' } assert detailed_response.get_status_code() == 200 response_str = clean(detailed_response.__str__()) assert clean(detailed_response.get_result().__str__()) in response_str #assert clean(detailed_response.get_headers().__str__()) in response_str assert clean(detailed_response.get_status_code().__str__()) in response_str
def test_detailed_response_dict(): responses.add(responses.GET, 'https://test.com', status=200, body=json.dumps({'foobar': 'baz'}), content_type='application/json') mock_response = requests.get('https://test.com') detailed_response = DetailedResponse(response=mock_response.json(), headers=mock_response.headers, status_code=mock_response.status_code) assert detailed_response is not None assert detailed_response.get_result() == {'foobar': 'baz'} assert detailed_response.get_headers() == { 'Content-Type': 'application/json' } assert detailed_response.get_status_code() == 200 response_str = clean(str(detailed_response)) assert clean(str(detailed_response.get_result())) in response_str #assert clean(str(detailed_response.get_headers())) in response_str assert clean(str(detailed_response.get_status_code())) in response_str