24
24
import co .elastic .clients .transport .Endpoint ;
25
25
import org .apache .http .client .utils .URLEncodedUtils ;
26
26
27
+ import javax .annotation .Nullable ;
27
28
import java .util .Collections ;
28
29
import java .util .Map ;
29
30
import java .util .function .Function ;
30
31
31
32
public class EndpointBase <RequestT , ResponseT > implements Endpoint <RequestT , ResponseT , ErrorResponse > {
32
33
33
34
private static final Function <?, Map <String , String >> EMPTY_MAP = x -> Collections .emptyMap ();
35
+ private static final Function <?, Object > RETURN_NULL = x -> null ;
36
+ private static final Function <?, ?> RETURN_SELF = x -> x ;
34
37
35
38
/**
36
39
* Returns a function that always returns an empty String to String map. Useful to avoid creating lots of
@@ -41,27 +44,44 @@ public static <T> Function<T, Map<String, String>> emptyMap() {
41
44
return (Function <T , Map <String , String >>) EMPTY_MAP ;
42
45
}
43
46
47
+ /**
48
+ * Returns a function that always returns {@code null}.
49
+ */
50
+ @ SuppressWarnings ("unchecked" )
51
+ static <T , U > Function <T , U > returnNull () {
52
+ return (Function <T , U >) RETURN_NULL ;
53
+ }
54
+
55
+ /**
56
+ * Returns a function that always returns its parameter. It's similar to {@code Function.identity()} with the difference
57
+ * that the input and output generic parameters are different, making it suitable for use in a wider range of use cases.
58
+ */
59
+ @ SuppressWarnings ("unchecked" )
60
+ static <T , U > Function <T , U > returnSelf () {
61
+ return (Function <T , U >) RETURN_SELF ;
62
+ }
63
+
44
64
protected final String id ;
45
65
protected final Function <RequestT , String > method ;
46
66
protected final Function <RequestT , String > requestUrl ;
47
67
protected final Function <RequestT , Map <String , String >> queryParameters ;
48
68
protected final Function <RequestT , Map <String , String >> headers ;
49
- protected final boolean hasRequestBody ;
69
+ protected final Function < RequestT , Object > body ;
50
70
51
71
public EndpointBase (
52
72
String id ,
53
73
Function <RequestT , String > method ,
54
74
Function <RequestT , String > requestUrl ,
55
75
Function <RequestT , Map <String , String >> queryParameters ,
56
76
Function <RequestT , Map <String , String >> headers ,
57
- boolean hasRequestBody
77
+ Function < RequestT , Object > body
58
78
) {
59
79
this .id = id ;
60
80
this .method = method ;
61
81
this .requestUrl = requestUrl ;
62
82
this .queryParameters = queryParameters ;
63
83
this .headers = headers ;
64
- this .hasRequestBody = hasRequestBody ;
84
+ this .body = body ;
65
85
}
66
86
67
87
@ Override
@@ -89,9 +109,10 @@ public Map<String, String> headers(RequestT request) {
89
109
return this .headers .apply (request );
90
110
}
91
111
112
+ @ Nullable
92
113
@ Override
93
- public boolean hasRequestBody ( ) {
94
- return this .hasRequestBody ;
114
+ public Object body ( RequestT request ) {
115
+ return this .body . apply ( request ) ;
95
116
}
96
117
97
118
// ES-specific
@@ -114,7 +135,7 @@ public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeseria
114
135
requestUrl ,
115
136
queryParameters ,
116
137
headers ,
117
- hasRequestBody ,
138
+ body ,
118
139
newResponseParser
119
140
);
120
141
}
0 commit comments