add authentication

This commit is contained in:
wing328 2015-05-20 15:57:54 +08:00
parent deca6a0329
commit 278a653154
7 changed files with 164 additions and 55 deletions

View File

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

View File

@ -67,6 +67,30 @@ class APIClient {
$this->curl_timeout = $seconds; $this->curl_timeout = $seconds;
} }
/**
* @param array $headerParams
* @param array $queryParams
* @param array $authSettings
*/
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}}'] = Configuration::$api_key;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = Configuration::$api_key;{{/isKeyInQuery}}{{#isBasic}}$headerParams['Authorization'] = base64_encode(Configuraiton::$username.":"Configuration::$password){{/isBasic}}{{/isApiKey}}
break;
{{/authMethods}}
}
}
}
/** /**
* @param string $resourcePath path to method endpoint * @param string $resourcePath path to method endpoint
* @param string $method method to call * @param string $method method to call
@ -76,7 +100,7 @@ class APIClient {
* @return mixed * @return mixed
*/ */
public function callAPI($resourcePath, $method, $queryParams, $postData, public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) { $headerParams, $authSettings) {
$headers = array(); $headers = array();

View File

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

View File

@ -67,6 +67,35 @@ class APIClient {
$this->curl_timeout = $seconds; $this->curl_timeout = $seconds;
} }
/**
* @param array $headerParams
* @param array $queryParams
* @param array $authSettings
*/
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 auth scheme to use
switch($auth) {
case 'api_key':
$headerParams['api_key'] = Configuration::$api_key;
break;
case 'petstore_auth':
break;
}
}
}
/** /**
* @param string $resourcePath path to method endpoint * @param string $resourcePath path to method endpoint
* @param string $method method to call * @param string $method method to call
@ -76,7 +105,7 @@ class APIClient {
* @return mixed * @return mixed
*/ */
public function callAPI($resourcePath, $method, $queryParams, $postData, public function callAPI($resourcePath, $method, $queryParams, $postData,
$headerParams) { $headerParams, $authSettings) {
$headers = array(); $headers = array();
@ -142,20 +171,21 @@ class APIClient {
$response_info = curl_getinfo($curl); $response_info = curl_getinfo($curl);
// Handle the response // Handle the response
if ($response === false) { // error, likely in the client side if ($response_info['http_code'] == 0) {
throw new APIClientException("API Error ($url): ".curl_error($curl), 0, $response_info, $response); throw new APIClientException("TIMEOUT: api call to " . $url .
" took more than 5s to return", 0, $response_info, $response);
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) { } else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response); $data = json_decode($response);
if (json_last_error() > 0) { // if response is a string if (json_last_error() > 0) { // if response is a string
$data = $response; $data = $response;
} }
} else if ($response_info['http_code'] == 401) { // server returns 401 } else if ($response_info['http_code'] == 401) {
throw new APIClientException("Unauthorized API request to " . $url . throw new APIClientException("Unauthorized API request to " . $url .
": " . serialize($response), 0, $response_info, $response); ": " . serialize($response), 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) { // server returns 404 } else if ($response_info['http_code'] == 404) {
$data = null; $data = null;
} else { } else {
throw new APIClientException("Can't connect to the API: " . $url . throw new APIClientException("Can't connect to the api: " . $url .
" response code: " . " response code: " .
$response_info['http_code'], 0, $response_info, $response); $response_info['http_code'], 0, $response_info, $response);
} }

View File

@ -73,10 +73,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -125,10 +128,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -176,18 +182,20 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'array[Pet]');
'array[Pet]'); return $responseObject;
return $responseObject;
} }
/** /**
@ -233,18 +241,20 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'array[Pet]');
'array[Pet]'); return $responseObject;
return $responseObject;
} }
/** /**
@ -296,18 +306,20 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('api_key', 'petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'Pet');
'Pet'); return $responseObject;
return $responseObject;
} }
/** /**
@ -367,10 +379,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -428,10 +443,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -493,10 +511,13 @@ class PetApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array('petstore_auth');
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }

View File

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

View File

@ -73,10 +73,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -125,10 +128,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -177,10 +183,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -232,18 +241,20 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'string');
'string'); return $responseObject;
return $responseObject;
} }
/** /**
@ -285,10 +296,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -342,18 +356,20 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
if(! $response) { if(! $response) {
return null; return null;
} }
$responseObject = $this->apiClient->deserialize($response, $responseObject = $this->apiClient->deserialize($response,'User');
'User'); return $responseObject;
return $responseObject;
} }
/** /**
@ -410,10 +426,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }
@ -467,10 +486,13 @@ class UserApi {
$httpBody = $formParams; $httpBody = $formParams;
} }
// authentication setting, if any
$authSettings = array();
// make the API Call // make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method, $response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $httpBody, $queryParams, $httpBody,
$headerParams); $headerParams, $authSettings);
} }