|
1 | 1 | package meet_at_mensa.matching.client; |
2 | 2 |
|
| 3 | +import org.openapitools.client.Configuration; |
| 4 | +import org.openapitools.client.api.GenAiApi; |
| 5 | +import org.springframework.stereotype.Service; |
| 6 | + |
| 7 | +import meet_at_mensa.matching.exception.RestException; |
| 8 | +import meet_at_mensa.matching.repository.GroupRepository; |
| 9 | +import meet_at_mensa.matching.service.ConversationStarterService; |
| 10 | + |
| 11 | +import org.openapitools.client.ApiClient; |
| 12 | + |
| 13 | +import org.openapitools.model.User; |
| 14 | +import org.openapitools.model.UserCollection; |
| 15 | +import org.openapitools.model.ConversationStarter; |
| 16 | +import org.openapitools.model.ConversationStarterCollection; |
| 17 | + |
| 18 | +import org.openapitools.client.auth.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * GenAiClient uses the generated java client to handle REST requests to the GenAi-Microservice |
| 22 | + * |
| 23 | + * WARNING: This class is currently non-functional due to complications with Auth0 |
| 24 | + * |
| 25 | + * TODO: @AK - Implement Authentication |
| 26 | + * |
| 27 | + */ |
| 28 | +@Service |
3 | 29 | public class GenAiClient { |
| 30 | + |
| 31 | + private ApiClient defaultClient; |
| 32 | + |
| 33 | + public GenAiClient() { |
| 34 | + |
| 35 | + // get default client |
| 36 | + this.defaultClient = Configuration.getDefaultApiClient(); |
| 37 | + |
| 38 | + // set path |
| 39 | + this.defaultClient.setBasePath("http://meetatmensa-genai:80"); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Uses a Generated client to a REST request to genai-service for a ConverationStarterCollection object |
| 46 | + * |
| 47 | + * WARNING: This currently fails due to missing authentication! |
| 48 | + * |
| 49 | + * TODO: @AK Figure out auth0 |
| 50 | + * |
| 51 | + * @param users server-style UserCollection object of the user being fetched |
| 52 | + * @return server-style ConversationStarterCollection object (org.openapitools.model.ConversationStarterCollection) |
| 53 | + */ |
| 54 | + public ConversationStarterCollection getPrompts(UserCollection users) { |
| 55 | + |
| 56 | + // create instance of the API |
| 57 | + GenAiApi apiInstance = new GenAiApi(this.defaultClient); |
| 58 | + |
| 59 | + try { |
| 60 | + |
| 61 | + // initiate object |
| 62 | + org.openapitools.client.model.ConversationStarterCollection clientCollection; |
| 63 | + |
| 64 | + // try to fetch prompts from ConversationStarter |
| 65 | + clientCollection = apiInstance.getApiV2GenaiConversationStarter( |
| 66 | + |
| 67 | + // convert from server-type to client-type |
| 68 | + convertServerUserCollectionToClientUserCollection(users) |
| 69 | + |
| 70 | + ); |
| 71 | + |
| 72 | + // convert from client-type to server-type |
| 73 | + return convertClientPromptsToServerPrompts(clientCollection); |
| 74 | + |
| 75 | + } catch (Exception e) { |
| 76 | + |
| 77 | + throw new RestException(e.toString()); |
| 78 | + |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * Converts a Server-Style User object into a Client-Style User object. |
| 86 | + * |
| 87 | + * Generated clients and servers are generated separatly, and each have their own version of model objects |
| 88 | + * A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same |
| 89 | + * fields, but are distinct in a formal sense. |
| 90 | + * |
| 91 | + * These conversion functions convert between client-type objects returned by generated client functions |
| 92 | + * and server-type objects used by the rest of the application |
| 93 | + * |
| 94 | + * @param serverUser server-style user object (org.openapitools.model.User) |
| 95 | + * @return clientUser client-style user object (org.openapitools.client.model.User) |
| 96 | + */ |
| 97 | + private org.openapitools.client.model.User convertServerUserToClientUser(User serverUser) { |
| 98 | + |
| 99 | + // create new object of client type |
| 100 | + org.openapitools.client.model.User clientUser = new org.openapitools.client.model.User(); |
| 101 | + |
| 102 | + // copy fields |
| 103 | + clientUser.setUserID(serverUser.getUserID()); |
| 104 | + clientUser.setEmail(serverUser.getEmail()); |
| 105 | + clientUser.setFirstname(serverUser.getFirstname()); |
| 106 | + clientUser.setLastname(serverUser.getLastname()); |
| 107 | + clientUser.setBirthday(serverUser.getBirthday()); |
| 108 | + clientUser.setGender(serverUser.getGender()); |
| 109 | + clientUser.setDegree(serverUser.getDegree()); |
| 110 | + clientUser.setDegreeStart(serverUser.getDegreeStart()); |
| 111 | + clientUser.setInterests(serverUser.getInterests()); |
| 112 | + clientUser.setBio(serverUser.getBio()); |
| 113 | + |
| 114 | + // return object |
| 115 | + return clientUser; |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + * Converts a Server-Style UserCollection object into a Client-Style UserCollection object. |
| 122 | + * |
| 123 | + * Generated clients and servers are generated separatly, and each have their own version of model objects |
| 124 | + * A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same |
| 125 | + * fields, but are distinct in a formal sense. |
| 126 | + * |
| 127 | + * These conversion functions convert between client-type objects returned by generated client functions |
| 128 | + * and server-type objects used by the rest of the application |
| 129 | + * |
| 130 | + * @param serverUsers server-style userCollection object (org.openapitools.model.UserCollection) |
| 131 | + * @return clientUsers client-style userCollection object (org.openapitools.client.model.UserCollection) |
| 132 | + */ |
| 133 | + private org.openapitools.client.model.UserCollection convertServerUserCollectionToClientUserCollection(UserCollection serverUsers) { |
| 134 | + |
| 135 | + // create new client-type UserCollection |
| 136 | + org.openapitools.client.model.UserCollection clientUsers = new org.openapitools.client.model.UserCollection(); |
| 137 | + |
| 138 | + // convert to client-user and add to collection |
| 139 | + for (User serverUser : serverUsers.getUsers()) { |
| 140 | + clientUsers.addUsersItem( |
| 141 | + convertServerUserToClientUser(serverUser) |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + // return object |
| 146 | + return clientUsers; |
| 147 | + |
| 148 | + } |
| 149 | + |
4 | 150 |
|
| 151 | + /** |
| 152 | + * Converts a Client-Style ConversationStarter object into a Server-Style ConversationStarter object. |
| 153 | + * |
| 154 | + * Generated clients and servers are generated separatly, and each have their own version of model objects |
| 155 | + * A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same |
| 156 | + * fields, but are distinct in a formal sense. |
| 157 | + * |
| 158 | + * These conversion functions convert between client-type objects returned by generated client functions |
| 159 | + * and server-type objects used by the rest of the application |
| 160 | + * |
| 161 | + * @param clientPromt Client-Style ConversationStarter object (org.openapitools.client.model.ConversationStarter) |
| 162 | + * @return serverPrompt Server-Style ConversationStarter object (org.openapitools.model.ConversationStarter) |
| 163 | + */ |
| 164 | + private ConversationStarter convertClientPromptToServerPrompt(org.openapitools.client.model.ConversationStarter clientPrompt) { |
| 165 | + |
| 166 | + // create new object of server type |
| 167 | + ConversationStarter serverPrompt = new ConversationStarter(); |
| 168 | + |
| 169 | + // copy fields |
| 170 | + serverPrompt.setPrompt(clientPrompt.getPrompt()); |
| 171 | + |
| 172 | + // return server object |
| 173 | + return serverPrompt; |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + /** |
| 179 | + * Converts a Client-Style ConversationStarterCollection object into a Server-Style ConversationStarterCollection object. |
| 180 | + * |
| 181 | + * Generated clients and servers are generated separatly, and each have their own version of model objects |
| 182 | + * A user from org.openapitools.model.User and a user from org.openapitools.client.model.User have the same |
| 183 | + * fields, but are distinct in a formal sense. |
| 184 | + * |
| 185 | + * These conversion functions convert between client-type objects returned by generated client functions |
| 186 | + * and server-type objects used by the rest of the application |
| 187 | + * |
| 188 | + * @param clientPromts Client-Style ConversationStarterCollection object (org.openapitools.client.model.ConversationStarterCollection) |
| 189 | + * @return serverPrompts Server-Style ConversationStarterCollection object (org.openapitools.model.ConversationStarterCollection) |
| 190 | + */ |
| 191 | + private ConversationStarterCollection convertClientPromptsToServerPrompts(org.openapitools.client.model.ConversationStarterCollection clientPrompts) { |
| 192 | + |
| 193 | + // create new object of server type |
| 194 | + ConversationStarterCollection serverPrompts = new ConversationStarterCollection(); |
| 195 | + |
| 196 | + // convert to server-type and add to collection |
| 197 | + for (org.openapitools.client.model.ConversationStarter clientPrompt : clientPrompts.getConversationsStarters()) { |
| 198 | + |
| 199 | + serverPrompts.addConversationsStartersItem( |
| 200 | + convertClientPromptToServerPrompt(clientPrompt) |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + // return server object |
| 205 | + return serverPrompts; |
| 206 | + |
| 207 | + } |
| 208 | + |
5 | 209 | } |
0 commit comments