16
16
17
17
package org .springframework .boot .web .client ;
18
18
19
+ import java .util .function .Predicate ;
20
+
21
+ import org .springframework .boot .selector .Selectable ;
22
+ import org .springframework .boot .selector .Selector ;
19
23
import org .springframework .web .client .RestClient ;
24
+ import org .springframework .web .client .RestClient .Builder ;
20
25
21
26
/**
22
27
* Callback interface that can be used to customize a
23
28
* {@link org.springframework.web.client.RestClient.Builder RestClient.Builder}.
24
29
*
25
30
* @author Arjen Poutsma
31
+ * @author Phillip Webb
26
32
* @since 3.2.0
27
33
*/
28
34
@ FunctionalInterface
29
- public interface RestClientCustomizer {
35
+ public interface RestClientCustomizer extends Selector < RestClientCustomizer > {
30
36
31
37
/**
32
38
* Callback to customize a {@link org.springframework.web.client.RestClient.Builder
@@ -35,4 +41,65 @@ public interface RestClientCustomizer {
35
41
*/
36
42
void customize (RestClient .Builder restClientBuilder );
37
43
44
+ /**
45
+ * Callback to customize a {@link org.springframework.web.client.RestClient.Builder
46
+ * RestClient.Builder} instance for a selected {@link Selectable}.
47
+ * @param restClientBuilder the client builder to customize
48
+ * @param selected the {@link #selects(Selectable) selected} selectable
49
+ */
50
+ default void customize (RestClient .Builder restClientBuilder , Selectable selected ) {
51
+ customize (restClientBuilder );
52
+ }
53
+
54
+ @ Override
55
+ default RestClientCustomizer onlyWhen (Predicate <Selectable > predicate ) {
56
+ return new ForSelected () {
57
+
58
+ @ Override
59
+ public boolean selects (Selectable selectable ) {
60
+ return (selectable != null ) && RestClientCustomizer .this .selects (selectable )
61
+ && predicate .test (selectable );
62
+ }
63
+
64
+ @ Override
65
+ public void customize (RestClient .Builder restClientBuilder , Selectable selectable ) {
66
+ RestClientCustomizer .this .customize (restClientBuilder , selectable );
67
+ }
68
+
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Helper method that can be used to create a {@link RestClientCustomizer} from a
74
+ * lambda for method chaining.
75
+ * @param customizer the source customizer
76
+ * @return the customizer
77
+ */
78
+ static RestClientCustomizer of (RestClientCustomizer customizer ) {
79
+ return customizer ;
80
+ }
81
+
82
+ /**
83
+ * Helper method that can be used to create a {@link RestClientCustomizer} from a
84
+ * lambda for method chaining.
85
+ * @param customizer the source customizer
86
+ * @return the customizer
87
+ */
88
+ static RestClientCustomizer of (ForSelected customizer ) {
89
+ return customizer ;
90
+ }
91
+
92
+ /**
93
+ * {@link RestClientCustomizer} that can be used when the selected {@link Selectable}
94
+ * is needed during customization`.
95
+ */
96
+ interface ForSelected extends RestClientCustomizer {
97
+
98
+ @ Override
99
+ default void customize (Builder restClientBuilder ) {
100
+ customize (restClientBuilder , Selectable .blank ());
101
+ }
102
+
103
+ }
104
+
38
105
}
0 commit comments