Ignore:
Timestamp:
2021-08-03T17:12:35+12:00 (3 years ago)
Author:
davidb
Message:

Improve error handling for calls to the Reo Tuhituhi API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/services/ReoTuhituhiApiService.java

    r35241 r35242  
    1111
    1212import com.google.gson.Gson;
     13import com.google.gson.JsonSyntaxException;
    1314
    1415import org.apache.logging.log4j.LogManager;
    1516import org.apache.logging.log4j.Logger;
     17import org.atea.nlptools.koreromaoriinterface.exceptions.ReoTuhituhiException;
    1618import org.atea.nlptools.koreromaoriinterface.models.AudioFilePart;
    1719import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult;
     20import org.atea.nlptools.koreromaoriinterface.services.HttpRequestService.HttpRequestException;
    1821
    1922/**
     
    4346     * @param audioFileParts The audio files to retrieve a transcription for.
    4447     * @return A list of {@link TranscriptionResult} objects.
    45      * @throws Exception
     48     * @throws HttpRequestException When the API call fails.
     49     * @throws JsonSyntaxException When the result cannot be parsed.
    4650     */
    47     public List<AudioFilePart> GetTranscriptions(Iterable<AudioFilePart> audioFileParts)
    48         throws Exception
     51    public List<AudioFilePart> getTranscriptions(Iterable<AudioFilePart> audioFileParts)
     52        throws HttpRequestException, JsonSyntaxException, Exception
    4953    {
    5054        Queue<Future<AudioFilePart>> apiCalls = new LinkedList<Future<AudioFilePart>>();
     
    6468            TranscriptionResult res = getTranscription(part.dataStream);
    6569            part.setTranscriptionResult(res);
     70
    6671            apiResults.add(part);
    6772        }
     
    7883    }
    7984
     85    /**
     86     * Calls the Reo Tuhituhi API to transcribe a wave audio file.
     87     *
     88     * @param audioStream The wave audio stream.
     89     * @return A {@link TranscriptionResult} object.
     90     * @throws HttpRequestException Thrown when the API call fails.
     91     * @throws JsonSyntaxException Thrown when the result cannot be parsed.
     92     * @throws ReoTuhituhiException Thrown when the Reo Tuhituhi API returns an invalid response.
     93     */
     94    public TranscriptionResult getTranscription(InputStream audioStream)
     95        throws HttpRequestException, JsonSyntaxException, ReoTuhituhiException
     96    {
     97        HttpRequestService request = HttpRequestService
     98            .post(apiEndpoint)
     99            .authorization("Basic " + apiKey)
     100            .send(audioStream);
     101
     102        // Check that the request returned an OK status
     103        if (!request.ok())
     104        {
     105            logger.error
     106            (
     107                "The Reo Tuhituhi API returned a non-OK status code {} with message {}",
     108                request.code(),
     109                request.message()
     110            );
     111
     112            throw new ReoTuhituhiException(request.code(), request.message(), "Non-OK status code");
     113        }
     114
     115        // Check that the content type is valid
     116        if (request.contentType() != "application/json")
     117        {
     118            logger.error
     119            (
     120                "The Reo Tuhituhi API returned an invalid content type {}. Provided content was {}",
     121                request.contentType(),
     122                request.body()
     123            );
     124
     125            throw new ReoTuhituhiException(request.code(), request.message(), "Invalid content type: " + request.contentType());
     126        }
     127
     128        TranscriptionResult res = jsonSerialiser.fromJson(request.body(), TranscriptionResult.class);
     129
     130        return res;
     131    }
     132
    80133    private Callable<AudioFilePart> getTranscriptionCallable(final AudioFilePart part)
    81134    {
     
    83136        {
    84137            @Override
    85             public AudioFilePart call() throws Exception
     138            public AudioFilePart call()
     139                throws Exception
    86140            {
    87141                TranscriptionResult res = getTranscription(part.dataStream);
     
    91145        };
    92146    }
    93 
    94     private TranscriptionResult getTranscription(InputStream audioStream)
    95         throws Exception
    96     {
    97         String jsonResponse = HttpRequestService
    98             .post(apiEndpoint)
    99             .authorization("Basic " + apiKey)
    100             .send(audioStream)
    101             .body();
    102 
    103         logger.trace("Reo Tuhituhi API responded with a success status.");
    104         TranscriptionResult res = jsonSerialiser.fromJson(jsonResponse, TranscriptionResult.class);
    105         logger.trace("Reo Tuhituhi API response was succesfully deserialised.");
    106         return res;
    107     }
    108147}
Note: See TracChangeset for help on using the changeset viewer.