-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openchallenges): fetch data in JSON-LD format from the challenge…
… service (#2750)
- Loading branch information
1 parent
6bf1010
commit 38ce37a
Showing
18 changed files
with
1,111 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 36 additions & 3 deletions
39
...va/org/sagebionetworks/openchallenges/challenge/service/api/ChallengeApiDelegateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.