-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create api implementation for couple of requests
- Loading branch information
Showing
5 changed files
with
101 additions
and
108 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/ru/nsu/fit/directors/userservice/api/AbstractApi.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ru.nsu.fit.directors.userservice.api; | ||
|
||
import brave.internal.Nullable; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.util.UriBuilder; | ||
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse; | ||
import ru.nsu.fit.directors.userservice.exception.ClientException; | ||
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public class AbstractApi implements DefaultApi { | ||
private final WebClient.Builder webClient; | ||
|
||
public AbstractApi(WebClient.Builder webClient) { | ||
this.webClient = webClient; | ||
} | ||
|
||
@Nullable | ||
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) { | ||
ParameterizedTypeReference<BaseResponse<T>> reference = new ParameterizedTypeReference<>() { | ||
}; | ||
return Optional.ofNullable(webClient.build() | ||
.get() | ||
.uri(path, params) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)) | ||
.toEntity(reference) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.map(BaseResponse::getResult) | ||
.orElse(null); | ||
} | ||
|
||
@Nullable | ||
public <T> List<T> syncListGetWithParams( | ||
Function<UriBuilder, URI> uriBuilder, | ||
ParameterizedTypeReference<BaseResponse<List<T>>> reference | ||
) { | ||
return Optional.ofNullable(webClient.build() | ||
.get() | ||
.uri(uriBuilder) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus( | ||
HttpStatusCode::is4xxClientError, | ||
response -> response.bodyToMono(BaseResponse.class) | ||
.map(resp -> new ClientException(resp.getException().getMessage())) | ||
) | ||
.onStatus( | ||
HttpStatusCode::is5xxServerError, | ||
response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new) | ||
) | ||
.toEntity(reference) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.map(BaseResponse::getResult) | ||
.orElse(null); | ||
} | ||
} |
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
47 changes: 5 additions & 42 deletions
47
src/main/java/ru/nsu/fit/directors/userservice/api/EstablishmentApi.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,52 +1,15 @@ | ||
package ru.nsu.fit.directors.userservice.api; | ||
|
||
import brave.internal.Nullable; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.util.UriBuilder; | ||
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class EstablishmentApi implements DefaultApi { | ||
private final WebClient.Builder establishmentClientBuilder; | ||
|
||
@Nullable | ||
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) { | ||
return Optional.ofNullable(establishmentClientBuilder.build() | ||
.get() | ||
.uri(path, params) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)) | ||
.toEntity(type) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.orElse(null); | ||
} | ||
public class EstablishmentApi extends AbstractApi { | ||
|
||
@Nullable | ||
public <T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, Class<T> type) { | ||
return Optional.ofNullable(establishmentClientBuilder.build() | ||
.get() | ||
.uri(uriBuilder) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)) | ||
.toEntityList(type) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.orElse(null); | ||
@Autowired | ||
public EstablishmentApi(WebClient.Builder establishmentClientBuilder) { | ||
super(establishmentClientBuilder); | ||
} | ||
|
||
} |
58 changes: 5 additions & 53 deletions
58
src/main/java/ru/nsu/fit/directors/userservice/api/OrderApi.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,61 +1,13 @@ | ||
package ru.nsu.fit.directors.userservice.api; | ||
|
||
import brave.internal.Nullable; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.util.UriBuilder; | ||
import ru.nsu.fit.directors.userservice.dto.response.BaseResponse; | ||
import ru.nsu.fit.directors.userservice.exception.ClientException; | ||
import ru.nsu.fit.directors.userservice.exception.ServerNotAvailableException; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class OrderApi implements DefaultApi { | ||
private final WebClient.Builder orderClientBuilder; | ||
|
||
@Nullable | ||
public <T> T syncGetWithParams(String path, Map<String, ?> params, Class<T> type) { | ||
return Optional.ofNullable(orderClientBuilder.build() | ||
.get() | ||
.uri(path, params) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is5xxServerError, response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new)) | ||
.toEntity(type) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.orElse(null); | ||
public class OrderApi extends AbstractApi { | ||
@Autowired | ||
public OrderApi(WebClient.Builder orderClientBuilder) { | ||
super(orderClientBuilder); | ||
} | ||
|
||
@Nullable | ||
public <T> List<T> syncListGetWithParams(Function<UriBuilder, URI> uriBuilder, Class<T> type) { | ||
return Optional.ofNullable(orderClientBuilder.build() | ||
.get() | ||
.uri(uriBuilder) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus( | ||
HttpStatusCode::is4xxClientError, | ||
response -> response.bodyToMono(BaseResponse.class).map(resp -> new ClientException(resp.getException().getMessage())) | ||
) | ||
.onStatus( | ||
HttpStatusCode::is5xxServerError, | ||
response -> response.bodyToMono(String.class).map(ServerNotAvailableException::new) | ||
) | ||
.toEntityList(type) | ||
.block()) | ||
.map(ResponseEntity::getBody) | ||
.orElse(null); | ||
} | ||
|
||
} |
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