-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminAnnouncementClient.java
More file actions
110 lines (91 loc) · 4.21 KB
/
Copy pathAdminAnnouncementClient.java
File metadata and controls
110 lines (91 loc) · 4.21 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package apu.saerok_admin.infra.announcement;
import apu.saerok_admin.infra.SaerokApiProps;
import apu.saerok_admin.infra.announcement.dto.AdminAnnouncementDetailResponse;
import apu.saerok_admin.infra.announcement.dto.AdminAnnouncementImagePresignRequest;
import apu.saerok_admin.infra.announcement.dto.AdminAnnouncementListResponse;
import apu.saerok_admin.infra.announcement.dto.AdminCreateAnnouncementRequest;
import apu.saerok_admin.infra.announcement.dto.AdminUpdateAnnouncementRequest;
import apu.saerok_admin.infra.announcement.dto.AnnouncementImagePresignResponse;
import java.net.URI;
import java.util.List;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;
@Component
public class AdminAnnouncementClient {
private static final String[] ADMIN_ANNOUNCEMENT_SEGMENTS = {"admin", "announcement"};
private final RestClient saerokRestClient;
private final String[] missingPrefixSegments;
public AdminAnnouncementClient(RestClient saerokRestClient, SaerokApiProps saerokApiProps) {
this.saerokRestClient = saerokRestClient;
List<String> missing = saerokApiProps.missingPrefixSegments();
this.missingPrefixSegments = missing.toArray(new String[0]);
}
public AdminAnnouncementListResponse listAnnouncements() {
return get(AdminAnnouncementListResponse.class);
}
public AdminAnnouncementDetailResponse getAnnouncement(Long announcementId) {
return get(AdminAnnouncementDetailResponse.class, announcementId.toString());
}
public AdminAnnouncementDetailResponse createAnnouncement(AdminCreateAnnouncementRequest request) {
return post(AdminAnnouncementDetailResponse.class, request);
}
public AdminAnnouncementDetailResponse updateAnnouncement(Long announcementId, AdminUpdateAnnouncementRequest request) {
return put(AdminAnnouncementDetailResponse.class, request, announcementId.toString());
}
public void deleteAnnouncement(Long announcementId) {
delete(announcementId.toString());
}
public AnnouncementImagePresignResponse generateImagePresignUrl(AdminAnnouncementImagePresignRequest request) {
return post(AnnouncementImagePresignResponse.class, request, "image", "presign");
}
private <T> T get(Class<T> responseType, String... segments) {
T response = saerokRestClient.get()
.uri(uriBuilder -> buildUri(uriBuilder, segments))
.retrieve()
.body(responseType);
if (response == null) {
throw new IllegalStateException("Empty response from admin announcement API");
}
return response;
}
private <T> T post(Class<T> responseType, Object body, String... segments) {
T response = saerokRestClient.post()
.uri(uriBuilder -> buildUri(uriBuilder, segments))
.body(body)
.retrieve()
.body(responseType);
if (response == null) {
throw new IllegalStateException("Empty response from admin announcement API");
}
return response;
}
private <T> T put(Class<T> responseType, Object body, String... segments) {
T response = saerokRestClient.method(HttpMethod.PUT)
.uri(uriBuilder -> buildUri(uriBuilder, segments))
.body(body)
.retrieve()
.body(responseType);
if (response == null) {
throw new IllegalStateException("Empty response from admin announcement API");
}
return response;
}
private void delete(String... segments) {
saerokRestClient.delete()
.uri(uriBuilder -> buildUri(uriBuilder, segments))
.retrieve()
.toBodilessEntity();
}
private URI buildUri(UriBuilder builder, String... segments) {
if (missingPrefixSegments.length > 0) {
builder.pathSegment(missingPrefixSegments);
}
builder.pathSegment(ADMIN_ANNOUNCEMENT_SEGMENTS);
if (segments != null && segments.length > 0) {
builder.pathSegment(segments);
}
return builder.build();
}
}