add file response for php

This commit is contained in:
wing328
2015-06-26 17:06:30 +08:00
parent 4c2df94ec7
commit fcd0b31d7d
22 changed files with 154 additions and 153 deletions

View File

@@ -79,7 +79,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("boolean", "bool");
typeMapping.put("date", "\\DateTime");
typeMapping.put("datetime", "\\DateTime");
typeMapping.put("file", "string");
typeMapping.put("file", "\\SplFileObject");
typeMapping.put("map", "map");
typeMapping.put("array", "array");
typeMapping.put("list", "array");

View File

@@ -90,7 +90,7 @@ class ApiClient {
* @throws \{{invokerPackage}}\ApiException on a non 2xx response
* @return mixed
*/
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) {
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType=null) {
$headers = array();
@@ -174,6 +174,11 @@ class ApiClient {
if ($response_info['http_code'] == 0) {
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
// return raw body if response is a file
if ($responseType == 'SplFileObject') {
return array($http_body, $http_header);
}
$data = json_decode($http_body);
if (json_last_error() > 0) { // if response is a string
$data = $http_body;

View File

@@ -79,8 +79,12 @@ class ObjectSerializer {
* @return string the form string
*/
public function toFormValue($value) {
if ($value instanceof SplFileObject) {
return $value->getRealPath();
} else {
return $this->toString($value);
}
}
/**
* Take value and turn it into a string suitable for inclusion in
@@ -104,7 +108,7 @@ class ObjectSerializer {
* @param string $class class name is passed as a string
* @return object an instance of $class
*/
public function deserialize($data, $class) {
public function deserialize($data, $class, $httpHeader=null) {
if (null === $data) {
$deserialized = null;
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
@@ -129,6 +133,24 @@ class ObjectSerializer {
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
settype($data, $class);
$deserialized = $data;
} elseif ($class === 'SplFileObject') {
# determine temp folder path
if (!isset(Configuration::$tempFolderPath) || '' === Configuration::$tempFolderPath) {
$tmpFolderPath = sys_get_temp_dir();
} else {
$tmpFolderPath = Configuration::tempFolderPath;
}
# determine file name
if (preg_match('/Content-Disposition: inline; filename=(.*)/i', $httpHeader, $match)) {
$filename = $tmpFolderPath.$match[1];
} else {
$filename = tempnam($tmpFolderPath, '');
}
$deserialized = new \SplFileObject($filename, "w");
$byte_written = $deserialized->fwrite($data);
error_log("[INFO] Written $byte_written to $filename. Please move the file to a proper folder for further processing and delete the temp afterwards", 3, Configuration::$debug_file);
} else {
$instance = new $class();
foreach ($instance::$swaggerTypes as $property => $type) {

View File

@@ -134,13 +134,13 @@ class {{classname}} {
{{/authMethods}}
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams{{#returnType}}, '{{returnType}}'{{/returnType}});
} catch (ApiException $e) {
switch ($e->getCode()) { {{#responses}}{{#dataType}}
case {{code}}:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}', $httpHeader);
$e->setResponseObject($data);
break;{{/dataType}}{{/responses}}
}

View File

@@ -263,4 +263,18 @@ class Configuration {
public static function setDefaultConfiguration(Configuration $config) {
self::$defaultConfiguration = $config;
}
/*
* return the report for debuggin
*/
public static function toDebugReport() {
$report = "PHP SDK ({{invokerPackage}}) Debug Report:\n";
$report .= " OS: ".php_uname()."\n";
$report .= " PHP Version: ".phpversion()."\n";
$report .= " Swagger Spec Version: {{version}}\n";
$report .= " SDK Package Version: {{version}}\n";
return $report;
}
}

View File

@@ -100,9 +100,9 @@ class {{classname}} implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -110,7 +110,7 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -171,7 +171,7 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -231,13 +231,13 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\Pet[]');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet[]');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet[]', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -302,13 +302,13 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\Pet[]');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet[]');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet[]', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -375,9 +375,6 @@ class PetApi {
$httpBody = $formParams;
}
//TODO support oauth
$apiKey = $this->apiClient->getApiKeyWithPrefix('api_key');
if (isset($apiKey)) {
$headerParams['api_key'] = $apiKey;
@@ -385,15 +382,18 @@ class PetApi {
//TODO support oauth
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\Pet');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Pet', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -473,7 +473,7 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -544,7 +544,7 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -563,7 +563,7 @@ class PetApi {
*
* @param int $pet_id ID of pet to update (required)
* @param string $additional_metadata Additional data to pass to server (required)
* @param string $file file to upload (required)
* @param \SplFileObject $file file to upload (required)
* @return void
* @throws \Swagger\Client\ApiException on non-2xx response
*/
@@ -619,7 +619,7 @@ class PetApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {

View File

@@ -109,13 +109,13 @@ class StoreApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, 'map[string,int]');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'map[string,int]');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'map[string,int]', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -178,13 +178,13 @@ class StoreApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\Order');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Order');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Order', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -253,13 +253,13 @@ class StoreApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\Order');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Order');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\Order', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -328,7 +328,7 @@ class StoreApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {

View File

@@ -107,7 +107,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -165,7 +165,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -223,7 +223,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -284,13 +284,13 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, 'string');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'string');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'string', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -348,7 +348,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -412,13 +412,13 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
$headerParams, '\Swagger\Client\Model\User');
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\User');
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\Swagger\Client\Model\User', $httpHeader);
$e->setResponseObject($data);
break;
}
@@ -492,7 +492,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {
@@ -556,7 +556,7 @@ class UserApi {
// make the API Call
try {
$response = $this->apiClient->callAPI($resourcePath, $method,
$response = $this->apiClient->callApi($resourcePath, $method,
$queryParams, $httpBody,
$headerParams);
} catch (ApiException $e) {

View File

@@ -90,7 +90,7 @@ class ApiClient {
* @throws \Swagger\Client\ApiException on a non 2xx response
* @return mixed
*/
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) {
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType=null) {
$headers = array();
@@ -174,6 +174,11 @@ class ApiClient {
if ($response_info['http_code'] == 0) {
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
// return raw body if response is a file
if ($responseType == 'SplFileObject') {
return array($http_body, $http_header);
}
$data = json_decode($http_body);
if (json_last_error() > 0) { // if response is a string
$data = $http_body;

View File

@@ -263,4 +263,18 @@ class Configuration {
public static function setDefaultConfiguration(Configuration $config) {
self::$defaultConfiguration = $config;
}
/*
* return the report for debuggin
*/
public static function toDebugReport() {
$report = "PHP SDK (Swagger\Client) Debug Report:\n";
$report .= " OS: ".php_uname()."\n";
$report .= " PHP Version: ".phpversion()."\n";
$report .= " Swagger Spec Version: 1.0.0\n";
$report .= " SDK Package Version: 1.0.0\n";
return $report;
}
}

View File

@@ -119,9 +119,9 @@ class Category implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -223,9 +223,9 @@ class Order implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -223,9 +223,9 @@ class Pet implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -119,9 +119,9 @@ class Tag implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -275,9 +275,9 @@ class User implements ArrayAccess {
public function __toString() {
if (defined('JSON_PRETTY_PRINT')) {
return json_encode($this, JSON_PRETTY_PRINT);
return json_encode(get_object_vars($this), JSON_PRETTY_PRINT);
} else {
return json_encode($this);
return json_encode(get_object_vars($this));
}
}
}

View File

@@ -79,8 +79,12 @@ class ObjectSerializer {
* @return string the form string
*/
public function toFormValue($value) {
if ($value instanceof SplFileObject) {
return $value->getRealPath();
} else {
return $this->toString($value);
}
}
/**
* Take value and turn it into a string suitable for inclusion in
@@ -104,7 +108,7 @@ class ObjectSerializer {
* @param string $class class name is passed as a string
* @return object an instance of $class
*/
public function deserialize($data, $class) {
public function deserialize($data, $class, $httpHeader=null) {
if (null === $data) {
$deserialized = null;
} elseif (substr($class, 0, 4) == 'map[') { # for associative array e.g. map[string,int]
@@ -129,6 +133,24 @@ class ObjectSerializer {
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
settype($data, $class);
$deserialized = $data;
} elseif ($class === 'SplFileObject') {
# determine temp folder path
if (!isset(Configuration::$tempFolderPath) || '' === Configuration::$tempFolderPath) {
$tmpFolderPath = sys_get_temp_dir();
} else {
$tmpFolderPath = Configuration::tempFolderPath;
}
# determine file name
if (preg_match('/Content-Disposition: inline; filename=(.*)/i', $httpHeader, $match)) {
$filename = $tmpFolderPath.$match[1];
} else {
$filename = tempnam($tmpFolderPath, '');
}
$deserialized = new \SplFileObject($filename, "w");
$byte_written = $deserialized->fwrite($data);
error_log("[INFO] Written $byte_written to $filename. Please move the file to a proper folder for further processing and delete the temp afterwards", 3, Configuration::$debug_file);
} else {
$instance = new $class();
foreach ($instance::$swaggerTypes as $property => $type) {

View File

@@ -1,5 +0,0 @@
main: Contrib
title: php-coveralls
internal: yes
todo: yes
wipeout: yes

View File

@@ -1,31 +0,0 @@
<?xml version="1.0"?>
<ruleset name="PHP">
<description>The coding standard for standard PHP application</description>
<exclude-pattern>*/img/*</exclude-pattern>
<exclude-pattern>*/images/*</exclude-pattern>
<exclude-pattern>*/less/*</exclude-pattern>
<exclude-pattern>*/css/*</exclude-pattern>
<exclude-pattern>*/js/*</exclude-pattern>
<exclude-pattern>*.html</exclude-pattern>
<exclude-pattern>*.twig</exclude-pattern>
<exclude-pattern>*.yml</exclude-pattern>
<exclude-pattern>*.xml</exclude-pattern>
<exclude-pattern>*.txt</exclude-pattern>
<exclude-pattern>*.less</exclude-pattern>
<exclude-pattern>*.css</exclude-pattern>
<exclude-pattern>*.js</exclude-pattern>
<exclude-pattern>*.jpg</exclude-pattern>
<exclude-pattern>*.jpeg</exclude-pattern>
<exclude-pattern>*.png</exclude-pattern>
<exclude-pattern>*.gif</exclude-pattern>
<!-- Include the whole PSR-2 standard -->
<rule ref="PSR2"/>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="140"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
</ruleset>

View File

@@ -1,45 +0,0 @@
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>My custom rule set that checks my code...</description>
<!-- Import the entire unused code rule set -->
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable"/>
<exclude name="LongVariable"/>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="minimum" value="2" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
<properties>
<property name="maximum" value="30" />
</properties>
</rule>
<rule ref="rulesets/codesize.xml">
<exclude name="TooManyMethods"/>
<exclude name="ExcessiveClassComplexity"/>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods">
<properties>
<property name="maxmethods" value="20" />
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<property name="maximum" value="70" />
</properties>
</rule>
</ruleset>

View File

@@ -19,7 +19,7 @@ try {
//$pet_api = new SwaggerClient\PetAPI($api_client);
$pet_api = new Swagger\Client\Api\PetAPI();
// test default header
$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903");
//$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903");
// return Pet (model)
$response = $pet_api->getPetById($petId);
// to test __toString()
@@ -28,26 +28,26 @@ try {
// add pet (post json)
$new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->id = $new_pet_id;
$new_pet->name = "PHP Unit Test";
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test");
// new tag
$tag= new Swagger\Client\Model\Tag;
$tag->id = $new_pet_id; // use the same id as pet
$tag->setId($new_pet_id); // use the same id as pet
//$tag->name = "test php tag";
// new category
$category = new Swagger\Client\Model\Category;
$category->id = 0; // use the same id as pet
$category->setId(10005); // use the same id as pet
//$category->name = "test php category";
$new_pet->tags = array($tag);
$new_pet->category = $category;
$new_pet->setTags(array($tag));
$new_pet->setCategory($category);
$pet_api = new Swagger\Client\Api\PetAPI();
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
// test upload file (exception)
$upload_response = $pet_api->uploadFile($petId, "test meta", NULL);
//$upload_response = $pet_api->uploadFile($petId, "test meta", NULL);
} catch (Swagger\Client\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";