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

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);
}