Skip to content

Commit 98fab71

Browse files
sven1103KochTobi
andauthored
Enable request IDs for service API (#1040)
* Provide request ID * Check valid response construction * better method * update to null --------- Co-authored-by: KochTobi <[email protected]>
1 parent 0760bf3 commit 98fab71

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

project-management/src/main/java/life/qbic/projectmanagement/application/api/AsyncProjectService.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package life.qbic.projectmanagement.application.api;
22

3+
import static java.util.Objects.nonNull;
4+
5+
import java.util.Optional;
6+
import java.util.UUID;
37
import reactor.core.publisher.Mono;
48

59
/**
@@ -62,6 +66,24 @@ sealed interface UpdateResponseBody permits ProjectDesign {
6266

6367
}
6468

69+
/**
70+
* Cacheable requests provide a unique identifier so cache implementations can unambiguously
71+
* manage the requests.
72+
*
73+
* @since 1.9.0
74+
*/
75+
sealed interface CacheableRequest permits ProjectUpdateRequest {
76+
77+
/**
78+
* Returns an ID that is unique to the request.
79+
*
80+
* @return the id
81+
* @since 1.9.0
82+
*/
83+
String requestId();
84+
85+
}
86+
6587
/**
6688
* Container for passing information in an {@link UpdateRequestBody} or
6789
* {@link UpdateResponseBody}.
@@ -82,8 +104,17 @@ record ProjectDesign(String title, String objective) implements UpdateRequestBod
82104
* @param requestBody the information to be updated.
83105
* @since 1.9.0
84106
*/
85-
record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
107+
record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody, String id) implements
108+
CacheableRequest {
86109

110+
public ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
111+
this(projectId, requestBody, UUID.randomUUID().toString());
112+
}
113+
114+
@Override
115+
public String requestId() {
116+
return id;
117+
}
87118
}
88119

89120
/**
@@ -93,8 +124,43 @@ record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
93124
* @param responseBody the information that was updated.
94125
* @since 1.9.0
95126
*/
96-
record ProjectUpdateResponse(String projectId, UpdateResponseBody responseBody) {
127+
record ProjectUpdateResponse(String projectId, UpdateResponseBody responseBody, String requestId) {
128+
129+
public ProjectUpdateResponse {
130+
if (projectId == null) {
131+
throw new IllegalArgumentException("Project ID cannot be null");
132+
}
133+
if (projectId.isBlank()) {
134+
throw new IllegalArgumentException("Project ID cannot be blank");
135+
}
136+
if (requestId != null && requestId.isBlank()) {
137+
requestId = null;
138+
}
139+
}
97140

141+
/**
142+
* Retrieves the request id associated with this response. May be {@link Optional#empty()} but
143+
* never null.
144+
*
145+
* @return an Optional with the requestId; {@link Optional#empty()} otherwise.
146+
*/
147+
public Optional<String> retrieveRequestId() {
148+
return Optional.ofNullable(requestId);
149+
}
150+
151+
/**
152+
* Returns the requestId, can be null.
153+
*
154+
* @return Returns the requestId, if no requestId is set, returns null.
155+
*/
156+
@Override
157+
public String requestId() {
158+
return requestId;
159+
}
160+
161+
boolean hasRequestId() {
162+
return nonNull(requestId);
163+
}
98164
}
99165

100166
/**

project-management/src/main/java/life/qbic/projectmanagement/application/api/AsyncProjectServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Mono<ProjectUpdateResponse> update(@NonNull ProjectUpdateRequest request)
4040
case ProjectDesign design:
4141
return
4242
withSecurityContext(SecurityContextHolder.getContext(),
43-
() -> updateProjectDesign(projectId, design)).subscribeOn(scheduler);
43+
() -> updateProjectDesign(projectId, design, request.requestId())).subscribeOn(scheduler);
4444
default:
4545
return Mono.error(new UnknownRequestException("Invalid request body"));
4646
}
@@ -59,13 +59,13 @@ private Mono<ProjectUpdateResponse> withSecurityContext(SecurityContext sctx,
5959
}).contextWrite(rcontext);
6060
}
6161

62-
private Mono<ProjectUpdateResponse> updateProjectDesign(String projectId, ProjectDesign design) {
62+
private Mono<ProjectUpdateResponse> updateProjectDesign(String projectId, ProjectDesign design, String requestId) {
6363
return Mono.create(sink -> {
6464
try {
6565
var id = ProjectId.parse(projectId);
6666
projectService.updateTitle(id, design.title());
6767
projectService.updateObjective(id, design.objective());
68-
sink.success(new ProjectUpdateResponse(projectId, design));
68+
sink.success(new ProjectUpdateResponse(projectId, design, requestId));
6969
} catch (IllegalArgumentException e) {
7070
sink.error(new RequestFailedException("Invalid project id: " + projectId));
7171
} catch (org.springframework.security.access.AccessDeniedException e) {

0 commit comments

Comments
 (0)