Skip to content

Commit

Permalink
feat(openchallenges): fetch data in JSON-LD format from the challenge…
Browse files Browse the repository at this point in the history
… service (#2750)
  • Loading branch information
tschaffter authored Aug 1, 2024
1 parent 6bf1010 commit 38ce37a
Show file tree
Hide file tree
Showing 18 changed files with 1,111 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/Cha
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengeDirectionDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengeDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengeIncentiveDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengeJsonLdDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengePlatformDirectionDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengePlatformDto.java
src/main/java/org/sagebionetworks/openchallenges/challenge/service/model/dto/ChallengePlatformSearchQueryDto.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.validation.constraints.*;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.BasicErrorDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeJsonLdDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeSearchQueryDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengesPageDto;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -50,8 +51,8 @@ default ChallengeApiDelegate getDelegate() {
mediaType = "application/json",
schema = @Schema(implementation = ChallengeDto.class)),
@Content(
mediaType = "application/problem+json",
schema = @Schema(implementation = ChallengeDto.class))
mediaType = "application/ld+json",
schema = @Schema(implementation = ChallengeJsonLdDto.class)),
}),
@ApiResponse(
responseCode = "404",
Expand All @@ -60,6 +61,9 @@ default ChallengeApiDelegate getDelegate() {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = BasicErrorDto.class)),
@Content(
mediaType = "application/ld+json",
schema = @Schema(implementation = BasicErrorDto.class)),
@Content(
mediaType = "application/problem+json",
schema = @Schema(implementation = BasicErrorDto.class))
Expand All @@ -71,6 +75,9 @@ default ChallengeApiDelegate getDelegate() {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = BasicErrorDto.class)),
@Content(
mediaType = "application/ld+json",
schema = @Schema(implementation = BasicErrorDto.class)),
@Content(
mediaType = "application/problem+json",
schema = @Schema(implementation = BasicErrorDto.class))
Expand All @@ -79,8 +86,8 @@ default ChallengeApiDelegate getDelegate() {
@RequestMapping(
method = RequestMethod.GET,
value = "/challenges/{challengeId}",
produces = {"application/json", "application/problem+json"})
default ResponseEntity<ChallengeDto> getChallenge(
produces = {"application/json", "application/ld+json", "application/problem+json"})
default ResponseEntity<?> getChallenge(
@Parameter(
name = "challengeId",
description = "The unique identifier of the challenge.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Optional;
import javax.annotation.Generated;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeSearchQueryDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengesPageDto;
import org.springframework.http.HttpStatus;
Expand All @@ -29,7 +28,7 @@ default Optional<NativeWebRequest> getRequest() {
* or The request cannot be fulfilled due to an unexpected server error (status code 500)
* @see ChallengeApi#getChallenge
*/
default ResponseEntity<ChallengeDto> getChallenge(Long challengeId) {
default ResponseEntity<?> getChallenge(Long challengeId) {
getRequest()
.ifPresent(
request -> {
Expand All @@ -40,6 +39,12 @@ default ResponseEntity<ChallengeDto> getChallenge(Long challengeId) {
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
if (mediaType.isCompatibleWith(MediaType.valueOf("application/ld+json"))) {
String exampleString =
"Custom MIME type example not yet supported: application/ld+json";
ApiUtil.setExampleResponse(request, "application/ld+json", exampleString);
break;
}
if (mediaType.isCompatibleWith(MediaType.valueOf("application/problem+json"))) {
String exampleString =
"Custom MIME type example not yet supported: application/problem+json";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
package org.sagebionetworks.openchallenges.challenge.service.api;

import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeDto;
import java.util.List;
import java.util.Optional;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengeSearchQueryDto;
import org.sagebionetworks.openchallenges.challenge.service.model.dto.ChallengesPageDto;
import org.sagebionetworks.openchallenges.challenge.service.service.ChallengeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.NativeWebRequest;

@Component
public class ChallengeApiDelegateImpl implements ChallengeApiDelegate {

private static final Logger LOGGER = LoggerFactory.getLogger(ChallengeApiDelegateImpl.class);

private static final MediaType APPLICATION_LD_JSON = MediaType.valueOf("application/ld+json");
private static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");

private final ChallengeService challengeService;

public ChallengeApiDelegateImpl(ChallengeService challengeService) {
private final NativeWebRequest request;

public ChallengeApiDelegateImpl(ChallengeService challengeService, NativeWebRequest request) {
this.challengeService = challengeService;
this.request = request;
}

@Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(request);
}

@Override
public ResponseEntity<ChallengeDto> getChallenge(Long challengeId) {
public ResponseEntity<?> getChallenge(Long challengeId) {
for (MediaType mediaType : getAcceptedMediaTypes(getRequest())) {
if (mediaType.isCompatibleWith(APPLICATION_LD_JSON)) {
return ResponseEntity.ok(challengeService.getChallengeJsonLd(challengeId));
}
if (mediaType.isCompatibleWith(APPLICATION_JSON)) {
return ResponseEntity.ok(challengeService.getChallenge(challengeId));
}
}
// TODO return an error object if this API does not support any of the accepted types
return ResponseEntity.ok(challengeService.getChallenge(challengeId));
}

@Override
public ResponseEntity<ChallengesPageDto> listChallenges(ChallengeSearchQueryDto query) {
return ResponseEntity.ok(challengeService.listChallenges(query));
}

public List<MediaType> getAcceptedMediaTypes(Optional<NativeWebRequest> requestOpt) {
return requestOpt
.map(request -> MediaType.parseMediaTypes(request.getHeader("Accept")))
.orElseGet(List::of);
}
}
Loading

0 comments on commit 38ce37a

Please sign in to comment.