17
17
*/
18
18
package org .wso2 .carbon .apimgt .rest .api .publisher .v1 .impl ;
19
19
20
+ import com .fasterxml .jackson .core .JsonProcessingException ;
21
+ import com .fasterxml .jackson .databind .JsonMappingException ;
20
22
import com .fasterxml .jackson .databind .ObjectMapper ;
21
23
import com .google .gson .Gson ;
22
24
import org .apache .commons .lang3 .StringUtils ;
52
54
public class DesignAssistantApiServiceImpl implements DesignAssistantApiService {
53
55
54
56
private static final Log log = LogFactory .getLog (DesignAssistantApiServiceImpl .class );
55
-
56
57
private static DesignAssistantConfigurationDTO configDto ;
57
58
58
59
@ Override
@@ -84,12 +85,13 @@ public Response designAssistantApiPayloadGen(DesignAssistantGenAPIPayloadDTO des
84
85
if (configDto .isKeyProvided ()) {
85
86
response = APIUtil .invokeAIService (configDto .getEndpoint (), configDto .getTokenEndpoint (),
86
87
configDto .getKey (), configDto .getGenApiPayloadResource (), payload .toString (), null );
88
+
87
89
} else {
88
90
response = APIUtil .invokeAIService (configDto .getEndpoint (), null ,
89
91
configDto .getAccessToken (), configDto .getGenApiPayloadResource (), payload .toString (), null );
92
+
90
93
}
91
94
92
- ObjectMapper objectMapper = new ObjectMapper ();
93
95
DesignAssistantAPIPayloadResponseDTO responseDTO = new DesignAssistantAPIPayloadResponseDTO ();
94
96
responseDTO .setGeneratedPayload (response );
95
97
@@ -122,35 +124,34 @@ public Response designAssistantChat(DesignAssistantChatQueryDTO designAssistantC
122
124
}
123
125
try {
124
126
if (configDto .isKeyProvided () || configDto .isAuthTokenProvided ()) {
125
-
126
- boolean isChatQueryEmpty = StringUtils .isEmpty (designAssistantChatQueryDTO . getSessionId () );
127
+ String sessionId = designAssistantChatQueryDTO . getSessionId ();
128
+ boolean isChatQueryEmpty = StringUtils .isEmpty (sessionId );
127
129
if (isChatQueryEmpty ) {
128
130
String errorMessage = "Payload is badly formatted. Expected to have 'sessionId'" ;
129
131
RestApiUtil .handleBadRequest (errorMessage , log );
130
132
return null ;
131
133
}
132
134
133
135
JSONObject payload = new JSONObject ();
134
- String text = new Gson ().toJson (designAssistantChatQueryDTO .getText ());
135
- String sessionId = new Gson ().toJson (designAssistantChatQueryDTO .getSessionId ());
136
136
137
- payload .put (APIConstants .QUERY , designAssistantChatQueryDTO .getSessionId ());
138
- payload .put (APIConstants .SESSIONID , designAssistantChatQueryDTO . getSessionId () );
137
+ payload .put (APIConstants .TEXT , designAssistantChatQueryDTO .getText ());
138
+ payload .put (APIConstants .SESSIONID , sessionId );
139
139
140
140
String response ;
141
141
if (configDto .isKeyProvided ()) {
142
142
response = APIUtil .invokeAIService (configDto .getEndpoint (), configDto .getTokenEndpoint (),
143
143
configDto .getKey (), configDto .getChatResource (), payload .toString (), null );
144
+
144
145
} else {
145
146
response = APIUtil .invokeAIService (configDto .getEndpoint (), null ,
146
147
configDto .getAccessToken (), configDto .getChatResource (), payload .toString (), null );
148
+
147
149
}
148
-
150
+
149
151
ObjectMapper objectMapper = new ObjectMapper ();
150
152
DesignAssistantChatResponseDTO responseDTO = objectMapper .readValue (response , DesignAssistantChatResponseDTO .class );
151
153
152
154
return Response .ok (responseDTO ).build ();
153
-
154
155
}
155
156
} catch (APIManagementException e ) {
156
157
if (RestApiUtil .isDueToAIServiceNotAccessible (e )) {
@@ -162,7 +163,9 @@ public Response designAssistantChat(DesignAssistantChatQueryDTO designAssistantC
162
163
"Assistant service" ;
163
164
RestApiUtil .handleInternalServerError (errorMessage , e , log );
164
165
}
166
+ } catch (JsonProcessingException e ) {
167
+ throw new RuntimeException (e );
165
168
}
166
169
return null ;
167
170
}
168
- }
171
+ }
0 commit comments