Skip to content

Commit b224e91

Browse files
committed
Move Watch to util package
1 parent ad22d1e commit b224e91

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

examples/src/main/java/io/kubernetes/client/examples/WatchExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
import com.google.gson.reflect.TypeToken;
1616
import io.kubernetes.client.ApiClient;
17-
import io.kubernetes.client.Watch;
1817
import io.kubernetes.client.ApiException;
1918
import io.kubernetes.client.Configuration;
2019
import io.kubernetes.client.apis.CoreV1Api;
2120
import io.kubernetes.client.models.V1Namespace;
2221
import io.kubernetes.client.util.Config;
22+
import io.kubernetes.client.util.Watch;
2323

2424
import java.io.IOException;
2525

@@ -33,7 +33,8 @@ public static void main(String[] args) throws IOException, ApiException{
3333

3434
CoreV1Api api = new CoreV1Api();
3535

36-
Watch<V1Namespace> watch = client.watch(
36+
Watch<V1Namespace> watch = Watch.createWatch(
37+
client,
3738
api.listNamespaceCall(null, null, null, null, 5, Boolean.TRUE, null, null),
3839
new TypeToken<Watch.Response<V1Namespace>>(){}.getType());
3940

kubernetes/src/main/java/io/kubernetes/client/ApiClient.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -949,26 +949,6 @@ public File prepareDownloadFile(Response response) throws IOException {
949949
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
950950
}
951951

952-
public <T> Watch<T> watch(Call call, Type watchType) throws ApiException {
953-
try {
954-
Response response = call.execute();
955-
if (!response.isSuccessful()) {
956-
String respBody = null;
957-
if (response.body() != null) {
958-
try {
959-
respBody = response.body().string();
960-
} catch (IOException e) {
961-
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
962-
}
963-
}
964-
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
965-
}
966-
return new Watch<>(this.json, response.body(), watchType);
967-
} catch (IOException e) {
968-
throw new ApiException(e);
969-
}
970-
}
971-
972952
/**
973953
* {@link #execute(Call, Type)}
974954
*

kubernetes/src/main/java/io/kubernetes/client/Watch.java renamed to util/src/main/java/io/kubernetes/client/util/Watch.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13-
package io.kubernetes.client;
13+
package io.kubernetes.client.util;
1414

1515
import com.google.gson.annotations.SerializedName;
16+
import com.squareup.okhttp.Call;
1617
import com.squareup.okhttp.ResponseBody;
18+
import io.kubernetes.client.ApiClient;
19+
import io.kubernetes.client.ApiException;
20+
import io.kubernetes.client.JSON;
1721

1822
import java.io.IOException;
1923
import java.lang.reflect.Type;
@@ -51,7 +55,38 @@ public static class Response<T> {
5155
ResponseBody response;
5256
JSON json;
5357

54-
public Watch(JSON json, ResponseBody body, Type watchType) {
58+
/**
59+
* Creates a watch on a TYPENAME (T) using an API Client and a Call object.
60+
* @param client the API client
61+
* @param call the call object returned by api.{ListOperation}Call(...)
62+
* method. Make sure watch flag is set in the call.
63+
* @param watchType The type of the WatchResponse<T>. Use something like
64+
* new TypeToken<Watch.Response<TYPENAME>>(){}.getType()
65+
* @param <T> TYPENAME.
66+
* @return Watch object on TYPENAME
67+
* @throws ApiException on IO exceptions.
68+
*/
69+
public static <T> Watch<T> createWatch(ApiClient client, Call call, Type watchType) throws ApiException {
70+
try {
71+
com.squareup.okhttp.Response response = call.execute();
72+
if (!response.isSuccessful()) {
73+
String respBody = null;
74+
if (response.body() != null) {
75+
try {
76+
respBody = response.body().string();
77+
} catch (IOException e) {
78+
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
79+
}
80+
}
81+
throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
82+
}
83+
return new Watch<>(new JSON(client), response.body(), watchType);
84+
} catch (IOException e) {
85+
throw new ApiException(e);
86+
}
87+
}
88+
89+
private Watch(JSON json, ResponseBody body, Type watchType) {
5590
this.response = body;
5691
this.watchType = watchType;
5792
this.json = json;

0 commit comments

Comments
 (0)