forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClient.java
232 lines (188 loc) · 6.26 KB
/
HttpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.http;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.RequestConfig;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import java.io.BufferedReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;
public interface HttpClient extends AutoCloseable {
interface Factory {
/**
* Create a builder that is customized by the {@link Config}. By default it
* will apply the common configuration {@link HttpClientUtils#applyCommonConfiguration(Config, Builder, Factory)}
*
* @param config the configuration to apply
* @return the configured {@link Builder}
*/
default HttpClient.Builder newBuilder(Config config) {
Builder builder = newBuilder();
HttpClientUtils.applyCommonConfiguration(config, builder, this);
return builder;
}
HttpClient.Builder newBuilder();
/**
* If the implementation should be considered default. If default any other implementation in the classpath will
* be used instead.
*
* @return true if default
*/
default boolean isDefault() {
return false;
}
}
interface DerivedClientBuilder {
HttpClient build();
DerivedClientBuilder readTimeout(long readTimeout, TimeUnit unit);
DerivedClientBuilder writeTimeout(long writeTimeout, TimeUnit unit);
/**
* Sets the HttpClient to be used to perform HTTP requests whose responses
* will be streamed.
*
* @return this Builder instance.
*/
DerivedClientBuilder forStreaming();
DerivedClientBuilder addOrReplaceInterceptor(String name, Interceptor interceptor);
/**
* Prevents any built-in authenticator to respond to challenges from origin server.
* <p>
* <i>OkHttp specific option.</i>
*
* @return this Builder instance.
*/
DerivedClientBuilder authenticatorNone();
/**
* Supply an {@link RequestConfig} via a {@link Config} to {@link Interceptor#withConfig(Config)}
*
* @param config
* @return this Builder instance.
*/
DerivedClientBuilder requestConfig(Config config);
}
interface Builder extends DerivedClientBuilder {
/**
* {@inheritDoc}
*/
@Override
HttpClient build();
/**
* {@inheritDoc}
*/
@Override
Builder readTimeout(long readTimeout, TimeUnit unit);
/**
* {@inheritDoc}
*/
@Override
Builder writeTimeout(long writeTimeout, TimeUnit unit);
Builder connectTimeout(long connectTimeout, TimeUnit unit);
/**
* {@inheritDoc}
*/
@Override
Builder forStreaming();
/**
* {@inheritDoc}
*/
@Override
Builder addOrReplaceInterceptor(String name, Interceptor interceptor);
/**
* {@inheritDoc}
*/
@Override
Builder authenticatorNone();
Builder sslContext(KeyManager[] keyManagers, TrustManager[] trustManagers);
Builder followAllRedirects();
Builder proxyAddress(InetSocketAddress proxyAddress);
Builder proxyAuthorization(String credentials);
Builder tlsVersions(TlsVersion... tlsVersions);
Builder preferHttp11();
}
/**
* A simplified java.util.concurrent.Flow.Subscription and a future tracking done.
* <br>
* The body should be consumed until the end or cancelled.
*/
interface AsyncBody {
/**
* Called to deliver results to the {@link BodyConsumer}
*/
void consume();
/**
* Tracks the completion of the body.
*
* @return the future
*/
CompletableFuture<Void> done();
void cancel();
}
/**
* A functional interface for consuming async result bodies
*/
@FunctionalInterface
interface BodyConsumer<T> {
void consume(T value, AsyncBody asyncBody) throws Exception;
}
@Override
void close();
/**
* Create a builder that starts with the same state as this client.
* <br>
* The client resources will be shared across derived instances.
*
* @return a new builder
*/
DerivedClientBuilder newBuilder();
/**
* Send an async request
* <br>
* A Reader or InputStream result must be closed by the caller to properly cleanup resources.
* <br>
* A Reader or InputStream result should not be consumed by the thread completing the returned future
* as that will hijack a client thread.
*
* @param <T> return type
* @param request the HttpRequest to send asynchronously
* @param type one of InputStream, Reader, String, byte[]
* @return a CompletableFuture that returns the resulting HttpResponse when complete
*/
<T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type);
/**
* Send a request and consume the lines of the response body using the same logic as {@link BufferedReader} to
* break up the lines.
*
* @param request the HttpRequest to send
* @param consumer the response body consumer
* @return the future which will be ready after the headers have been read
*/
CompletableFuture<HttpResponse<AsyncBody>> consumeLines(HttpRequest request, BodyConsumer<String> consumer);
/**
* Send a request and consume the bytes of the resulting response body
*
* @param request the HttpRequest to send
* @param consumer the response body consumer
* @return the future which will be ready after the headers have been read
*/
CompletableFuture<HttpResponse<AsyncBody>> consumeBytes(HttpRequest request, BodyConsumer<List<ByteBuffer>> consumer);
WebSocket.Builder newWebSocketBuilder();
HttpRequest.Builder newHttpRequestBuilder();
}