Skip to content

Commit

Permalink
booking: Streamline Carrier's handling for HTTP requests
Browse files Browse the repository at this point in the history
Signed-off-by: Niels Thykier <[email protected]>
  • Loading branch information
nt-gt committed Nov 23, 2023
1 parent 4a2cce4 commit be95196
Showing 1 changed file with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,29 +466,52 @@ private void processAndEmitNotificationForStateTransition(
}
}

private ConformanceResponse return405(ConformanceRequest request, String ... allowedMethods) {
return request.createResponse(
405,
Map.of("Api-Version", List.of(apiVersion),
"Allow", List.of(String.join(",", allowedMethods))
),
new ConformanceMessageBody(
objectMapper
.createObjectNode()
.put(
"message",
"Returning 405 because the method was not supported")));
}


private ConformanceResponse return404(ConformanceRequest request) {
return request.createResponse(
404,
Map.of("Api-Version", List.of(apiVersion)),
new ConformanceMessageBody(
objectMapper
.createObjectNode()
.put(
"message",
"Returning 404 since the request did not match any known URL")));
}

@Override
public ConformanceResponse handleRequest(ConformanceRequest request) {
log.info("Carrier.handleRequest(%s)".formatted(request));
if (request.method().equals("GET")) {
return _handleGetBookingRequest(request);
} else if (request.method().equals("POST") && request.url().endsWith("/v2/bookings")) {
return _handlePostBookingRequest(request);
} else {
ConformanceResponse response =
request.createResponse(
409,
Map.of("Api-Version", List.of(apiVersion)),
new ConformanceMessageBody(
objectMapper
.createObjectNode()
.put(
"message",
"Rejecting '%s' for Booking '%s' because it is in state '%s'"
.formatted("TODO", "TODO", "TODO"))));
addOperatorLogEntry(
"Handling booking request '%s' (now in state '%s')".formatted("TODO", "TODO"));
return response;
}
var result = switch (request.method()) {
case "GET" -> _handleGetBookingRequest(request);
case "POST" -> {
var url = request.url();
if (url.endsWith("/v2/bookings") || url.endsWith("/v2/bookings/")) {
yield _handlePostBookingRequest(request);
}
yield return404(request);
}
case "PUT", "PATCH" -> throw new UnsupportedOperationException();
default -> return405(request, "GET", "POST", "PUT", "PATCH");
};
addOperatorLogEntry(
"Responded to request '%s %s' with '%d'"
.formatted(request.method(), request.url(), result.statusCode()));
return result;
}

private String lastUrlSegment(String url) {
Expand All @@ -501,15 +524,18 @@ private ConformanceResponse _handleGetBookingRequest(ConformanceRequest request)
// bookingReference can either be a CBR or CBRR.
var cbrr = cbrToCbrr.getOrDefault(bookingReference, bookingReference);
var booking = persistentMap.load(cbrr);
ConformanceResponse response =
if (booking != null) {
ConformanceResponse response =
request.createResponse(
200,
Map.of("Api-Version", List.of(apiVersion)),
new ConformanceMessageBody(booking));
addOperatorLogEntry(
200,
Map.of("Api-Version", List.of(apiVersion)),
new ConformanceMessageBody(booking));
addOperatorLogEntry(
"Responded to GET booking request '%s' (in state '%s')"
.formatted(bookingReference, booking.get("bookingStatus")));
return response;
return response;
}
return return404(request);
}

@SneakyThrows
Expand Down

0 comments on commit be95196

Please sign in to comment.