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

Add "servers" support to the operation, path in PHP API client #2072

Merged
merged 3 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 38 additions & 2 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,47 @@ use {{invokerPackage}}\ObjectSerializer;
*/
protected $headerSelector;

/**
* @var Host index
Copy link
Contributor

Choose a reason for hiding this comment

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

A 1st keyword of @var identifies the type of the property. so I think it would be @var int Host index .

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 I'll update it in the next push

*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down Expand Up @@ -538,10 +566,18 @@ use {{invokerPackage}}\ObjectSerializer;
$headers
);

{{#servers.0}}
$operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}
$operationHost = $operationHosts[$this->hostIndex];

{{/servers.0}}
$query = \GuzzleHttp\Psr7\build_query($queryParams);
return new Request(
'{{httpMethod}}',
$this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''),
{{^servers.0}}$this->config->getHost(){{/servers.0}}{{#servers.0}}$operationHost{{/servers.0}} . $resourcePath . ($query ? "?{$query}" : ''),
$headers,
$httpBody
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class AnotherFakeApi
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

How about the $host_index generates only if servers exists in pathItems?

I just wondering that the $host_index is defined but unused. 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, very good suggestion. My intention is to make the constructor consistent across different API classes.

What about updating the doc string to explain it cleary that it's optional and only needed when the endpoint has multiple servers/hosts defined?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with that. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Pushed some enhancements based on your feedback.

) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class FakeApi
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class FakeClassnameTags123Api
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
30 changes: 29 additions & 1 deletion samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class PetApi
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class StoreApi
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,47 @@ class UserApi
*/
protected $headerSelector;

/**
* @var Host index
*/
protected $hostIndex;

/**
* @param ClientInterface $client
* @param Configuration $config
* @param HeaderSelector $selector
* @param int $host_index
*/
public function __construct(
ClientInterface $client = null,
Configuration $config = null,
HeaderSelector $selector = null
HeaderSelector $selector = null,
$host_index = 0
) {
$this->client = $client ?: new Client();
$this->config = $config ?: new Configuration();
$this->headerSelector = $selector ?: new HeaderSelector();
$this->hostIndex = $host_index;
}

/**
* Set the host index
*
* @param int Host index (required)
*/
public function setHostIndex($host_index)
{
$this->hostIndex = $host_index;
}

/**
* Get the host index
*
* @return Host index
*/
public function getHostIndex()
{
return $this->hostIndex;
}

/**
Expand Down
Loading