Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable request IDs for service API #1040

Merged
merged 4 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package life.qbic.projectmanagement.application.api;

import java.util.UUID;
import reactor.core.publisher.Mono;

/**
Expand Down Expand Up @@ -62,6 +63,24 @@ sealed interface UpdateResponseBody permits ProjectDesign {

}

/**
* Cacheable requests provide a unique identifier so cache implementations can unambiguously
* manage the requests.
*
* @since 1.9.0
*/
sealed interface CacheableRequest permits ProjectUpdateRequest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't there need to be a permits statement?


/**
* Returns an ID that is unique to the request.
*
* @return the id
* @since 1.9.0
*/
String requestId();

}

/**
* Container for passing information in an {@link UpdateRequestBody} or
* {@link UpdateResponseBody}.
Expand All @@ -82,8 +101,17 @@ record ProjectDesign(String title, String objective) implements UpdateRequestBod
* @param requestBody the information to be updated.
* @since 1.9.0
*/
record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody, String id) implements
CacheableRequest {

public ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
this(projectId, requestBody, UUID.randomUUID().toString());
}

@Override
public String requestId() {
return id;
}
}

/**
Expand All @@ -93,7 +121,7 @@ record ProjectUpdateRequest(String projectId, UpdateRequestBody requestBody) {
* @param responseBody the information that was updated.
* @since 1.9.0
*/
record ProjectUpdateResponse(String projectId, UpdateResponseBody responseBody) {
record ProjectUpdateResponse(String projectId, UpdateResponseBody responseBody, String requestId) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Mono<ProjectUpdateResponse> update(@NonNull ProjectUpdateRequest request)
case ProjectDesign design:
return
withSecurityContext(SecurityContextHolder.getContext(),
() -> updateProjectDesign(projectId, design)).subscribeOn(scheduler);
() -> updateProjectDesign(projectId, design, request.requestId())).subscribeOn(scheduler);
default:
return Mono.error(new UnknownRequestException("Invalid request body"));
}
Expand All @@ -59,13 +59,13 @@ private Mono<ProjectUpdateResponse> withSecurityContext(SecurityContext sctx,
}).contextWrite(rcontext);
}

private Mono<ProjectUpdateResponse> updateProjectDesign(String projectId, ProjectDesign design) {
private Mono<ProjectUpdateResponse> updateProjectDesign(String projectId, ProjectDesign design, String requestId) {
return Mono.create(sink -> {
try {
var id = ProjectId.parse(projectId);
projectService.updateTitle(id, design.title());
projectService.updateObjective(id, design.objective());
sink.success(new ProjectUpdateResponse(projectId, design));
sink.success(new ProjectUpdateResponse(projectId, design, requestId));
} catch (IllegalArgumentException e) {
sink.error(new RequestFailedException("Invalid project id: " + projectId));
} catch (org.springframework.security.access.AccessDeniedException e) {
Expand Down
Loading