Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for http endpoint in addition to the websocket #599

Open
wants to merge 7 commits into
base: 1.11
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/Communication/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ public function __construct($socketClient, LoggerInterface $logger = null, int $

// create socket client
if (\is_string($socketClient)) {
if (\in_array(\parse_url($socketClient, \PHP_URL_SCHEME), ['http', 'https'])) {
Copy link
Contributor

@osbre osbre May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to extract all this code into a private/protected method? something like

private function retrieveWsDebuggerUrl(string $endpoint): string

$configURL = $socketClient.'/json/version';

if (!\function_exists('curl_init')) {
throw new \Exception('curl is not available');
}

$curl = \curl_init($configURL);
\curl_setopt($curl, \CURLOPT_URL, $configURL);
\curl_setopt($curl, \CURLOPT_RETURNTRANSFER, true);
$resp = \curl_exec($curl);
\curl_close($curl);

if (false === $resp) {
throw new \Exception("Unable to request $configURL");
}

$json_resp = \json_decode($resp);
if (null === $json_resp) {
throw new \Exception("Invalid JSON response from $configURL");
}

if (!\property_exists($json_resp, 'webSocketDebuggerUrl')) {
throw new \Exception("Websocket debugger URL not found in response from $configURL");
}

$socketClient = $json_resp->webSocketDebuggerUrl;
}

$socketClient = new Wrench(new WrenchBaseClient($socketClient, 'http://127.0.0.1'), $this->logger);
} elseif (!\is_object($socketClient) && !$socketClient instanceof SocketInterface) {
throw new \InvalidArgumentException('$socketClient param should be either a SockInterface instance or a web socket uri string');
Expand Down
Loading