Set explicit timeout for http request (#15505)

* Bondhome
* chatgpt
* electroluxair
* energidataservice
* freeboxos
* gardena
* generacmobilelink
* hdpowerview
* icalendar
* juicenet
* kostalinverter
* liquidcheck
* mcd
* meater
* miele
* mercedesme
* mybmw
* myq
* ojelectronics
* plex
* radiothermostat
* renault
* semsportal
* sensibo
* tapocontrol
* tellstick
* verisure
* vizio

---------

Signed-off-by: lsiepel <leosiepel@gmail.com>
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel
2023-10-19 22:30:41 +02:00
committed by GitHub
parent c7568cb206
commit 7313415ae0
32 changed files with 135 additions and 48 deletions

View File

@@ -12,10 +12,13 @@
*/
package org.openhab.binding.chatgpt.internal;
import static org.openhab.binding.chatgpt.internal.ChatGPTBindingConstants.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -55,6 +58,7 @@ import com.google.gson.JsonObject;
@NonNullByDefault
public class ChatGPTHandler extends BaseThingHandler {
private static final int REQUEST_TIMEOUT_MS = 10_000;
private final Logger logger = LoggerFactory.getLogger(ChatGPTHandler.class);
private HttpClient httpClient;
@@ -125,8 +129,8 @@ public class ChatGPTHandler extends BaseThingHandler {
String queryJson = gson.toJson(root);
Request request = httpClient.newRequest(apiUrl).method(HttpMethod.POST)
.header("Content-Type", "application/json").header("Authorization", "Bearer " + apiKey)
.content(new StringContentProvider(queryJson));
.timeout(REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS).header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey).content(new StringContentProvider(queryJson));
logger.trace("Query '{}'", queryJson);
try {
ContentResponse response = request.send();
@@ -166,8 +170,8 @@ public class ChatGPTHandler extends BaseThingHandler {
scheduler.execute(() -> {
try {
Request request = httpClient.newRequest(modelUrl).method(HttpMethod.GET).header("Authorization",
"Bearer " + apiKey);
Request request = httpClient.newRequest(modelUrl).timeout(REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.method(HttpMethod.GET).header("Authorization", "Bearer " + apiKey);
ContentResponse response = request.send();
if (response.getStatus() == 200) {
updateStatus(ThingStatus.ONLINE);