]> git.basschouten.com Git - openhab-addons.git/blob
4a7a7b8a06ade25d6902042c83509a521cc8cf9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.jellyfin.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.jellyfin.sdk.api.client.Response;
17 import org.jellyfin.sdk.api.client.exception.ApiClientException;
18
19 /**
20  * The {@link SyncResponse} util to consume sdk api calls.
21  *
22  * @author Miguel Álvarez - Initial contribution
23  */
24 @NonNullByDefault
25 public class SyncResponse<T> extends SyncCallback<Response<T>> {
26     public Response<T> awaitResponse() throws ApiClientException, SyncCallbackError {
27         try {
28             return awaitResult();
29         } catch (SyncCallbackError e) {
30             var cause = e.getCause();
31             if (cause instanceof ApiClientException apiClientException) {
32                 throw apiClientException;
33             }
34             throw e;
35         }
36     }
37
38     public T awaitContent() throws ApiClientException, SyncCallbackError {
39         var responseContent = awaitResponse().getContent();
40         if (responseContent == null) {
41             throw new SyncCallbackError("Missing content");
42         }
43         return responseContent;
44     }
45 }