Skip to content

Commit

Permalink
Merge pull request #10 from nick-ching23/Service
Browse files Browse the repository at this point in the history
Integrated Python microservice APIs
  • Loading branch information
nick-ching23 authored Oct 15, 2024
2 parents f6d19a5 + b0447dc commit b69e45a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
5 changes: 5 additions & 0 deletions veritas/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>mybatis-plus-annotation</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>6.1.6</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pineapple.veritas.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class VeritasConfig {
@Bean
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pineapple.veritas.response;

public class CheckTextResponse {
private Boolean result;

public Boolean getResult() {
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import com.pineapple.veritas.entity.Record;
import com.pineapple.veritas.mapper.RecordMapper;
import java.util.List;

import com.pineapple.veritas.response.CheckTextResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.HashMap;
Expand All @@ -16,17 +22,61 @@ public class VeritasService {
@Autowired
RecordMapper recordMapper;

public ResponseEntity<?> checkText(String text) {
//This should be calling the OpenAI API, change return val
return new ResponseEntity<>(0, HttpStatus.OK);
@Autowired
private WebClient.Builder webClientBuilder;

@Value("${external.api.url}")
private String modelUrl;

public ResponseEntity<Boolean> checkText(String text) {
if (text == null || text.isEmpty()) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

WebClient webClient = webClientBuilder.build();
String apiCall = modelUrl + "/api/check-text";

try {
Boolean response = webClient
.post()
.uri(apiCall)
.header("Content-Type", "application/json")
.bodyValue(Map.of("text", text))
.retrieve()
.bodyToMono(CheckTextResponse.class)
.map(CheckTextResponse::getResult)
.block();
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (WebClientResponseException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

public ResponseEntity<?> checkTextUser(String text, String userID, String orgID) {
if (userID == null || userID.isEmpty()) {
return new ResponseEntity<>("UserID cannot be null or empty", HttpStatus.BAD_REQUEST);
}
if (orgID == null || orgID.isEmpty()) {
return new ResponseEntity<>("OrgID cannot be null or empty", HttpStatus.BAD_REQUEST);
}
ResponseEntity<Boolean> textRes;
try {
textRes = (ResponseEntity<Boolean>) checkText(text);
} catch (Exception e) {
return new ResponseEntity<>("Error while checking text: " + e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
}

if (textRes.getStatusCode() != HttpStatus.OK) {
return new ResponseEntity<>("Error while checking text", textRes.getStatusCode());
}

if (textRes == null || textRes.getBody() == null) {
return new ResponseEntity<>("Invalid response from text verification service", HttpStatus.INTERNAL_SERVER_ERROR);
}

ResponseEntity<Integer> textRes = (ResponseEntity<Integer>) checkText(text);
//this will probably not be an int
Integer flagged = textRes.getBody();
int threshold = 0; //change this later
Boolean flagged = textRes.getBody();

Map<String, Object> recordMap = new HashMap<>();
recordMap.put("orgID", orgID);
Expand All @@ -36,19 +86,18 @@ public ResponseEntity<?> checkTextUser(String text, String userID, String orgID)
Record record = new Record();
record.setOrgID(orgID);
record.setUserID(userID);
if (flagged > threshold) {
if (Boolean.TRUE.equals(flagged)) {
record.setNumFlags(1);
} else {
record.setNumFlags(0);
}
recordMapper.insert(record);
} else if (flagged > threshold) {
} else if (Boolean.TRUE.equals(flagged)) {
Record record = records.get(0);
record.setNumFlags(record.getNumFlags() + 1);
recordMapper.updateById(record);
}
//change return val
return new ResponseEntity<>(null, HttpStatus.OK);
return new ResponseEntity<>("Operation completed successfully", HttpStatus.OK);
}

public ResponseEntity<?> numFlags(String userID, String orgID) {
Expand Down
4 changes: 4 additions & 0 deletions veritas/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ spring:
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver

external:
api:
url: ${MODEL_MICROSERVICE_URL}

0 comments on commit b69e45a

Please sign in to comment.