Skip to content

Commit

Permalink
add php
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed May 20, 2022
1 parent b98e6e0 commit 7a8718c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
44 changes: 21 additions & 23 deletions clients/algoliasearch-client-php/lib/RetryStrategy/ApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,24 @@ public function __construct(
}
}

public function read($method, $path, $requestOptions = [])
{
if ('GET' === mb_strtoupper($method)) {
public function sendRequest(
$method,
$path,
$data = [],
$requestOptions = [],
$useReadTransporter = false
) {
/**
* Some POST methods in the Algolia REST API uses the `read` transporter.
* This information is defined at the spec level.
*/
$isRead = $useReadTransporter || 'GET' === mb_strtoupper($method);

if ($isRead) {
$requestOptions = $this->requestOptionsFactory->createBodyLess(
$requestOptions
);
} else {
$requestOptions = $this->requestOptionsFactory->create(
$requestOptions
);
}

return $this->request(
$method,
$path,
$requestOptions,
$this->clusterHosts->read(),
$requestOptions->getReadTimeout()
);
}

public function write($method, $path, $data = [], $requestOptions = [])
{
if ('DELETE' === mb_strtoupper($method)) {
} elseif ('DELETE' === mb_strtoupper($method)) {
$requestOptions = $this->requestOptionsFactory->createBodyLess(
$requestOptions
);
Expand All @@ -104,8 +98,12 @@ public function write($method, $path, $data = [], $requestOptions = [])
$method,
$path,
$requestOptions,
$this->clusterHosts->write(),
$requestOptions->getWriteTimeout(),
$isRead
? $this->clusterHosts->read()
: $this->clusterHosts->write(),
$isRead
? $requestOptions->getReadTimeout()
: $requestOptions->getWriteTimeout(),
$data
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

interface ApiWrapperInterface
{
public function read($method, $path, $requestOptions = []);

public function write($method, $path, $data = [], $requestOptions = []);
public function sendRequest(
$method,
$path,
$data = [],
$requestOptions = [],
$useReadTransporter = false
);

public function send($method, $path, $requestOptions = [], $hosts = null);
}
4 changes: 2 additions & 2 deletions scripts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export async function playground({
break;
case 'php':
await run(
`cd playground/php && \
`cd clients/algoliasearch-client-php/ && \
composer update && \
composer dump-autoload && \
cd src && \
cd ../../playground/php/src && \
php8 ${client}.php`,
{ verbose }
);
Expand Down
29 changes: 9 additions & 20 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
public function __construct(ApiWrapperInterface $apiWrapper, {{configClassname}} $config)
{
$this->config = $config;
$this->api = $apiWrapper;
}

Expand Down Expand Up @@ -261,12 +260,12 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;

{{/servers.0}}

return $this->sendRequest('{{httpMethod}}', $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions);
return $this->sendRequest('{{httpMethod}}', $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, {{#vendorExtensions.x-use-read-transporter}}true{{/vendorExtensions.x-use-read-transporter}});
}

{{/operation}}

private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions)
private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, $useReadTransporter = false)
{
if (!isset($requestOptions['headers'])) {
$requestOptions['headers'] = [];
Expand All @@ -277,25 +276,15 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;

$requestOptions['headers'] = array_merge($headers, $requestOptions['headers']);
$requestOptions['queryParameters'] = array_merge($queryParameters, $requestOptions['queryParameters']);

$query = \GuzzleHttp\Psr7\Query::build($requestOptions['queryParameters']);

if($method == 'GET') {
$request = $this->api->read(
$method,
$resourcePath . ($query ? "?{$query}" : ''),
$requestOptions
);
} else {
$request = $this->api->write(
$method,
$resourcePath . ($query ? "?{$query}" : ''),
$httpBody,
$requestOptions
);
}

return $request;
return $this->api->sendRequest(
$method,
$resourcePath . ($query ? "?{$query}" : ''),
$httpBody,
$requestOptions,
$useReadTransporter
);
}
}
{{/operations}}

0 comments on commit 7a8718c

Please sign in to comment.