-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAgitClient.java
More file actions
56 lines (47 loc) · 2.23 KB
/
AgitClient.java
File metadata and controls
56 lines (47 loc) · 2.23 KB
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
package clap.server.adapter.outbound.api.agit;
import clap.server.adapter.outbound.api.data.PushNotificationTemplate;
import clap.server.adapter.outbound.persistense.entity.notification.constant.NotificationType;
import clap.server.application.port.outbound.webhook.SendAgitPort;
import clap.server.common.annotation.architecture.ExternalApiAdapter;
import clap.server.domain.model.task.Task;
import clap.server.exception.AdapterException;
import clap.server.exception.code.NotificationErrorCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@ExternalApiAdapter
@RequiredArgsConstructor
public class AgitClient implements SendAgitPort {
@Value("${webhook.agit.url}")
private String AGIT_WEBHOOK_URL;
private final AgitTemplateBuilder agitTemplateBuilder;
private final ObjectMapper objectMapper;
@Override
public Long sendAgit(PushNotificationTemplate request, Task task, String taskDetailUrl) {
HttpEntity<String> entity = agitTemplateBuilder.createAgitEntity(request, task, taskDetailUrl);
RestTemplate restTemplate = new RestTemplate();
if (request.notificationType() == NotificationType.TASK_REQUESTED) {
ResponseEntity<String> responseEntity = restTemplate.exchange(
AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class);
return getAgitPostId(responseEntity);
}
else {
restTemplate.exchange(AGIT_WEBHOOK_URL, HttpMethod.POST, entity, String.class);
return null;
}
}
private Long getAgitPostId(ResponseEntity<String> responseEntity) {
try {
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
return jsonNode.get("id").asLong();
} catch (JsonProcessingException e) {
throw new AdapterException(NotificationErrorCode.AGIT_SEND_FAILED);
}
}
}