-
Notifications
You must be signed in to change notification settings - Fork 3
/
AsyncDAOImpl.java
188 lines (130 loc) · 7.21 KB
/
AsyncDAOImpl.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package io.github.namankhurpia.DAO;
import io.github.namankhurpia.Exception.MalformedRequestException;
import io.github.namankhurpia.Interfaces.AsyncApiInterface;
import io.github.namankhurpia.Interfaces.RetrofitApiInterface;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse;
import io.github.namankhurpia.Pojo.Completion.CompletionResponse;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;
import io.github.namankhurpia.Pojo.Vision.VisionApiRequest;
import io.github.namankhurpia.Pojo.Vision.VisionApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static io.github.namankhurpia.Validators.ParameterCheckers.*;
public class AsyncDAOImpl implements AsyncApiInterface {
ModerationAPIResponse moderationAPIResponseObj;
CompletionResponse completionResponseObj;
ChatCompletionResponse chatCompletionResponseObj;
VisionApiResponse visionApiResponseObj;
private static Logger LOGGER = LoggerFactory.getLogger(AsyncDAOImpl.class);
RetrofitApiInterface retrofitApiInterfaceObj;
@Override
public ModerationAPIResponse getASyncModeration(String accessToken, ModerationAPIRequest request) throws IOException, ExecutionException, InterruptedException {
// Param checking
if (checkParamForModeration(request)) {
throw new MalformedRequestException("Request object has Model name empty or Input empty ", new Throwable());
}
retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);
LOGGER.info("Making request " + accessToken + " with request " + request.toString());
CompletableFuture<ModerationAPIResponse> future = new CompletableFuture<>();
Call<ModerationAPIResponse> call = retrofitApiInterfaceObj.getModeration("Bearer "+accessToken, request);
call.enqueue(new Callback<ModerationAPIResponse>() {
@Override
public void onResponse(Call<ModerationAPIResponse> call, Response<ModerationAPIResponse> response) {
if (response.isSuccessful()) {
moderationAPIResponseObj = response.body();
future.complete(response.body());
}
else
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
LOGGER.error("HTTP Status Code: " + httpStatusCode);
LOGGER.error("Error Body: " + errorBody.toString());
future.completeExceptionally(new MalformedRequestException(errorBody, new Throwable(errorBody)));
}
}
@Override
public void onFailure(Call<ModerationAPIResponse> call, Throwable throwable) {
future.completeExceptionally(throwable);
}
});
return future.get();
}
@Override
public ChatCompletionResponse getAsyncChatCompletion(String accessToken, ChatCompletionRequest request) throws IOException, ExecutionException, InterruptedException {
//param checking
if(checkParamForChatCompletion_Messages_role_content(request))
{
throw new MalformedRequestException("messages Object has either role or content Empty", new Throwable());
}
if(checkParamForChatCompletion_modelName(request))
{
throw new MalformedRequestException("Request object has Model name empty, please specify a model you wish to use", new Throwable());
}
retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);
LOGGER.info("making req" + accessToken + " with request "+ request.toString());
CompletableFuture<ChatCompletionResponse> future = new CompletableFuture<>();
Call<ChatCompletionResponse> call = retrofitApiInterfaceObj.chatCompletion("Bearer "+accessToken,request);
call.enqueue(new Callback<ChatCompletionResponse>() {
@Override
public void onResponse(Call<ChatCompletionResponse> call, Response<ChatCompletionResponse> response) {
if(response.isSuccessful())
{
chatCompletionResponseObj = response.body();
future.complete(response.body());
LOGGER.info("Correct response" + chatCompletionResponseObj.toString());
}
else
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
LOGGER.error("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBody);
future.completeExceptionally(new MalformedRequestException(errorBody, new Throwable(errorBody)));
}
}
@Override
public void onFailure(Call<ChatCompletionResponse> call, Throwable throwable) {
future.completeExceptionally(throwable);
}
});
return future.get();
}
@Override
public VisionApiResponse getAsyncVisionAPI(String accessToken, VisionApiRequest request) throws IOException, ExecutionException, InterruptedException {
retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);
LOGGER.info("making req" + accessToken + " with request "+ request.toString());
CompletableFuture<VisionApiResponse> future = new CompletableFuture<>();
Call<VisionApiResponse> call = retrofitApiInterfaceObj.visionAPI("Bearer "+accessToken,request);
call.enqueue(new Callback<VisionApiResponse>() {
@Override
public void onResponse(Call<VisionApiResponse> call, Response<VisionApiResponse> response) {
if(response.isSuccessful())
{
visionApiResponseObj = response.body();
future.complete(response.body());
LOGGER.info("Correct response" + visionApiResponseObj.toString());
}
else
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
LOGGER.error("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBody);
future.completeExceptionally(new MalformedRequestException(errorBody, new Throwable(errorBody)));
}
}
@Override
public void onFailure(Call<VisionApiResponse> call, Throwable throwable) {
future.completeExceptionally(throwable);
}
});
return future.get();
}
}