-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminRoleClient.java
More file actions
128 lines (109 loc) · 4.61 KB
/
Copy pathAdminRoleClient.java
File metadata and controls
128 lines (109 loc) · 4.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package apu.saerok_admin.infra.role;
import apu.saerok_admin.infra.SaerokApiProps;
import apu.saerok_admin.infra.role.dto.AdminMyRoleResponse;
import apu.saerok_admin.infra.role.dto.AdminRoleListResponse;
import apu.saerok_admin.infra.role.dto.AdminRoleUserListResponse;
import apu.saerok_admin.infra.role.dto.AdminUserRoleResponse;
import apu.saerok_admin.infra.role.dto.AssignRoleRequest;
import apu.saerok_admin.infra.role.dto.CreateRoleRequest;
import apu.saerok_admin.infra.role.dto.RoleDetailResponse;
import apu.saerok_admin.infra.role.dto.UpdateRolePermissionsRequest;
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 AdminRoleClient {
private static final String[] ADMIN_ROLE_SEGMENTS = {"admin", "role"};
private final RestClient saerokRestClient;
private final String[] missingPrefixSegments;
public AdminRoleClient(RestClient saerokRestClient, SaerokApiProps saerokApiProps) {
this.saerokRestClient = saerokRestClient;
List<String> missing = saerokApiProps.missingPrefixSegments();
this.missingPrefixSegments = missing.toArray(new String[0]);
}
public AdminMyRoleResponse getMyRoles() {
return get(AdminMyRoleResponse.class, "me");
}
public AdminRoleUserListResponse listAdminUsers() {
return get(AdminRoleUserListResponse.class, "users");
}
public AdminRoleListResponse listRoles() {
return get(AdminRoleListResponse.class);
}
public RoleDetailResponse createRole(CreateRoleRequest request) {
return post(RoleDetailResponse.class, request);
}
public RoleDetailResponse updateRolePermissions(String roleCode, UpdateRolePermissionsRequest request) {
return put(RoleDetailResponse.class, request, roleCode, "permissions");
}
public void deleteRole(String roleCode) {
deleteVoid(roleCode);
}
public AdminUserRoleResponse grantRole(Long userId, AssignRoleRequest request) {
return post(AdminUserRoleResponse.class, request, "users", userId.toString(), "roles");
}
public AdminUserRoleResponse revokeRole(Long userId, String roleCode) {
return delete(AdminUserRoleResponse.class, "users", userId.toString(), "roles", roleCode);
}
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 role 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 role 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 role API");
}
return response;
}
private <T> T delete(Class<T> responseType, String... segments) {
T response = saerokRestClient.delete()
.uri(uriBuilder -> buildUri(uriBuilder, segments))
.retrieve()
.body(responseType);
if (response == null) {
throw new IllegalStateException("Empty response from admin role API");
}
return response;
}
private void deleteVoid(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_ROLE_SEGMENTS);
if (segments.length > 0) {
builder.pathSegment(segments);
}
return builder.build();
}
}