Skip to content

Commit a19a8d7

Browse files
committed
Merge pull request #6 from mrjf/master
list serialization to CSV, generic header param handling (for auth_token), cleaner php codegen path
2 parents 50a35f6 + 3747077 commit a19a8d7

File tree

5 files changed

+62
-39
lines changed

5 files changed

+62
-39
lines changed

conf/php/structure/APIClient.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
5050

5151
$headers = array();
5252
$headers[] = "Content-type: application/json";
53-
$headers[] = "api_key: " . $this->apiKey;
5453

54+
# Allow API key from $headerParams to override default
55+
$added_api_key = False;
5556
if ($headerParams != null) {
5657
foreach ($headerParams as $key => $val) {
5758
$headers[] = "$key: $val";
59+
if ($key == 'api_key') {
60+
$added_api_key = True;
61+
}
5862
}
5963
}
60-
64+
if (! $added_api_key) {
65+
$headers[] = "api_key: " . $this->apiKey;
66+
}
67+
6168
if (is_object($postData) or is_array($postData)) {
6269
$postData = json_encode($postData);
6370
}
@@ -117,13 +124,17 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
117124

118125

119126
/**
120-
* Serialize the object to a string of JSON
121-
*
122-
* @param object $object an object to be serialized to JSON
123-
* @return string the serialized JSON
127+
* Take value and turn it into a string suitable for inclusion in
128+
* the path or the header
129+
* @param object $object an object to be serialized to a string
130+
* @return string the serialized object
124131
*/
125-
public static function serialize($object) {
126-
return json_encode($object);
132+
public static function toPathValue($object) {
133+
if (is_array($object)) {
134+
return implode(',', $object);
135+
} else {
136+
return $object;
137+
}
127138
}
128139

129140

conf/php/templates/ResourceObject.st

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,41 @@ $if(!method.responseVoid)$
5151
\$queryParams = array();
5252
\$headerParams = array();
5353

54-
$if(method.authToken)$
55-
if(\$authToken == null || strlen(\$authToken) == 0) {
56-
throw new Exception('missing authToken');
57-
}
58-
\$headerParams['auth_token'] = \$authToken;
59-
$endif$
60-
61-
6254
$if(!method.inputModel)$
6355
$method.queryParameters:{ argument |
64-
\$queryParams['$argument.name$'] = \$$argument.name$;
56+
if(\$$argument.name$ != null) {
57+
\$queryParams['$argument.name$'] = \$this->apiClient->toPathValue(\$$argument.name$);
58+
}
6559
}$
6660
$method.pathParameters:{ argument |
6761
if(\$$argument.name$ != null) {
6862
\$resourcePath = str_replace("{$argument.name$}", \$$argument.name$, \$resourcePath);
6963
}
7064
}$
65+
$method.headerParameters:{ argument |
66+
if(\$$argument.name$ != null) {
67+
\$headerParams['$argument.name$'] = \$this->apiClient->toPathValue(\$$argument.name$);
68+
}
69+
}$
7170
$endif$
7271
$if(method.inputModel)$
7372
$method.queryParameters:{ argument |
7473
if(\$$argument.inputModelClassArgument$ != null && \$$argument.inputModelClassArgument$->$argument.name$ != null) {
75-
\$queryParams["$argument.name$"] = \$$argument.inputModelClassArgument$->$argument.name$;
74+
\$queryParams["$argument.name$"] = \$this->apiClient->toPathValue(\$$argument.inputModelClassArgument$->$argument.name$);
7675
}
7776
}$
7877
$method.pathParameters:{ argument |
7978
if(\$$argument.inputModelClassArgument$ != null && \$$argument.inputModelClassArgument$->$argument.name$ != null) {
8079
\$resourcePath = str_replace("{$argument.name$}", \$$argument.inputModelClassArgument$->$argument.name$, \$resourcePath);
8180
}
8281
}$
82+
$method.headerParameters:{ argument |
83+
if(\$$argument.inputModelClassArgument$ != null && \$$argument.inputModelClassArgument$->$argument.name$ != null) {
84+
\$headerParams['$argument.name$'] = \$this->apiClient->toPathValue(\$$argument.inputModelClassArgument$->$argument.name$);
85+
}
86+
}$
8387
$endif$
88+
8489
//make the API Call
8590
$if(method.postObject)$
8691
\$response = \$this->apiClient->callAPI(\$resourcePath, \$method, \$queryParams, \$postData, \$headerParams);

conf/python/structure/APIClient.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ def callAPI(self, resourcePath, method, queryParams, postData,
7171

7272
return data
7373

74-
def serialize(self, obj):
75-
"""
74+
def toPathValue(self, obj):
75+
"""Serialize a list to a CSV string, if necessary.
7676
Args:
7777
obj -- data object to be serialized
7878
Returns:
7979
string -- json serialization of object
8080
"""
81-
return json.dumps(obj)
81+
if type(obj) == list:
82+
return ','.join(obj)
83+
else:
84+
return obj
8285

8386
def deserialize(self, obj, objClass):
8487
"""Derialize a JSON string into an object.

conf/python/templates/ResourceObject.st

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,36 @@ $methods:{ method |
5151

5252
queryParams = {}
5353
headerParams = {}
54-
$if(method.authToken)$
55-
if not authToken:
56-
raise Exception('missing authToken')
57-
headerParams['auth_token'] = authToken
58-
$endif$
5954

6055
$if(!method.inputModel)$
6156
$method.queryParameters:{ argument |
62-
queryParams['$argument.name$'] = $argument.name$
63-
}$
57+
queryParams['$argument.name$'] = self.apiClient.toPathValue($argument.name$)
58+
}$
6459

6560
$method.pathParameters:{ argument |
6661
if $argument.name$ != None:
6762
resourcePath = resourcePath.replace('{$argument.name$}', $argument.name$)
68-
63+
}$
64+
$method.headerParameters:{ argument |
65+
if $argument.name$ != None:
66+
headerParams['$argument.name$'] = self.apiClient.toPathValue($argument.name$)
6967
}$
7068
$endif$
7169
$if(method.inputModel)$
7270
$method.queryParameters:{ argument |
7371
if $argument.inputModelClassArgument$ != None and $argument.inputModelClassArgument$.$argument.name$ != None:
74-
queryParams['$argument.name$'] = $argument.inputModelClassArgument$.$argument.name$
72+
queryParams['$argument.name$'] = self.apiClient.toPathValue($argument.inputModelClassArgument$.$argument.name$)
7573

7674
}$
7775
$method.pathParameters:{ argument |
7876
if $argument.inputModelClassArgument$ != None and $argument.inputModelClassArgument$.$argument.name$ != None:
79-
resourcePath = resourcePath.replace('{$argument.name$}', $argument.inputModelClassArgument$.$argument.name$)
77+
resourcePath = resourcePath.replace('{$argument.name$}', $argument.inputModelClassArgument$.$argument.name$)
78+
}$
79+
$method.headerParameters:{ argument |
80+
if $argument.inputModelClassArgument$ != None and $argument.inputModelClassArgument$.$argument.name$ != None:
81+
headerParams['$argument.name$'] = self.apiClient.toPathValue($argument.inputModelClassArgument$.$argument.name$)
8082
}$
83+
8184
$endif$
8285

8386
# Make the API Call

src/main/java/com/wordnik/swagger/codegen/config/php/PHPLibCodeGen.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
* Time: 11:00 PM
3131
*/
3232
public class PHPLibCodeGen extends LibraryCodeGenerator {
33-
String outputBasePath;
3433

3534
public static void main(String[] args) {
3635
if(args.length < 1){
@@ -50,19 +49,21 @@ public static void main(String[] args) {
5049
String packageName = args[2];
5150
String libraryHome = args[3];
5251
if(libraryHome.endsWith("/")){
53-
libraryHome = libraryHome.substring(0, libraryHome.length()-1) + packageName;
52+
libraryHome = libraryHome.substring(0, libraryHome.length()-1);
5453
}
5554
String modelPackageName = packageName+".model";
5655
String apiPackageName = packageName+".api";
57-
String classOutputDir = libraryHome + "/src/main/php/" + packageName.replace(".","/");
58-
PHPLibCodeGen codeGenerator = new PHPLibCodeGen(apiServerURL, apiKey, modelPackageName, apiPackageName, classOutputDir, libraryHome);
56+
String classOutputDir = libraryHome + packageName.replace(".","/");
57+
PHPLibCodeGen codeGenerator = new PHPLibCodeGen(apiServerURL, apiKey, modelPackageName,
58+
apiPackageName, classOutputDir, libraryHome);
5959
codeGenerator.generateCode();
6060
}
61+
6162
}
6263

63-
public PHPLibCodeGen(String apiServerURL, String apiKey, String modelPackageName, String apiPackageName, String classOutputDir, String libraryHome){
64+
public PHPLibCodeGen(String apiServerURL, String apiKey, String modelPackageName, String apiPackageName,
65+
String classOutputDir, String libraryHome){
6466
super(apiServerURL, apiKey, modelPackageName, apiPackageName, classOutputDir, libraryHome);
65-
this.outputBasePath = classOutputDir;
6667
this.setDataTypeMappingProvider(new PHPDataTypeMappingProvider());
6768
this.setNameGenerator(new CamelCaseNamingPolicyProvider());
6869
}
@@ -86,8 +87,8 @@ protected LanguageConfiguration initializeLangConfig(LanguageConfiguration PHPCo
8687
FileUtil.createOutputDirectories(PHPConfiguration.getResourceClassLocation(), PHPConfiguration.getClassFileExtension());
8788
FileUtil.clearFolder(PHPConfiguration.getModelClassLocation());
8889
FileUtil.clearFolder(PHPConfiguration.getResourceClassLocation());
89-
90-
FileUtil.copyDirectory(new File(PHPConfiguration.getStructureLocation()), new File(PHPConfiguration.getLibraryHome() + "/src/main/php"));
90+
FileUtil.copyDirectory(new File(PHPConfiguration.getStructureLocation()), new File(PHPConfiguration.getResourceClassLocation()));
9191
return PHPConfiguration;
9292
}
93+
9394
}

0 commit comments

Comments
 (0)