-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAIResponse.java
More file actions
82 lines (72 loc) · 2.92 KB
/
RequestAIResponse.java
File metadata and controls
82 lines (72 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package Tutorial;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
/*
This sends a request to the server for a response to the message and receives a request id
*/
public class RequestAIResponse {
OkHttpClient client;
public RequestAIResponse(){
client = new OkHttpClient();
}
public CompletableFuture<String> que(String postBody){
Request request = new Request.Builder()
.url("http://localhost:5000/")
.post(RequestBody.create(MediaType.parse("text/plain"), postBody))
.build();
CompletableFuture<String> futureAns = new CompletableFuture<String>();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
System.out.print("request failed");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
String ans = response.body().string();
System.out.print(ans);
futureAns.complete(ans);
}
});
return futureAns;
}
/*
This method pings the server with the request id to check if the request is done processing
*/
public CompletableFuture<String> getResponse (String requestId){
Request request = new Request.Builder()
.url("http://localhost:5000/retrieve")
.post(RequestBody.create(MediaType.parse("text/plain"), requestId))
.build();
CompletableFuture<String>futureResponse = new CompletableFuture<String>();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e){ System.out.print("response failed"); }
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
int responseCode = response.code();
//checks the code to see if it's still processing
if(responseCode == 202){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Call newCall = client.newCall(request);
newCall.enqueue(this);
}
//received an answer
else if(responseCode == 200){
String ans = response.body().string();
System.out.print(ans);
futureResponse.complete(ans);
}
else{
System.out.printf("error code: %d", responseCode);
}
}
});
return futureResponse;
}
}