Merge branch 'wing328-php_auth_setting' into develop_2.0

This commit is contained in:
Tony Tam 2015-05-20 22:46:02 -07:00
commit dd70a13c02
11 changed files with 291 additions and 80 deletions

View File

@ -221,6 +221,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
bundle.put("models", allModels);
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
bundle.put("modelPackage", config.modelPackage());
bundle.put("authMethods", config.fromSecurity(swagger.getSecurityDefinitions()));
if (swagger.getExternalDocs() != null) {
bundle.put("externalDocs", swagger.getExternalDocs());
}

View File

@ -84,6 +84,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("list", "array");
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php"));
supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php"));
supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php"));
supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php"));

View File

@ -38,13 +38,14 @@ class APIClient {
protected $user_agent = "PHP-Swagger";
/**
* @param string $host the address of the API server
* @param string $headerName a header to pass on requests
* @param string $host Base url of the API server (optional)
*/
function __construct($host, $headerName = null, $headerValue = null) {
$this->host = $host;
$this->headerName = $headerName;
$this->headerValue = $headerValue;
function __construct($host = null) {
if ($host == null) {
$this->host = '{{basePath}}';
} else {
$this->host = $host;
}
}
/**
@ -100,6 +101,47 @@ class APIClient {
$this->curl_timeout = $seconds;
}
/**
* Get API key (with prefix if set)
* @param string key name
* @return string API key with the prefix
*/
public function getApiKeyWithPrefix($apiKey) {
if (Configuration::$apiKeyPrefix[$apiKey]) {
return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey];
} else {
return Configuration::$apiKey[$apiKey];
}
}
/**
* update hearder and query param based on authentication setting
*
* @param array $headerParams header parameters (by ref)
* @param array $queryParams query parameters (by ref)
* @param array $authSettings array of authentication scheme (e.g ['api_key'])
*/
public function updateParamsForAuth(&$headerParams, &$queryParams, $authSettings)
{
if (count($authSettings) == 0)
return;
// one endpoint can have more than 1 auth settings
foreach($authSettings as $auth) {
// determine which one to use
switch($auth) {
{{#authMethods}}
case '{{name}}':
{{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}$headerParams['Authorization'] = 'Basic '.base64_encode(Configuration::$username.":".Configuration::$password);{{/isBasic}}
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
break;
{{/authMethods}}
default:
//TODO show warning about security definition not found
}
}
}
/**
* @param string $resourcePath path to method endpoint
* @param string $method method to call
@ -109,26 +151,22 @@ class APIClient {
* @return mixed
*/
public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) {
$headerParams, $authSettings) {
$headers = array();
# Allow API key from $headerParams to override default
$added_api_key = False;
# determine authentication setting
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
# construct the http header
if ($headerParams != null) {
# add default header
$headerParams = array_merge((array)self::$default_header, $headerParams);
foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val";
if ($key == $this->headerName) {
$added_api_key = True;
}
}
}
if (! $added_api_key && $this->headerName != null) {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {

View File

@ -91,18 +91,20 @@ class {{classname}} {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array({{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}});
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
{{#returnType}}if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'{{returnType}}');
return $responseObject;{{/returnType}}
$responseObject = $this->apiClient->deserialize($response,'{{returnType}}');
return $responseObject;{{/returnType}}
}
{{/operation}}
{{newline}}

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace {{invokerPackage}};
class Configuration {
public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
public static $PUT = "PUT";
public static $DELETE = "DELETE";
// authentication setting
public static $apiKey = array();
public static $apiKeyPrefix = array();
public static $username = '';
public static $password = '';
}

View File

@ -38,13 +38,14 @@ class APIClient {
protected $user_agent = "PHP-Swagger";
/**
* @param string $host the address of the API server
* @param string $headerName a header to pass on requests
* @param string $host Base url of the API server (optional)
*/
function __construct($host, $headerName = null, $headerValue = null) {
$this->host = $host;
$this->headerName = $headerName;
$this->headerValue = $headerValue;
function __construct($host = null) {
if ($host == null) {
$this->host = 'http://petstore.swagger.io/v2';
} else {
$this->host = $host;
}
}
/**
@ -100,6 +101,52 @@ class APIClient {
$this->curl_timeout = $seconds;
}
/**
* Get API key (with prefix if set)
* @param string key name
* @return string API key with the prefix
*/
public function getApiKeyWithPrefix($apiKey) {
if (Configuration::$apiKeyPrefix[$apiKey]) {
return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey];
} else {
return Configuration::$apiKey[$apiKey];
}
}
/**
* update hearder and query param based on authentication setting
*
* @param array $headerParams header parameters (by ref)
* @param array $queryParams query parameters (by ref)
* @param array $authSettings array of authentication scheme (e.g ['api_key'])
*/
public function updateParamsForAuth(&$headerParams, &$queryParams, $authSettings)
{
if (count($authSettings) == 0)
return;
// one endpoint can have more than 1 auth settings
foreach($authSettings as $auth) {
// determine which one to use
switch($auth) {
case 'api_key':
$headerParams['api_key'] = $this->getApiKeyWithPrefix('api_key');
break;
case 'petstore_auth':
//TODO support oauth
break;
default:
//TODO show warning about security definition not found
}
}
}
/**
* @param string $resourcePath path to method endpoint
* @param string $method method to call
@ -109,26 +156,22 @@ class APIClient {
* @return mixed
*/
public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) {
$headerParams, $authSettings) {
$headers = array();
# Allow API key from $headerParams to override default
$added_api_key = False;
# determine authentication setting
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
# construct the http header
if ($headerParams != null) {
# add default header
$headerParams = array_merge((array)self::$default_header, $headerParams);
foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val";
if ($key == $this->headerName) {
$added_api_key = True;
}
}
}
if (! $added_api_key && $this->headerName != null) {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {

View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace SwaggerClient;
class Configuration {
public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
public static $PUT = "PUT";
public static $DELETE = "DELETE";
// authentication setting
public static $apiKey = array();
public static $apiKeyPrefix = array();
public static $username = '';
public static $password = '';
}

View File

@ -72,10 +72,13 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -123,10 +126,13 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -173,18 +179,20 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'array[Pet]');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'array[Pet]');
return $responseObject;
}
/**
@ -229,18 +237,20 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'array[Pet]');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'array[Pet]');
return $responseObject;
}
/**
@ -291,18 +301,20 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('api_key', 'petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'Pet');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'Pet');
return $responseObject;
}
/**
@ -361,10 +373,13 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -421,10 +436,13 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -485,10 +503,13 @@ class PetApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}

View File

@ -67,18 +67,20 @@ class StoreApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array('api_key');
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'map[string,int]');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'map[string,int]');
return $responseObject;
}
/**
@ -124,18 +126,20 @@ class StoreApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'Order');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'Order');
return $responseObject;
}
/**
@ -186,18 +190,20 @@ class StoreApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'Order');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'Order');
return $responseObject;
}
/**
@ -248,10 +254,13 @@ class StoreApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}

View File

@ -72,10 +72,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -123,10 +126,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -174,10 +180,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -228,18 +237,20 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'string');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'string');
return $responseObject;
}
/**
@ -280,10 +291,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -336,18 +350,20 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'User');
return $responseObject;
$responseObject = $this->apiClient->deserialize($response,'User');
return $responseObject;
}
/**
@ -403,10 +419,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}
@ -459,10 +478,13 @@ class UserApi {
$httpBody = $formParams;
}
// authentication setting, if any
$authSettings = array();
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, $authSettings);
}

View File

@ -53,13 +53,15 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
# test deleteDefaultHeader
SwaggerClient\APIClient::deleteDefaultHeader('test2');
$this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2']));
}
// test getPetById with a Pet object (id 10005)
public function testGetPetById()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
// initialize the API client without host
$api_client = new SwaggerClient\APIClient();
SwaggerClient\Configuration::$apiKey['api_key'] = '111222333444555';
$pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new SwaggerClient\PetAPI($api_client);
// return Pet (model)