1+ package io .kubernetes .client ;
2+
3+ import io .kubernetes .client .ApiException ;
4+ import io .kubernetes .client .Configuration ;
5+ import io .kubernetes .client .proto .Runtime .Unknown ;
6+
7+ import com .google .common .io .ByteStreams ;
8+ import com .google .protobuf .Message ;
9+ import com .squareup .okhttp .Request ;
10+ import com .squareup .okhttp .Response ;
11+ import com .squareup .okhttp .ResponseBody ;
12+
13+ import java .io .IOException ;
14+ import java .io .InputStream ;
15+ import java .util .ArrayList ;
16+ import java .util .HashMap ;
17+
18+ public class ProtoClient {
19+ private ApiClient apiClient ;
20+ // Magic number for the beginning of proto encoded.
21+ // https://github.com/kubernetes/apimachinery/blob/master/pkg/runtime/serializer/protobuf/protobuf.go#L42
22+ private static final byte [] MAGIC = new byte [] {0x6b , 0x38 , 0x73 , 0x00 };
23+
24+ /**
25+ * Simple Attach API constructor, uses default configuration
26+ */
27+ public ProtoClient () {
28+ this (Configuration .getDefaultApiClient ());
29+ }
30+
31+ /**
32+ * Attach API Constructor
33+ * @param apiClient The api client to use.
34+ */
35+ public ProtoClient (ApiClient apiClient ) {
36+ this .apiClient = apiClient ;
37+ }
38+
39+ /**
40+ * Get the API client for these Attach operations.
41+ * @return The API client that will be used.
42+ */
43+ public ApiClient getApiClient () {
44+ return apiClient ;
45+ }
46+
47+ /**
48+ * Set the API client for subsequent Attach operations.
49+ * @param apiClient The new API client to use.
50+ */
51+ public void setApiClient (ApiClient apiClient ) {
52+ this .apiClient = apiClient ;
53+ }
54+
55+ public <T extends Message > T get (T .Builder obj , String path )
56+ throws ApiException , IOException {
57+ return (T ) request (obj , path , "GET" );
58+ }
59+
60+ public <T extends Message > T list (T .Builder listObj , String path )
61+ throws ApiException , IOException {
62+ return get (listObj , path );
63+ }
64+
65+ private Message request (Message .Builder builder , String path , String method )
66+ throws ApiException , IOException {
67+ HashMap <String , String > headers = new HashMap <String , String >();
68+ headers .put ("Content-type" , "application/vnd.kubernetes.protobuf" );
69+ headers .put ("Accept" , "application/vnd.kubernetes.protobuf" );
70+ Request request = apiClient .buildRequest (path , method , new ArrayList <Pair >(), new ArrayList <Pair >(), null , headers , new HashMap <String , Object >(), new String [0 ], null );
71+ Response resp = apiClient .getHttpClient ().newCall (request ).execute ();
72+ Unknown u = parse (resp .body ().byteStream ());
73+ return builder .mergeFrom (u .getRaw ()).build ();
74+ }
75+
76+ private Unknown parse (InputStream stream ) throws ApiException , IOException {
77+ byte [] magic = new byte [4 ];
78+ stream .read (magic );
79+ for (int i = 0 ; i < MAGIC .length ; i ++) {
80+ if (magic [i ] != MAGIC [i ]) {
81+ throw new ApiException ("Unexpected magic number: " + magic );
82+ }
83+ }
84+ return Unknown .parseFrom (stream );
85+ }
86+ }
0 commit comments