Esempio n. 1
0
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(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
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(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
Esempio n. 3
0
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
Esempio n. 4
0
    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)
Esempio n. 5
0
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

    dict_repr = detailed_response._to_dict()
    assert dict_repr['result'] == ['foobar', 'baz']
    detailed_response.__str__()