|
| 1 | +package ru.practicum.shareit.client; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.springframework.http.HttpEntity; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpMethod; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.lang.Nullable; |
| 12 | +import org.springframework.web.client.HttpStatusCodeException; |
| 13 | +import org.springframework.web.client.RestTemplate; |
| 14 | + |
| 15 | +public class BaseClient { |
| 16 | + protected final RestTemplate rest; |
| 17 | + |
| 18 | + public BaseClient(RestTemplate rest) { |
| 19 | + this.rest = rest; |
| 20 | + } |
| 21 | + |
| 22 | + protected ResponseEntity<Object> get(String path) { |
| 23 | + return get(path, null, null); |
| 24 | + } |
| 25 | + |
| 26 | + protected ResponseEntity<Object> get(String path, long userId) { |
| 27 | + return get(path, userId, null); |
| 28 | + } |
| 29 | + |
| 30 | + protected ResponseEntity<Object> get(String path, Long userId, @Nullable Map<String, Object> parameters) { |
| 31 | + return makeAndSendRequest(HttpMethod.GET, path, userId, parameters, null); |
| 32 | + } |
| 33 | + |
| 34 | + protected <T> ResponseEntity<Object> post(String path, T body) { |
| 35 | + return post(path, null, null, body); |
| 36 | + } |
| 37 | + |
| 38 | + protected <T> ResponseEntity<Object> post(String path, long userId, T body) { |
| 39 | + return post(path, userId, null, body); |
| 40 | + } |
| 41 | + |
| 42 | + protected <T> ResponseEntity<Object> post(String path, Long userId, @Nullable Map<String, Object> parameters, T body) { |
| 43 | + return makeAndSendRequest(HttpMethod.POST, path, userId, parameters, body); |
| 44 | + } |
| 45 | + |
| 46 | + protected <T> ResponseEntity<Object> put(String path, long userId, T body) { |
| 47 | + return put(path, userId, null, body); |
| 48 | + } |
| 49 | + |
| 50 | + protected <T> ResponseEntity<Object> put(String path, long userId, @Nullable Map<String, Object> parameters, T body) { |
| 51 | + return makeAndSendRequest(HttpMethod.PUT, path, userId, parameters, body); |
| 52 | + } |
| 53 | + |
| 54 | + protected <T> ResponseEntity<Object> patch(String path, T body) { |
| 55 | + return patch(path, null, null, body); |
| 56 | + } |
| 57 | + |
| 58 | + protected <T> ResponseEntity<Object> patch(String path, long userId) { |
| 59 | + return patch(path, userId, null, null); |
| 60 | + } |
| 61 | + |
| 62 | + protected <T> ResponseEntity<Object> patch(String path, long userId, T body) { |
| 63 | + return patch(path, userId, null, body); |
| 64 | + } |
| 65 | + |
| 66 | + protected <T> ResponseEntity<Object> patch(String path, Long userId, @Nullable Map<String, Object> parameters, T body) { |
| 67 | + return makeAndSendRequest(HttpMethod.PATCH, path, userId, parameters, body); |
| 68 | + } |
| 69 | + |
| 70 | + protected ResponseEntity<Object> delete(String path) { |
| 71 | + return delete(path, null, null); |
| 72 | + } |
| 73 | + |
| 74 | + protected ResponseEntity<Object> delete(String path, long userId) { |
| 75 | + return delete(path, userId, null); |
| 76 | + } |
| 77 | + |
| 78 | + protected ResponseEntity<Object> delete(String path, Long userId, @Nullable Map<String, Object> parameters) { |
| 79 | + return makeAndSendRequest(HttpMethod.DELETE, path, userId, parameters, null); |
| 80 | + } |
| 81 | + |
| 82 | + private <T> ResponseEntity<Object> makeAndSendRequest(HttpMethod method, String path, Long userId, @Nullable Map<String, Object> parameters, @Nullable T body) { |
| 83 | + HttpEntity<T> requestEntity = new HttpEntity<>(body, defaultHeaders(userId)); |
| 84 | + |
| 85 | + ResponseEntity<Object> shareitServerResponse; |
| 86 | + try { |
| 87 | + if (parameters != null) { |
| 88 | + shareitServerResponse = rest.exchange(path, method, requestEntity, Object.class, parameters); |
| 89 | + } else { |
| 90 | + shareitServerResponse = rest.exchange(path, method, requestEntity, Object.class); |
| 91 | + } |
| 92 | + } catch (HttpStatusCodeException e) { |
| 93 | + return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsByteArray()); |
| 94 | + } |
| 95 | + return prepareGatewayResponse(shareitServerResponse); |
| 96 | + } |
| 97 | + |
| 98 | + private HttpHeaders defaultHeaders(Long userId) { |
| 99 | + HttpHeaders headers = new HttpHeaders(); |
| 100 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 101 | + headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 102 | + if (userId != null) { |
| 103 | + headers.set("X-Sharer-User-Id", String.valueOf(userId)); |
| 104 | + } |
| 105 | + return headers; |
| 106 | + } |
| 107 | + |
| 108 | + private static ResponseEntity<Object> prepareGatewayResponse(ResponseEntity<Object> response) { |
| 109 | + if (response.getStatusCode().is2xxSuccessful()) { |
| 110 | + return response; |
| 111 | + } |
| 112 | + |
| 113 | + ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(response.getStatusCode()); |
| 114 | + |
| 115 | + if (response.hasBody()) { |
| 116 | + return responseBuilder.body(response.getBody()); |
| 117 | + } |
| 118 | + |
| 119 | + return responseBuilder.build(); |
| 120 | + } |
| 121 | +} |
0 commit comments