All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
uk.co.mruoc.nac.client.NaughtsAndCrossesApiClient Maven / Gradle / Ivy
package uk.co.mruoc.nac.client;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import uk.co.mruoc.nac.api.dto.ApiCreateGameRequest;
import uk.co.mruoc.nac.api.dto.ApiCreateUserRequest;
import uk.co.mruoc.nac.api.dto.ApiGame;
import uk.co.mruoc.nac.api.dto.ApiTurn;
import uk.co.mruoc.nac.api.dto.ApiUpdateUserRequest;
import uk.co.mruoc.nac.api.dto.ApiUser;
import uk.co.mruoc.nac.api.dto.ApiUserBatch;
@RequiredArgsConstructor
@Slf4j
public class NaughtsAndCrossesApiClient {
private final UriFactory uriFactory;
private final HttpEntityFactory entityFactory;
private final RestTemplate template;
public NaughtsAndCrossesApiClient(String baseUrl, String token) {
this(new UriFactory(baseUrl), new HttpEntityFactory(token), new RestTemplate());
}
public Collection getUserGroups() {
String uri = uriFactory.buildUserGroupsUri();
String[] groups = performGet(uri, String[].class);
return toCollection(groups);
}
public ApiUser createUser(ApiCreateUserRequest request) {
String uri = uriFactory.buildUsersUri();
return performPost(uri, entityFactory.buildRequest(request), ApiUser.class);
}
public ApiUser updateUser(String username, ApiUpdateUserRequest request) {
String uri = uriFactory.buildUserUri(username);
return performPut(uri, entityFactory.buildRequest(request));
}
public ApiUser getUser(String username) {
String uri = uriFactory.buildUserUri(username);
return performGet(uri, ApiUser.class);
}
public void deleteUser(String username) {
String uri = uriFactory.buildUserUri(username);
performDelete(uri);
}
public ApiUserBatch uploadUserBatch(Resource resource) {
return performPost(
uriFactory.buildUserBatchesUri(), entityFactory.buildRequest(resource), ApiUserBatch.class);
}
public ApiUserBatch getUserBatch(String id) {
return performGet(uriFactory.buildUserBatchUri(id), ApiUserBatch.class);
}
public Collection getAllUserBatches() {
ApiUserBatch[] batches = performGet(uriFactory.buildUserBatchesUri(), ApiUserBatch[].class);
return toCollection(batches);
}
public Collection getAllCandidatePlayerUsernames() {
String[] usernames =
performGet(uriFactory.buildGetCandidatePlayerUsernamesUri(), String[].class);
return toCollection(usernames);
}
public void deleteAllUserBatches() {
performDelete(uriFactory.buildUserBatchesUri());
}
public void synchronizeExternalUsers() {
String uri = uriFactory.buildExternalUserSynchronizationsUri();
performPost(uri, entityFactory.buildRequest());
}
public Collection getAllUsers() {
ApiUser[] users = performGet(uriFactory.buildUsersUri(), ApiUser[].class);
return toCollection(users);
}
public Collection getAllGames() {
ApiGame[] games = performGet(uriFactory.buildGamesUri(), ApiGame[].class);
return toCollection(games);
}
public ApiGame createGame(ApiCreateGameRequest request) {
return performPostGame(uriFactory.buildGamesUri(), entityFactory.buildRequest(request));
}
public ApiGame takeTurn(long gameId, ApiTurn turn) {
HttpEntity request = entityFactory.buildRequest(turn);
return performPostGame(uriFactory.buildTakeTurnUri(gameId), request);
}
public ApiGame getGame(long gameId) {
return performGetGame(uriFactory.buildGameUri(gameId));
}
public ApiGame getMinimalGame(long gameId) {
return performGetGame(uriFactory.buildGetMinimalGameUri(gameId));
}
public void deleteAllGames() {
performDelete(uriFactory.buildGamesUri());
}
public void deleteGame(long id) {
performDelete(uriFactory.buildGameUri(id));
}
public void resetIds() {
performDelete(uriFactory.buildIdsUri());
}
private ApiGame performGetGame(String uri) {
return performGet(uri, ApiGame.class);
}
private T performGet(String uri, Class responseType) {
ResponseEntity response =
template.exchange(uri, HttpMethod.GET, entityFactory.buildRequest(), responseType);
return toBodyIfNotNull(response);
}
private void performPost(String uri, HttpEntity> request) {
template.exchange(uri, HttpMethod.POST, request, Void.class);
}
private ApiGame performPostGame(String uri, HttpEntity> request) {
return performPost(uri, request, ApiGame.class);
}
private T performPost(String uri, HttpEntity> request, Class responseType) {
return performUpdate(uri, HttpMethod.POST, request, responseType);
}
private ApiUser performPut(String uri, HttpEntity> request) {
return performUpdate(uri, HttpMethod.PUT, request, ApiUser.class);
}
private T performUpdate(
String uri, HttpMethod method, HttpEntity> request, Class responseType) {
try {
ResponseEntity response = template.exchange(uri, method, request, responseType);
return toBodyIfNotNull(response);
} catch (HttpClientErrorException e) {
throw new NaughtsAndCrossesApiClientException(e.getMessage());
}
}
private void performDelete(String uri) {
template.exchange(uri, HttpMethod.DELETE, entityFactory.buildRequest(), Void.class);
}
private static T toBodyIfNotNull(HttpEntity response) {
return Optional.ofNullable(response)
.map(HttpEntity::getBody)
.orElseThrow(
() -> new NaughtsAndCrossesApiClientException("null response body returned from api"));
}
private static Collection toCollection(T[] array) {
return Optional.ofNullable(array).map(List::of).orElse(Collections.emptyList());
}
}