Changeset 35242 for gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/services/ReoTuhituhiApiService.java
- Timestamp:
- 2021-08-03T17:12:35+12:00 (3 years ago)
- 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 11 11 12 12 import com.google.gson.Gson; 13 import com.google.gson.JsonSyntaxException; 13 14 14 15 import org.apache.logging.log4j.LogManager; 15 16 import org.apache.logging.log4j.Logger; 17 import org.atea.nlptools.koreromaoriinterface.exceptions.ReoTuhituhiException; 16 18 import org.atea.nlptools.koreromaoriinterface.models.AudioFilePart; 17 19 import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult; 20 import org.atea.nlptools.koreromaoriinterface.services.HttpRequestService.HttpRequestException; 18 21 19 22 /** … … 43 46 * @param audioFileParts The audio files to retrieve a transcription for. 44 47 * @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. 46 50 */ 47 public List<AudioFilePart> GetTranscriptions(Iterable<AudioFilePart> audioFileParts)48 throws Exception51 public List<AudioFilePart> getTranscriptions(Iterable<AudioFilePart> audioFileParts) 52 throws HttpRequestException, JsonSyntaxException, Exception 49 53 { 50 54 Queue<Future<AudioFilePart>> apiCalls = new LinkedList<Future<AudioFilePart>>(); … … 64 68 TranscriptionResult res = getTranscription(part.dataStream); 65 69 part.setTranscriptionResult(res); 70 66 71 apiResults.add(part); 67 72 } … … 78 83 } 79 84 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 80 133 private Callable<AudioFilePart> getTranscriptionCallable(final AudioFilePart part) 81 134 { … … 83 136 { 84 137 @Override 85 public AudioFilePart call() throws Exception 138 public AudioFilePart call() 139 throws Exception 86 140 { 87 141 TranscriptionResult res = getTranscription(part.dataStream); … … 91 145 }; 92 146 } 93 94 private TranscriptionResult getTranscription(InputStream audioStream)95 throws Exception96 {97 String jsonResponse = HttpRequestService98 .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 }108 147 }
Note:
See TracChangeset
for help on using the changeset viewer.