|
1 | 1 | package com.api.sss.model.service; |
2 | 2 |
|
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.springframework.http.HttpEntity; |
| 8 | +import org.springframework.http.HttpHeaders; |
| 9 | +import org.springframework.http.HttpMethod; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.web.client.RestTemplate; |
| 14 | + |
3 | 15 | import com.api.sss.config.exception.CustomException; |
4 | 16 | import com.api.sss.config.exception.ErrorCode; |
5 | 17 | import com.api.sss.model.dto.request.ChatAskRequest; |
6 | | -import com.api.sss.model.dto.response.ChatAskResponse; |
7 | 18 | import com.api.sss.model.dto.request.NewsRequest; |
| 19 | +import com.api.sss.model.dto.response.ChatAskResponse; |
8 | 20 | import com.api.sss.model.dto.response.NewsResponse; |
9 | | -import org.springframework.http.*; |
10 | | -import org.springframework.stereotype.Service; |
11 | | -import org.springframework.web.client.RestTemplate; |
12 | | - |
13 | | -import java.util.List; |
14 | 21 |
|
15 | 22 | @Service |
16 | 23 | public class ModelService { |
17 | 24 |
|
18 | | - private final RestTemplate restTemplate = new RestTemplate(); |
19 | | - private final String FASTAPI_URL = "http://203.153.147.12:5050"; |
20 | | - |
21 | | - public List<NewsResponse.Result> cardNews(NewsRequest request) { |
22 | | - HttpHeaders headers = new HttpHeaders(); |
23 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
24 | | - |
25 | | - HttpEntity<NewsRequest> entity = new HttpEntity<>(request, headers); |
26 | | - |
27 | | - try { |
28 | | - ResponseEntity<NewsResponse> response = restTemplate.exchange( |
29 | | - FASTAPI_URL + "/news", |
30 | | - HttpMethod.POST, |
31 | | - entity, |
32 | | - NewsResponse.class |
33 | | - ); |
34 | | - return response.getBody().getResults(); |
35 | | - } catch (Exception e) { |
36 | | - throw new CustomException(ErrorCode.FASTAPI_COMMUNICATION_ERROR); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - public ChatAskResponse askQuestion(ChatAskRequest request) { |
41 | | - HttpHeaders headers = new HttpHeaders(); |
42 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
43 | | - |
44 | | - HttpEntity<ChatAskRequest> entity = new HttpEntity<>(request, headers); |
45 | | - |
46 | | - try { |
47 | | - ResponseEntity<ChatAskResponse> response = restTemplate.exchange( |
48 | | - FASTAPI_URL + "/chat", |
49 | | - HttpMethod.POST, |
50 | | - entity, |
51 | | - ChatAskResponse.class |
52 | | - ); |
53 | | - return response.getBody(); |
54 | | - } catch (Exception e) { |
55 | | - throw new CustomException(ErrorCode.FASTAPI_COMMUNICATION_ERROR); |
56 | | - } |
57 | | - } |
| 25 | + private final RestTemplate restTemplate = new RestTemplate(); |
| 26 | + private final String FASTAPI_URL = "http://203.153.147.12:5050"; |
| 27 | + |
| 28 | + public List<NewsResponse.Result> cardNews(NewsRequest request) { |
| 29 | + HttpHeaders headers = new HttpHeaders(); |
| 30 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 31 | + |
| 32 | + HttpEntity<NewsRequest> entity = new HttpEntity<>(request, headers); |
| 33 | + |
| 34 | + try { |
| 35 | + ResponseEntity<NewsResponse> response = restTemplate.exchange( |
| 36 | + FASTAPI_URL + "/news", |
| 37 | + HttpMethod.POST, |
| 38 | + entity, |
| 39 | + NewsResponse.class |
| 40 | + ); |
| 41 | + return response.getBody().getResults(); |
| 42 | + } catch (Exception e) { |
| 43 | + throw new CustomException(ErrorCode.FASTAPI_COMMUNICATION_ERROR); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public ChatAskResponse askQuestion(ChatAskRequest request) { |
| 48 | + HttpHeaders headers = new HttpHeaders(); |
| 49 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 50 | + |
| 51 | + HttpEntity<ChatAskRequest> entity = new HttpEntity<>(request, headers); |
| 52 | + |
| 53 | + try { |
| 54 | + ResponseEntity<ChatAskResponse> response = restTemplate.exchange( |
| 55 | + FASTAPI_URL + "/chat", |
| 56 | + HttpMethod.POST, |
| 57 | + entity, |
| 58 | + ChatAskResponse.class |
| 59 | + ); |
| 60 | + return response.getBody(); |
| 61 | + } catch (Exception e) { |
| 62 | + throw new CustomException(ErrorCode.FASTAPI_COMMUNICATION_ERROR); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public String predictCoin(String coinName) { |
| 67 | + HttpHeaders headers = new HttpHeaders(); |
| 68 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 69 | + |
| 70 | + Map<String, String> body = new HashMap<>(); |
| 71 | + body.put("coinName", coinName); |
| 72 | + HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers); |
| 73 | + |
| 74 | + try { |
| 75 | + // TODO: return response.getBody(); |
| 76 | + // ResponseEntity<String> response = restTemplate.exchange( |
| 77 | + // FASTAPI_URL + "/chat/predict", |
| 78 | + // HttpMethod.POST, |
| 79 | + // entity, |
| 80 | + // String.class |
| 81 | + // ); |
| 82 | + return coinName; |
| 83 | + } catch (Exception e) { |
| 84 | + throw new CustomException(ErrorCode.FASTAPI_COMMUNICATION_ERROR); |
| 85 | + } |
| 86 | + } |
58 | 87 | } |
0 commit comments