Add multiple servers support to PHP client (#1964)

* add multiple server support

* update php samples

* update wording

* make variables optional
This commit is contained in:
William Cheng
2019-01-24 11:32:03 +08:00
committed by GitHub
parent 69323aec48
commit 33b7547da3
4 changed files with 277 additions and 0 deletions

View File

@@ -419,4 +419,82 @@ class Configuration
return $keyWithPrefix;
}
/**
* Returns an array of host settings
*
* @return an array of host settings
*/
public function getHostSettings()
{
return array(
{{#servers}}
array(
"url" => "{{{url}}}",
"description" => "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
"variables" => array(
{{/-first}}
"{{{name}}}" => array(
"description" => "{{{description}}}{{^description}}No description provided{{/description}}",
"default_value" => "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
"enum_values" => array(
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/enumValues}}
){{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/variables}}
){{^-last}},{{/-last}}
{{/servers}}
);
}
/**
* Returns URL based on the index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value (optional)
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables = null)
{
if (null === $variables) {
$variables = array();
}
$hosts = $this->getHostSettings();
// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts));
}
$host = $hosts[$index];
$url = $host["url"];
// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}
return $url;
}
}

View File

@@ -426,4 +426,59 @@ class Configuration
return $keyWithPrefix;
}
/**
* Returns an array of host settings
*
* @return an array of host settings
*/
public function getHostSettings()
{
return array(
array(
"url" => "http://petstore.swagger.io:80/v2",
"description" => "No description provided",
)
);
}
/**
* Returns URL based on the index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value (optional)
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables = null)
{
if (null === $variables) {
$variables = array();
}
$hosts = $this->getHostSettings();
// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts));
}
$host = $hosts[$index];
$url = $host["url"];
// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}
return $url;
}
}

View File

@@ -426,4 +426,92 @@ class Configuration
return $keyWithPrefix;
}
/**
* Returns an array of host settings
*
* @return an array of host settings
*/
public function getHostSettings()
{
return array(
array(
"url" => "http://{server}.swagger.io:{port}/v2",
"description" => "petstore server",
"variables" => array(
"server" => array(
"description" => "No description provided",
"default_value" => "petstore",
"enum_values" => array(
"petstore",
"qa-petstore",
"dev-petstore"
)
),
"port" => array(
"description" => "No description provided",
"default_value" => "80",
"enum_values" => array(
"80",
"8080"
)
)
)
),
array(
"url" => "https://localhost:8080/{version}",
"description" => "The local server",
"variables" => array(
"version" => array(
"description" => "No description provided",
"default_value" => "v2",
"enum_values" => array(
"v1",
"v2"
)
)
)
)
);
}
/**
* Returns URL based on the index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value (optional)
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables = null)
{
if (null === $variables) {
$variables = array();
}
$hosts = $this->getHostSettings();
// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts));
}
$host = $hosts[$index];
$url = $host["url"];
// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}
return $url;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace OpenAPI\Client;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
class ConfigurationTest extends TestCase
{
/**
* Test server settings
*/
public function testMultipleServers()
{
$config = new Configuration();
$servers = $config->getHostSettings();
$this->assertCount(2, $servers);
$this->assertSame("http://{server}.swagger.io:{port}/v2", $servers[0]["url"]);
$this->assertSame("petstore", $servers[0]["variables"]["server"]["default_value"]);
$this->assertSame("80", $servers[0]["variables"]["port"]["default_value"]);
$this->assertSame(array("80", "8080"), $servers[0]["variables"]["port"]["enum_values"]);
}
/**
* Test server settings
*/
public function testServerUrl()
{
$config = new Configuration();
// default value
$url = $config->getHostFromSettings(0);
$this->assertSame("http://petstore.swagger.io:80/v2", $url);
// using a variable
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore"));
$this->assertSame("http://dev-petstore.swagger.io:80/v2", $url);
// using 2 variables
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8080"));
$this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url);
}
/**
* Test host settings with invalid vaues
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The variable `port` in the host URL has invalid value 8. Must be 80,8080
*/
public function testHostUrlWithInvalidValues()
{
// using 2 variables with invalid values
$config = new Configuration();
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8"));
$this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url);
}
}