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

fix(php): make requestOptions match other clients #497

Merged
merged 1 commit into from
May 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ export function createTransporter({
cacheable: baseRequestOptions?.cacheable,
timeout: baseRequestOptions?.timeout,
queryParameters: {
...baseRequestOptions?.queryParameters,
...methodOptions.queryParameters,
...baseRequestOptions?.queryParameters,
},
headers: {
Accept: 'application/json',
...baseRequestOptions?.headers,
...methodOptions.headers,
...baseRequestOptions?.headers,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class RequestOptions
{
private $headers = [];

private $query = [];
private $queryParameters = [];

private $body = [];

Expand All @@ -20,7 +20,7 @@ final class RequestOptions

public function __construct(array $options = [])
{
foreach (['headers', 'query', 'body'] as $name) {
foreach (['headers', 'queryParameters', 'body'] as $name) {
if (isset($options[$name]) && !empty($options[$name])) {
$this->{$name} = $options[$name];
}
Expand Down Expand Up @@ -120,15 +120,15 @@ public function setHeaders($headers)
*/
public function getQueryParameters()
{
return $this->query;
return $this->queryParameters;
}

/**
* @return string URL-encoded query string
*/
public function getBuiltQueryParameters()
{
return Helpers::buildQuery($this->query);
return Helpers::buildQuery($this->queryParameters);
}

/**
Expand All @@ -141,7 +141,7 @@ public function getBuiltQueryParameters()
*/
public function addQueryParameter($name, $value)
{
$this->query[$name] = $value;
$this->queryParameters[$name] = $value;

return $this;
}
Expand All @@ -156,7 +156,10 @@ public function addQueryParameter($name, $value)
*/
public function addQueryParameters($parameters)
{
$this->query = array_merge($this->query, $parameters);
$this->queryParameters = array_merge(
$this->queryParameters,
$parameters
);

return $this;
}
Expand All @@ -171,8 +174,8 @@ public function addQueryParameters($parameters)
*/
public function addDefaultQueryParameter($name, $value)
{
if (!isset($this->query[$name])) {
$this->query[$name] = $value;
if (!isset($this->queryParameters[$name])) {
$this->queryParameters[$name] = $value;
}

return $this;
Expand Down Expand Up @@ -203,7 +206,7 @@ public function addDefaultQueryParameters($queryParameters)
*/
public function setQueryParameters($queryParameters)
{
$this->query = $queryParameters;
$this->queryParameters = $queryParameters;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ final class RequestOptionsFactory
{
private $config;

private $validHeaders = ['Content-type', 'User-Agent'];

public function __construct(Configuration $config)
{
$this->config = $config;
Expand All @@ -22,21 +20,14 @@ public function __construct(Configuration $config)
*
* @return \Algolia\AlgoliaSearch\RequestOptions\RequestOptions
*/
public function create($options, $defaults = [])
public function create($options)
{
if (is_array($options)) {
$options += $defaults;
$options = $this->format($options);
$options = $this->normalize($options);

$options = new RequestOptions($options);
} elseif ($options instanceof RequestOptions) {
$defaults = $this->create($defaults);
$options->addDefaultHeaders($defaults->getHeaders());
$options->addDefaultQueryParameters(
$defaults->getQueryParameters()
);
$options->addDefaultBodyParameters($defaults->getBody());
$options = $this->create($options);
} else {
throw new \InvalidArgumentException(
'RequestOptions can only be created from array or from RequestOptions object'
Expand All @@ -46,9 +37,9 @@ public function create($options, $defaults = [])
return $options->addDefaultHeaders($this->config->getDefaultHeaders());
}

public function createBodyLess($options, $defaults = [])
public function createBodyLess($options)
{
$options = $this->create($options, $defaults);
$options = $this->create($options);

return $options->addQueryParameters($options->getBody())->setBody([]);
}
Expand All @@ -59,31 +50,24 @@ private function normalize($options)
'headers' => [
'X-Algolia-Application-Id' => $this->config->getAppId(),
'X-Algolia-API-Key' => $this->config->getAlgoliaApiKey(),
'User-Agent' => $this->config->getUserAgent() !== null
'User-Agent' =>
$this->config->getUserAgent() !== null
? $this->config->getUserAgent()
: UserAgent::get(),
'Content-Type' => 'application/json',
],
'query' => [],
'queryParameters' => [],
'body' => [],
'readTimeout' => $this->config->getReadTimeout(),
'writeTimeout' => $this->config->getWriteTimeout(),
'connectTimeout' => $this->config->getConnectTimeout(),
];

foreach ($options as $optionName => $value) {
$type = $this->getOptionType($optionName);

if (
in_array(
$type,
['readTimeout', 'writeTimeout', 'connectTimeout'],
true
)
) {
$normalized[$type] = $value;
if (is_array($value)) {
$normalized[$optionName] = $this->format($value);
} else {
$normalized[$type][$optionName] = $value;
$normalized[$optionName] = $value;
}
}

Expand All @@ -103,36 +87,6 @@ private function format($options)
return $options;
}

private function getOptionType($optionName)
{
if ($this->isValidHeaderName($optionName)) {
return 'headers';
} elseif (
in_array(
$optionName,
['connectTimeout', 'readTimeout', 'writeTimeout'],
true
)
) {
return $optionName;
}

return 'query';
}

private function isValidHeaderName($name)
{
if (preg_match('/^X-[a-zA-Z-]+/', $name)) {
return true;
}

if (in_array($name, $this->validHeaders, true)) {
return true;
}

return false;
}

public static function getAttributesToFormat()
{
return ['attributesToRetrieve', 'type'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,15 @@ public function __construct(
}
}

public function read(
$method,
$path,
$requestOptions = [],
$defaultRequestOptions = []
) {
public function read($method, $path, $requestOptions = [])
{
if ('GET' === mb_strtoupper($method)) {
$requestOptions = $this->requestOptionsFactory->createBodyLess(
$requestOptions,
$defaultRequestOptions
$requestOptions
);
} else {
$requestOptions = $this->requestOptionsFactory->create(
$requestOptions,
$defaultRequestOptions
$requestOptions
);
}

Expand All @@ -93,23 +87,16 @@ public function read(
);
}

public function write(
$method,
$path,
$data = [],
$requestOptions = [],
$defaultRequestOptions = []
) {
public function write($method, $path, $data = [], $requestOptions = [])
{
if ('DELETE' === mb_strtoupper($method)) {
$requestOptions = $this->requestOptionsFactory->createBodyLess(
$requestOptions,
$defaultRequestOptions
$requestOptions
);
$data = [];
} else {
$requestOptions = $this->requestOptionsFactory->create(
$requestOptions,
$defaultRequestOptions
$requestOptions
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@

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

public function write(
$method,
$path,
$data = [],
$requestOptions = [],
$defaultRequestOptions = []
);
public function write($method, $path, $data = [], $requestOptions = []);

public function send($method, $path, $requestOptions = [], $hosts = null);
}
22 changes: 16 additions & 6 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
* @see {{{dataType}}}
{{/isModel}}
{{/allParams}}
* @param array $requestOptions Request Options
* @param array $requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
*
* @return {{#returnType}}{{#responses}}{{#dataType}}{{#-first}}array<string, mixed>|{{{dataType}}}{{/-first}}{{/dataType}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}}
{{#isDeprecated}}
Expand Down Expand Up @@ -206,6 +206,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;

$resourcePath = '{{{path}}}';
$queryParameters = [];
$headers = [];
$httpBody = [];
{{#vendorExtensions}}{{#queryParams}}

Expand Down Expand Up @@ -257,7 +258,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
}
{{/bodyParams}}
{{#headerParams}}
$queryParameters['{{baseName}}'] = ${{paramName}};
$headers['{{baseName}}'] = ${{paramName}};
{{/headerParams}}
{{#servers.0}}
$operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}];
Expand All @@ -267,16 +268,25 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
$operationHost = $operationHosts[$this->hostIndex];

{{/servers.0}}
$requestOptions += $queryParameters;

return $this->sendRequest('{{httpMethod}}', $resourcePath, $queryParameters, $httpBody, $requestOptions);
return $this->sendRequest('{{httpMethod}}', $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions);
}

{{/operation}}

private function sendRequest($method, $resourcePath, $queryParameters, $httpBody, $requestOptions)
private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions)
{
$query = \GuzzleHttp\Psr7\Query::build($queryParameters);
if (!isset($requestOptions['headers'])) {
$requestOptions['headers'] = [];
}
if (!isset($requestOptions['queryParameters'])) {
$requestOptions['queryParameters'] = [];
}

$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(
Expand Down