]> git.basschouten.com Git - openhab-addons.git/blob
181b777112e8477ef0a7da1608171f98ee9bdce7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tado.swagger.codegen.api.client;
14
15 import java.io.IOException;
16 import java.lang.reflect.Type;
17 import java.util.List;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.eclipse.jetty.util.ssl.SslContextFactory;
28 import org.openhab.binding.tado.swagger.codegen.api.ApiException;
29 import org.openhab.binding.tado.swagger.codegen.api.auth.Authorizer;
30 import org.openhab.binding.tado.swagger.codegen.api.model.GenericZoneCapabilities;
31 import org.openhab.binding.tado.swagger.codegen.api.model.HomeInfo;
32 import org.openhab.binding.tado.swagger.codegen.api.model.HomePresence;
33 import org.openhab.binding.tado.swagger.codegen.api.model.HomeState;
34 import org.openhab.binding.tado.swagger.codegen.api.model.MobileDevice;
35 import org.openhab.binding.tado.swagger.codegen.api.model.Overlay;
36 import org.openhab.binding.tado.swagger.codegen.api.model.OverlayTemplate;
37 import org.openhab.binding.tado.swagger.codegen.api.model.User;
38 import org.openhab.binding.tado.swagger.codegen.api.model.Zone;
39 import org.openhab.binding.tado.swagger.codegen.api.model.ZoneState;
40
41 import com.google.gson.Gson;
42 import com.google.gson.reflect.TypeToken;
43
44 /**
45  * Static imported copy of class created by Swagger Codegen
46  *
47  * @author Andrew Fiddian-Green - Initial contribution
48  */
49 public class HomeApi {
50     private static final HttpClient CLIENT = new HttpClient(new SslContextFactory());
51
52     private String baseUrl = "https://my.tado.com/api/v2";
53     private int timeout = 5000;
54
55     private Gson gson;
56     private Authorizer authorizer;
57
58     public HomeApi(Gson gson, Authorizer authorizer) {
59         this.gson = gson;
60         this.authorizer = authorizer;
61     }
62
63     public void deleteZoneOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
64
65         // verify the required parameter 'homeId' is set
66         if (homeId == null) {
67             throw new ApiException(400, "Missing the required parameter 'homeId' when calling deleteZoneOverlay");
68         }
69
70         // verify the required parameter 'zoneId' is set
71         if (zoneId == null) {
72             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling deleteZoneOverlay");
73         }
74
75         startHttpClient(CLIENT);
76
77         // create path and map variables
78         String path = "/homes/{home_id}/zones/{zone_id}/overlay"
79                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
80                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
81
82         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.DELETE).timeout(timeout,
83                 TimeUnit.MILLISECONDS);
84
85         request.accept("application/json");
86         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
87
88         if (authorizer != null) {
89             authorizer.addAuthorization(request);
90         }
91
92         ContentResponse response;
93         try {
94             response = request.send();
95         } catch (Exception e) {
96             throw new IOException(e);
97         }
98
99         int statusCode = response.getStatus();
100         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
101             throw new ApiException(response, "Operation deleteZoneOverlay failed with error " + statusCode);
102         }
103     }
104
105     public HomeState homeState(Long homeId) throws IOException, ApiException {
106
107         // verify the required parameter 'homeId' is set
108         if (homeId == null) {
109             throw new ApiException(400, "Missing the required parameter 'homeId' when calling homeState");
110         }
111
112         startHttpClient(CLIENT);
113
114         // create path and map variables
115         String path = "/homes/{home_id}/state".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
116
117         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
118                 TimeUnit.MILLISECONDS);
119
120         request.accept("application/json");
121         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
122
123         if (authorizer != null) {
124             authorizer.addAuthorization(request);
125         }
126
127         ContentResponse response;
128         try {
129             response = request.send();
130         } catch (Exception e) {
131             throw new IOException(e);
132         }
133
134         int statusCode = response.getStatus();
135         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
136             throw new ApiException(response, "Operation homeState failed with error " + statusCode);
137         }
138
139         Type returnType = new TypeToken<HomeState>() {
140         }.getType();
141         return gson.fromJson(response.getContentAsString(), returnType);
142     }
143
144     public List<MobileDevice> listMobileDevices(Long homeId) throws IOException, ApiException {
145
146         // verify the required parameter 'homeId' is set
147         if (homeId == null) {
148             throw new ApiException(400, "Missing the required parameter 'homeId' when calling listMobileDevices");
149         }
150
151         startHttpClient(CLIENT);
152
153         // create path and map variables
154         String path = "/homes/{home_id}/mobileDevices".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
155
156         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
157                 TimeUnit.MILLISECONDS);
158
159         request.accept("application/json");
160         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
161
162         if (authorizer != null) {
163             authorizer.addAuthorization(request);
164         }
165
166         ContentResponse response;
167         try {
168             response = request.send();
169         } catch (Exception e) {
170             throw new IOException(e);
171         }
172
173         int statusCode = response.getStatus();
174         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
175             throw new ApiException(response, "Operation listMobileDevices failed with error " + statusCode);
176         }
177
178         Type returnType = new TypeToken<List<MobileDevice>>() {
179         }.getType();
180         return gson.fromJson(response.getContentAsString(), returnType);
181     }
182
183     public List<Zone> listZones(Long homeId) throws IOException, ApiException {
184
185         // verify the required parameter 'homeId' is set
186         if (homeId == null) {
187             throw new ApiException(400, "Missing the required parameter 'homeId' when calling listZones");
188         }
189
190         startHttpClient(CLIENT);
191
192         // create path and map variables
193         String path = "/homes/{home_id}/zones".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
194
195         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
196                 TimeUnit.MILLISECONDS);
197
198         request.accept("application/json");
199         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
200
201         if (authorizer != null) {
202             authorizer.addAuthorization(request);
203         }
204
205         ContentResponse response;
206         try {
207             response = request.send();
208         } catch (Exception e) {
209             throw new IOException(e);
210         }
211
212         int statusCode = response.getStatus();
213         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
214             throw new ApiException(response, "Operation listZones failed with error " + statusCode);
215         }
216
217         Type returnType = new TypeToken<List<Zone>>() {
218         }.getType();
219         return gson.fromJson(response.getContentAsString(), returnType);
220     }
221
222     public HomeInfo showHome(Long homeId) throws IOException, ApiException {
223
224         // verify the required parameter 'homeId' is set
225         if (homeId == null) {
226             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showHome");
227         }
228
229         startHttpClient(CLIENT);
230
231         // create path and map variables
232         String path = "/homes/{home_id}".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
233
234         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
235                 TimeUnit.MILLISECONDS);
236
237         request.accept("application/json");
238         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
239
240         if (authorizer != null) {
241             authorizer.addAuthorization(request);
242         }
243
244         ContentResponse response;
245         try {
246             response = request.send();
247         } catch (Exception e) {
248             throw new IOException(e);
249         }
250
251         int statusCode = response.getStatus();
252         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
253             throw new ApiException(response, "Operation showHome failed with error " + statusCode);
254         }
255
256         Type returnType = new TypeToken<HomeInfo>() {
257         }.getType();
258         return gson.fromJson(response.getContentAsString(), returnType);
259     }
260
261     public User showUser() throws IOException, ApiException {
262
263         startHttpClient(CLIENT);
264
265         // create path and map variables
266         String path = "/me";
267
268         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
269                 TimeUnit.MILLISECONDS);
270
271         request.accept("application/json");
272         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
273
274         if (authorizer != null) {
275             authorizer.addAuthorization(request);
276         }
277
278         ContentResponse response;
279         try {
280             response = request.send();
281         } catch (Exception e) {
282             throw new IOException(e);
283         }
284
285         int statusCode = response.getStatus();
286         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
287             throw new ApiException(response, "Operation showUser failed with error " + statusCode);
288         }
289
290         Type returnType = new TypeToken<User>() {
291         }.getType();
292         return gson.fromJson(response.getContentAsString(), returnType);
293     }
294
295     public GenericZoneCapabilities showZoneCapabilities(Long homeId, Long zoneId) throws IOException, ApiException {
296
297         // verify the required parameter 'homeId' is set
298         if (homeId == null) {
299             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneCapabilities");
300         }
301
302         // verify the required parameter 'zoneId' is set
303         if (zoneId == null) {
304             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneCapabilities");
305         }
306
307         startHttpClient(CLIENT);
308
309         // create path and map variables
310         String path = "/homes/{home_id}/zones/{zone_id}/capabilities"
311                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
312                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
313
314         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
315                 TimeUnit.MILLISECONDS);
316
317         request.accept("application/json");
318         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
319
320         if (authorizer != null) {
321             authorizer.addAuthorization(request);
322         }
323
324         ContentResponse response;
325         try {
326             response = request.send();
327         } catch (Exception e) {
328             throw new IOException(e);
329         }
330
331         int statusCode = response.getStatus();
332         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
333             throw new ApiException(response, "Operation showZoneCapabilities failed with error " + statusCode);
334         }
335
336         Type returnType = new TypeToken<GenericZoneCapabilities>() {
337         }.getType();
338         return gson.fromJson(response.getContentAsString(), returnType);
339     }
340
341     public OverlayTemplate showZoneDefaultOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
342
343         // verify the required parameter 'homeId' is set
344         if (homeId == null) {
345             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneDefaultOverlay");
346         }
347
348         // verify the required parameter 'zoneId' is set
349         if (zoneId == null) {
350             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneDefaultOverlay");
351         }
352
353         startHttpClient(CLIENT);
354
355         // create path and map variables
356         String path = "/homes/{home_id}/zones/{zone_id}/defaultOverlay"
357                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
358                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
359
360         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
361                 TimeUnit.MILLISECONDS);
362
363         request.accept("application/json");
364         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
365
366         if (authorizer != null) {
367             authorizer.addAuthorization(request);
368         }
369
370         ContentResponse response;
371         try {
372             response = request.send();
373         } catch (Exception e) {
374             throw new IOException(e);
375         }
376
377         int statusCode = response.getStatus();
378         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
379             throw new ApiException(response, "Operation showZoneDefaultOverlay failed with error " + statusCode);
380         }
381
382         Type returnType = new TypeToken<OverlayTemplate>() {
383         }.getType();
384         return gson.fromJson(response.getContentAsString(), returnType);
385     }
386
387     public Zone showZoneDetails(Long homeId, Long zoneId) throws IOException, ApiException {
388
389         // verify the required parameter 'homeId' is set
390         if (homeId == null) {
391             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneDetails");
392         }
393
394         // verify the required parameter 'zoneId' is set
395         if (zoneId == null) {
396             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneDetails");
397         }
398
399         startHttpClient(CLIENT);
400
401         // create path and map variables
402         String path = "/homes/{home_id}/zones/{zone_id}/details"
403                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
404                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
405
406         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
407                 TimeUnit.MILLISECONDS);
408
409         request.accept("application/json");
410         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
411
412         if (authorizer != null) {
413             authorizer.addAuthorization(request);
414         }
415
416         ContentResponse response;
417         try {
418             response = request.send();
419         } catch (Exception e) {
420             throw new IOException(e);
421         }
422
423         int statusCode = response.getStatus();
424         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
425             throw new ApiException(response, "Operation showZoneDetails failed with error " + statusCode);
426         }
427
428         Type returnType = new TypeToken<Zone>() {
429         }.getType();
430         return gson.fromJson(response.getContentAsString(), returnType);
431     }
432
433     public Overlay showZoneOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
434
435         // verify the required parameter 'homeId' is set
436         if (homeId == null) {
437             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneOverlay");
438         }
439
440         // verify the required parameter 'zoneId' is set
441         if (zoneId == null) {
442             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneOverlay");
443         }
444
445         startHttpClient(CLIENT);
446
447         // create path and map variables
448         String path = "/homes/{home_id}/zones/{zone_id}/overlay"
449                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
450                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
451
452         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
453                 TimeUnit.MILLISECONDS);
454
455         request.accept("application/json");
456         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
457
458         if (authorizer != null) {
459             authorizer.addAuthorization(request);
460         }
461
462         ContentResponse response;
463         try {
464             response = request.send();
465         } catch (Exception e) {
466             throw new IOException(e);
467         }
468
469         int statusCode = response.getStatus();
470         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
471             throw new ApiException(response, "Operation showZoneOverlay failed with error " + statusCode);
472         }
473
474         Type returnType = new TypeToken<Overlay>() {
475         }.getType();
476         return gson.fromJson(response.getContentAsString(), returnType);
477     }
478
479     public ZoneState showZoneState(Long homeId, Long zoneId) throws IOException, ApiException {
480
481         // verify the required parameter 'homeId' is set
482         if (homeId == null) {
483             throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneState");
484         }
485
486         // verify the required parameter 'zoneId' is set
487         if (zoneId == null) {
488             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneState");
489         }
490
491         startHttpClient(CLIENT);
492
493         // create path and map variables
494         String path = "/homes/{home_id}/zones/{zone_id}/state".replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
495                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
496
497         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
498                 TimeUnit.MILLISECONDS);
499
500         request.accept("application/json");
501         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
502
503         if (authorizer != null) {
504             authorizer.addAuthorization(request);
505         }
506
507         ContentResponse response;
508         try {
509             response = request.send();
510         } catch (Exception e) {
511             throw new IOException(e);
512         }
513
514         int statusCode = response.getStatus();
515         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
516             throw new ApiException(response, "Operation showZoneState failed with error " + statusCode);
517         }
518
519         Type returnType = new TypeToken<ZoneState>() {
520         }.getType();
521         return gson.fromJson(response.getContentAsString(), returnType);
522     }
523
524     public void updatePresenceLock(Long homeId, HomePresence json) throws IOException, ApiException {
525
526         // verify the required parameter 'homeId' is set
527         if (homeId == null) {
528             throw new ApiException(400, "Missing the required parameter 'homeId' when calling updatePresenceLock");
529         }
530
531         // verify the required parameter 'json' is set
532         if (json == null) {
533             throw new ApiException(400, "Missing the required parameter 'json' when calling updatePresenceLock");
534         }
535
536         startHttpClient(CLIENT);
537
538         // create path and map variables
539         String path = "/homes/{home_id}/presenceLock".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
540
541         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.PUT).timeout(timeout,
542                 TimeUnit.MILLISECONDS);
543
544         request.accept("application/json");
545         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
546
547         if (authorizer != null) {
548             authorizer.addAuthorization(request);
549         }
550
551         String serializedBody = gson.toJson(json);
552         request.content(new StringContentProvider(serializedBody), "application/json");
553
554         ContentResponse response;
555         try {
556             response = request.send();
557         } catch (Exception e) {
558             throw new IOException(e);
559         }
560
561         int statusCode = response.getStatus();
562         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
563             throw new ApiException(response, "Operation updatePresenceLock failed with error " + statusCode);
564         }
565     }
566
567     public Overlay updateZoneOverlay(Long homeId, Long zoneId, Overlay json) throws IOException, ApiException {
568
569         // verify the required parameter 'homeId' is set
570         if (homeId == null) {
571             throw new ApiException(400, "Missing the required parameter 'homeId' when calling updateZoneOverlay");
572         }
573
574         // verify the required parameter 'zoneId' is set
575         if (zoneId == null) {
576             throw new ApiException(400, "Missing the required parameter 'zoneId' when calling updateZoneOverlay");
577         }
578
579         // verify the required parameter 'json' is set
580         if (json == null) {
581             throw new ApiException(400, "Missing the required parameter 'json' when calling updateZoneOverlay");
582         }
583
584         startHttpClient(CLIENT);
585
586         // create path and map variables
587         String path = "/homes/{home_id}/zones/{zone_id}/overlay"
588                 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
589                 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
590
591         Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.PUT).timeout(timeout,
592                 TimeUnit.MILLISECONDS);
593
594         request.accept("application/json");
595         request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
596
597         if (authorizer != null) {
598             authorizer.addAuthorization(request);
599         }
600
601         String serializedBody = gson.toJson(json);
602         request.content(new StringContentProvider(serializedBody), "application/json");
603
604         ContentResponse response;
605         try {
606             response = request.send();
607         } catch (Exception e) {
608             throw new IOException(e);
609         }
610
611         int statusCode = response.getStatus();
612         if (statusCode >= HttpStatus.BAD_REQUEST_400) {
613             throw new ApiException(response, "Operation updateZoneOverlay failed with error " + statusCode);
614         }
615
616         Type returnType = new TypeToken<Overlay>() {
617         }.getType();
618         return gson.fromJson(response.getContentAsString(), returnType);
619     }
620
621     private static void startHttpClient(HttpClient client) {
622         if (!client.isStarted()) {
623             try {
624                 client.start();
625             } catch (Exception e) {
626                 // nothing we can do here
627             }
628         }
629     }
630 }