Fixed bug in the creation of the JSON request

This commit is contained in:
0xboobface 2018-08-13 18:28:15 +02:00
parent c64359d455
commit 16efe49324
1 changed files with 32 additions and 7 deletions

View File

@ -37,7 +37,7 @@ public class RemoteRecorder implements Recorder {
.build();
private JsonAdapter<ModelListResponse> modelListResponseAdapter = moshi.adapter(ModelListResponse.class);
private JsonAdapter<RecordingListResponse> recordingListResponseAdapter = moshi.adapter(RecordingListResponse.class);
private JsonAdapter<Model> modelAdapter = moshi.adapter(Model.class);
private JsonAdapter<ModelRequest> modelRequestAdapter = moshi.adapter(ModelRequest.class);
private List<Model> models = Collections.emptyList();
@ -65,15 +65,13 @@ public class RemoteRecorder implements Recorder {
}
private void sendRequest(String action, Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException {
String requestTemplate = "{\"action\": \"<<action>>\", \"model\": <<model>>}";
requestTemplate = requestTemplate.replaceAll("<<action>>", action);
requestTemplate = requestTemplate.replaceAll("<<model>>", modelAdapter.toJson(model));
LOG.debug("Sending request to recording server: {}", requestTemplate);
RequestBody body = RequestBody.create(JSON, requestTemplate);
String payload = modelRequestAdapter.toJson(new ModelRequest(action, model));
LOG.debug("Sending request to recording server: {}", payload);
RequestBody body = RequestBody.create(JSON, payload);
Request.Builder builder = new Request.Builder()
.url("http://" + config.getSettings().httpServer + ":" + config.getSettings().httpPort + "/rec")
.post(body);
addHmacIfNeeded(requestTemplate, builder);
addHmacIfNeeded(payload, builder);
Request request = builder.build();
Response response = client.execute(request);
String json = response.body().string();
@ -239,4 +237,31 @@ public class RemoteRecorder implements Recorder {
public File merge(Recording recording, boolean keepSegments) throws IOException {
throw new RuntimeException("Merging not available for remote recorder");
}
public static class ModelRequest {
private String action;
private Model model;
public ModelRequest(String action, Model model) {
super();
this.action = action;
this.model = model;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public Model getModel() {
return model;
}
public void setModel(Model model) {
this.model = model;
}
}
}