Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing https://github.com/attdevsupport/codekit-java/issues/4 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codekit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.att.api</groupId>
<artifactId>codekit</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<name>codekit</name>
Expand Down
27 changes: 23 additions & 4 deletions codekit/src/main/java/com/att/api/rest/RESTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,21 @@ public APIResponse httpPost(String body) throws RESTException {
}
}

/**
* Sends an http POST request with the POST body set to the file.
*
* <p>
* <strong>NOTE</strong>: Any parameters set using
* <code>addParameter()</code> or <code>setParameter()</code> will be
* ignored.
* </p>
*
* @param file file to use as POST body@return api response
* @throws RESTException if POST was unsuccessful
*/
public APIResponse httpPost(File file) throws RESTException {
return httpPost(file, null);
}
/**
* Sends an http POST request with the POST body set to the file.
*
Expand All @@ -568,20 +583,23 @@ public APIResponse httpPost(String body) throws RESTException {
* </p>
*
* @param file file to use as POST body
* @param mimeType mime type to use with the file (null for auto-detection)
* @return api response
* @throws RESTException if POST was unsuccessful
*/
public APIResponse httpPost(File file) throws RESTException {
public APIResponse httpPost(File file, String mimeType) throws RESTException {
HttpResponse response = null;
try {
HttpClient httpClient = createClient();

HttpPost httpPost = new HttpPost(url);
addInternalHeaders(httpPost);
if (mimeType == null) {
// detect the mime type
mimeType = this.getMIMEType(file);
}

String contentType = this.getMIMEType(file);

httpPost.setEntity(new FileEntity(file, contentType));
httpPost.setEntity(new FileEntity(file, mimeType));

return buildResponse(httpClient.execute(httpPost));
} catch (Exception e) {
Expand Down Expand Up @@ -673,6 +691,7 @@ private String getMIMEType(File file) throws IOException {
contentType = "audio/amr";
}
}

} catch (IOException ioe) {
throw ioe; // pass along exception
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public SpeechResponse speechToText(File audio, String xArgs,
*/
public SpeechResponse speechToText(File audio, String xArgs,
String speechContext, String subContext) throws RESTException {

return speechToText(audio, null, xArgs, speechContext, subContext );
}
/**
* Sends the request to the server.
*
* @param audio audio file to convert to text
* @param mimeType for audio file (null for auto-detection)
* @param xArgs Special information about the request
* @param speechContext additional context information about the audio
* @param subContext speechContext additional information
*
* @return SpeechResponse object
* @throws RESTException
* @see SpeechResponse
*/
public SpeechResponse speechToText(File audio, String mimeType, String xArgs,
String speechContext, String subContext ) throws RESTException {
final String endpoint = getFQDN() + "/speech/v3/speechToText";

RESTClient restClient = new RESTClient(endpoint)
Expand All @@ -108,7 +126,9 @@ public SpeechResponse speechToText(File audio, String xArgs,
&& speechContext.equals("Gaming")) {
restClient.addHeader("X-SpeechSubContext", subContext);
}
APIResponse apiResponse = restClient.httpPost(audio);

APIResponse apiResponse = restClient.httpPost(audio, mimeType);

try {
return SpeechResponse.valueOf(
new JSONObject(apiResponse.getResponseBody()));
Expand Down