From 94d617ba04aac9fea1abb7b6226743521b08d01e Mon Sep 17 00:00:00 2001 From: materus Date: Mon, 11 Mar 2024 15:01:21 +0100 Subject: [PATCH] Move most code from controller to service --- .../ghrepo/controller/GitHubController.java | 33 +------------ .../materus/ghrepo/service/GitHubService.java | 46 ++++++++++++++++++- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/main/java/pl/materus/ghrepo/controller/GitHubController.java b/src/main/java/pl/materus/ghrepo/controller/GitHubController.java index 856fe2c..206eaae 100644 --- a/src/main/java/pl/materus/ghrepo/controller/GitHubController.java +++ b/src/main/java/pl/materus/ghrepo/controller/GitHubController.java @@ -7,7 +7,6 @@ import pl.materus.ghrepo.service.GitHubService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -44,41 +43,13 @@ public class GitHubController { @ExceptionHandler(WebClientResponseException.class) public ResponseEntity handleResponseException(WebClientResponseException ex) { - HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - - String message; - Integer statusCode = ex.getStatusCode().value(); - switch (statusCode) { - case 404: - message = "User not found"; - status = HttpStatus.NOT_FOUND; - break; - - case 403: - message = "Github API limit"; - status = HttpStatus.FORBIDDEN; - break; - - default: - message = "Unknown github webclient error"; - break; - - } - - ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode,message); - return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel); + return gitHubService.handleWebClientException(ex); } @ExceptionHandler(WrongHeaderException.class) public ResponseEntity handleResponseException(WrongHeaderException ex) { - ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(),ex.getMessage()); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - - return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel); + return gitHubService.handleWrongHeaderException(ex); } } diff --git a/src/main/java/pl/materus/ghrepo/service/GitHubService.java b/src/main/java/pl/materus/ghrepo/service/GitHubService.java index 049a63f..120316c 100644 --- a/src/main/java/pl/materus/ghrepo/service/GitHubService.java +++ b/src/main/java/pl/materus/ghrepo/service/GitHubService.java @@ -1,12 +1,19 @@ package pl.materus.ghrepo.service; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClientResponseException; +import pl.materus.ghrepo.exception.WrongHeaderException; import pl.materus.ghrepo.model.GitHubBranchModel; import pl.materus.ghrepo.model.GitHubRepositoryModel; import pl.materus.ghrepo.model.ResponseBranchModel; +import pl.materus.ghrepo.model.ResponseErrorModel; import pl.materus.ghrepo.model.ResponseRepositoryModel; import reactor.core.publisher.Flux; @@ -39,6 +46,43 @@ public class GitHubService { } + public ResponseEntity handleWebClientException(WebClientResponseException ex) { + HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + + String message; + Integer statusCode = ex.getStatusCode().value(); + switch (statusCode) { + case 404: + message = "User not found"; + status = HttpStatus.NOT_FOUND; + break; + + case 403: + message = "Github API limit"; + status = HttpStatus.FORBIDDEN; + break; + + default: + message = "Unknown github webclient error"; + break; + + } + + ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode, message); + return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel); + } + + public ResponseEntity handleWrongHeaderException(WrongHeaderException ex) { + ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + + return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel); + } + private Flux getRepoBranches(String owner, String repo) { return githubWebClient.get() .uri("/repos/{owner}/{repo}/branches", owner, repo) @@ -49,7 +93,7 @@ public class GitHubService { } private Mono createResponse(GitHubRepositoryModel repository) { - String[] ret = { repository.owner().login(),repository.name() }; + String[] ret = { repository.owner().login(), repository.name() }; return Mono.just(ret); }