Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor
*.cache
composer.lock
state.json
state.json
20 changes: 12 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Client

/** @var array<string, string> headers */
private array $headers = [];
private int $timeout = 15;
private int $connectTimeout = 60;
private int $timeout = 15000; // milliseconds (15 seconds)
private int $connectTimeout = 60000; // milliseconds (60 seconds)
private int $maxRedirects = 5;
private bool $allowRedirects = true;
private string $userAgent = '';
Expand All @@ -51,7 +51,7 @@ public function addHeader(string $key, string $value): self
/**
* Set the request timeout.
*
* @param int $timeout
* @param int $timeout Timeout in milliseconds
* @return self
*/
public function setTimeout(int $timeout): self
Expand Down Expand Up @@ -87,7 +87,7 @@ public function setMaxRedirects(int $maxRedirects): self
/**
* Set the connection timeout.
*
* @param int $connectTimeout
* @param int $connectTimeout Timeout in milliseconds
* @return self
*/
public function setConnectTimeout(int $connectTimeout): self
Expand Down Expand Up @@ -235,6 +235,8 @@ private function withRetries(callable $callback): mixed
* @param array<string>|array<string, mixed> $body
* @param array<string, mixed> $query
* @param ?callable $chunks Optional callback function that receives a Chunk object
* @param ?int $timeoutMs Optional request timeout in milliseconds
* @param ?int $connectTimeoutMs Optional connection timeout in milliseconds
* @return Response
*/
public function fetch(
Expand All @@ -243,6 +245,8 @@ public function fetch(
?array $body = [],
?array $query = [],
?callable $chunks = null,
?int $timeoutMs = null,
?int $connectTimeoutMs = null,
): Response {
if (!in_array($method, [self::METHOD_PATCH, self::METHOD_GET, self::METHOD_CONNECT, self::METHOD_DELETE, self::METHOD_POST, self::METHOD_HEAD, self::METHOD_OPTIONS, self::METHOD_PUT, self::METHOD_TRACE])) {
throw new Exception("Unsupported HTTP method");
Expand Down Expand Up @@ -297,8 +301,8 @@ public function fetch(
}
return strlen($data);
},
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_CONNECTTIMEOUT_MS => $connectTimeoutMs ?? $this->connectTimeout,
CURLOPT_TIMEOUT_MS => $timeoutMs ?? $this->timeout,
CURLOPT_MAXREDIRS => $this->maxRedirects,
CURLOPT_FOLLOWLOCATION => $this->allowRedirects,
CURLOPT_USERAGENT => $this->userAgent
Expand Down Expand Up @@ -339,7 +343,7 @@ public function fetch(
}

/**
* Get the request timeout.
* Get the request timeout in milliseconds.
*
* @return int
*/
Expand Down Expand Up @@ -369,7 +373,7 @@ public function getMaxRedirects(): int
}

/**
* Get the connection timeout.
* Get the connection timeout in milliseconds.
*
* @return int
*/
Expand Down