Skip to content

Commit

Permalink
Setup infrastructure for mockingWhisperNConnection
Browse files Browse the repository at this point in the history
Co-authored-by: Samantha Prestrelski <sprestrelski@users.noreply.github.com>
  • Loading branch information
AllKeng and sprestrelski committed Nov 11, 2023
1 parent 545508c commit ede6d35
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 76 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/code/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code;

import code.client.Controllers.Controller;
import code.client.Model.Model;
import code.client.View.View;
import javafx.application.Application;
import javafx.scene.Scene;
Expand All @@ -17,8 +16,8 @@ public static void main(String[] args) {
public void start(Stage primaryStage) {

View view = new View();
Model model = new Model();
Controller controller = new Controller(view, model);
// Model model = new Model();
Controller controller = new Controller(view);// , model);

Scene scene = new Scene(view.getGrid(), 1200, 600);
primaryStage.setScene(scene);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void stopRecording() {
}

public boolean toggleRecording() {
if(recordingState) {
if (recordingState) {
stopRecording();
recordingState = false;
return false;
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/code/client/Controllers/Controller.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code.client.Controllers;

import code.client.Controllers.AudioRecorder;
import code.client.Model.Model;
import code.client.View.View;
import javafx.event.ActionEvent;

Expand All @@ -10,12 +9,10 @@
*/
public class Controller {
private View view;
private Model model;
private AudioRecorder audioRecorder;

public Controller(View view, Model model) {
public Controller(View view) {
this.view = view;
this.model = model;
this.audioRecorder = new AudioRecorder(view.getRecordingLabel());

// recording buttons
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/code/client/Model/CustomHttpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package code.client.Model;

import java.net.ProtocolException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public interface CustomHttpConnection {
int getResponseCode() throws IOException;

InputStream getInputStream() throws IOException;

OutputStream getOutputStream() throws IOException;

InputStream getErrorStream() throws IOException;

void setRequestMethod(String method) throws ProtocolException;

void setRequestProperty(String key, String value);

void setDoOutput(boolean output);

void disconnect();

}
54 changes: 54 additions & 0 deletions app/src/main/java/code/client/Model/MockHttpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package code.client.Model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MockHttpConnection implements CustomHttpConnection {
private int responseCode;
private InputStream inputStream;
private OutputStream outputStream;

public MockHttpConnection(int responseCode, InputStream inputStream, OutputStream outputStream) {
this.responseCode = responseCode;
this.inputStream = inputStream;
this.outputStream = outputStream;
}

@Override
public int getResponseCode() throws IOException {
return responseCode;
}

@Override
public InputStream getInputStream() {
return inputStream;
}

@Override
public OutputStream getOutputStream() {
return outputStream;
}

@Override
public InputStream getErrorStream() {
return inputStream;
}

@Override
public void setRequestMethod(String method) {
}

@Override
public void setRequestProperty(String key, String value) {

}

@Override
public void setDoOutput(boolean output) {
}

@Override
public void disconnect() {
}
}
52 changes: 0 additions & 52 deletions app/src/main/java/code/client/Model/Model.java

This file was deleted.

61 changes: 61 additions & 0 deletions app/src/main/java/code/client/Model/RealHttpConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package code.client.Model;

import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class RealHttpConnection implements CustomHttpConnection {
private HttpURLConnection connection;

public RealHttpConnection(String API_ENDPOINT)
throws URISyntaxException, IOException {
URL url = new URI(API_ENDPOINT).toURL();
this.connection = (HttpURLConnection) url.openConnection();
}

@Override
public int getResponseCode() throws IOException {
return connection.getResponseCode();
}

@Override
public InputStream getInputStream() throws IOException {
return connection.getInputStream();
}

@Override
public OutputStream getOutputStream() throws IOException {
return connection.getOutputStream();
}

@Override
public InputStream getErrorStream() throws IOException {
return connection.getErrorStream();
}

@Override
public void setRequestMethod(String method) throws ProtocolException {
connection.setRequestMethod(method);
}

@Override
public void setRequestProperty(String key, String value) {
connection.setRequestProperty(key, value);
}

@Override
public void setDoOutput(boolean output) {
connection.setDoOutput(output);
}

@Override
public void disconnect() {

}

}
32 changes: 19 additions & 13 deletions app/src/main/java/code/client/Model/WhisperHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ public class WhisperHandler {
private String API_ENDPOINT;
private String TOKEN;
private String MODEL;
private CustomHttpConnection connection;

public WhisperHandler(String API_ENDPOINT, String TOKEN, String MODEL) {
public WhisperHandler(String API_ENDPOINT, String TOKEN, String MODEL) throws IOException, URISyntaxException {
this.API_ENDPOINT = API_ENDPOINT;
this.TOKEN = TOKEN;
this.MODEL = MODEL;
this.connection = sendHttpRequest();
}

public WhisperHandler(String API_ENDPOINT, String TOKEN, String MODEL, CustomHttpConnection connection) {
this.API_ENDPOINT = API_ENDPOINT;
this.TOKEN = TOKEN;
this.MODEL = MODEL;
this.connection = connection;
}

// https://stackoverflow.com/questions/25334139/how-to-mock-a-url-connection
public String processAudio() throws IOException, URISyntaxException {
// Create file object from file path
File file = new File("recording.wav");
HttpURLConnection connection = sendHttpRequest(file);

// Get response code
int responseCode = connection.getResponseCode();
String response;
Expand All @@ -47,17 +52,18 @@ public String processAudio() throws IOException, URISyntaxException {
return response;
}

private HttpURLConnection sendHttpRequest(File file) throws IOException, URISyntaxException {
URL url = new URI(API_ENDPOINT).toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);

public CustomHttpConnection sendHttpRequest() throws IOException, URISyntaxException {
// Set up request headers
File file = new File("recording.wav");
CustomHttpConnection connection = new RealHttpConnection(API_ENDPOINT);

String boundary = "Boundary-" + System.currentTimeMillis();
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setRequestProperty("Authorization", "Bearer " + TOKEN);

connection.setRequestMethod("POST");
connection.setDoOutput(true);

// Set up output stream to write request body
OutputStream outputStream = connection.getOutputStream();

Expand Down Expand Up @@ -113,7 +119,7 @@ private static void writeFileToOutputStream(
}

// Helper method to handle a successful response
private static String handleSuccessResponse(HttpURLConnection connection)
private static String handleSuccessResponse(CustomHttpConnection connection)
throws IOException, JSONException {
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
Expand All @@ -133,7 +139,7 @@ private static String handleSuccessResponse(HttpURLConnection connection)
}

// Helper method to handle an error response
private static String handleErrorResponse(HttpURLConnection connection)
private static String handleErrorResponse(CustomHttpConnection connection)
throws IOException, JSONException {
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(connection.getErrorStream()));
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/code/client/View/NewRecipeUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class AppFrameMic extends BorderPane {
private ArrayList<IWindowUI> scenes;
private Stage primaryStage;

AppFrameMic() {
AppFrameMic() throws IOException, URISyntaxException {
header = new Header();
mealTypeSelection = new MealTypeSelection();
ingredientsList = new IngredientsList();
Expand Down Expand Up @@ -191,7 +191,7 @@ public void setScenes(Stage primaryStage, ArrayList<IWindowUI> scenes) {
this.primaryStage = primaryStage;
}

public void addListeners() {
public void addListeners() throws IOException, URISyntaxException {

AudioRecorder recorder = new AudioRecorder(new Label("Recording..."));
WhisperHandler audioProcessor = new WhisperHandler(API_ENDPOINT, TOKEN, MODEL);
Expand Down Expand Up @@ -270,7 +270,7 @@ public class NewRecipeUI implements IWindowUI {
private Stage primaryStage;
private AppFrameMic root;

NewRecipeUI() {
NewRecipeUI() throws IOException, URISyntaxException {
root = new AppFrameMic();
}

Expand Down

0 comments on commit ede6d35

Please sign in to comment.