Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Update WatermarkerClient.php
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Dec 6, 2024
1 parent afb539f commit 3581784
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/WatermarkerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function apply(
// sending the request
$response = $this->client->sendRequest(
$this->requestFactory
->createRequest("POST", $this->getConvertEndpointUri())
->createRequest("POST", $this->getEndpointUri('/apply'))
->withHeader(
"Content-Type",
"multipart/form-data; boundary={$multipartStreamBuilder->getBoundary()}"
Expand Down Expand Up @@ -145,16 +145,49 @@ public function saveStreamToFile(StreamInterface $stream, string $path, string $
}

/**
* Returns the convert endpoint URI.
* Returns an endpoint URI.
*
* @param string $endpoint
* @return string
*/
private function getConvertEndpointUri(): string
private function getEndpointUri(string $endpoint): string
{
$url = $this->baseUrl;
if (!str_ends_with($url, '/')) {
$url .= '/';
if (str_ends_with($url, '/')) {
$url = substr($url, 0, -1);
}
if (str_starts_with($endpoint, '/')) {
$endpoint = substr($endpoint, 1);
}

return "$url/$endpoint";
}

/**
* Health check to verify the service is running.
*
* @return bool Health check response, expected to be "ok".
*/
public function checkServiceHealth(): bool
{
try {
$response = $this->client->sendRequest(
$this->requestFactory->createRequest(
"GET",
$this->getEndpointUri("/health")
)
);

// The response status code should be 200
if ($response->getStatusCode() !== 200) {
return false;
}

// The response body should be {"status":"up"}
$responseBody = json_decode((string)$response->getBody(), true);
return isset($responseBody['status']) && $responseBody['status'] === 'up';
} catch (ClientExceptionInterface) {
return false;
}
return "{$url}apply";
}
}

0 comments on commit 3581784

Please sign in to comment.