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