Skip to content

Commit 0559ef4

Browse files
committed
Updating python and python3 generation of client to type check request handlers
1 parent 66bfb06 commit 0559ef4

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

ds3-autogen-python/src/main/java/com/spectralogic/ds3autogen/python/generators/client/BaseClientGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ public class BaseClientGenerator implements ClientModelGenerator<BaseClient> {
3030
@Override
3131
public BaseClient generate(final Ds3Request ds3Request, final Ds3DocSpec docSpec) {
3232
final String commandName = toPythonCommandName(ds3Request.getName());
33+
final String requestType = removePath(ds3Request.getName());
3334
final String responseName = toResponseName(ds3Request.getName());
3435
final String documentation = toDocumentation(ds3Request.getName(), docSpec);
3536

36-
return new BaseClient(commandName, responseName, documentation);
37+
return new BaseClient(commandName, requestType, responseName, documentation);
3738
}
3839

3940
/**

ds3-autogen-python/src/main/java/com/spectralogic/ds3autogen/python/model/client/BaseClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
public class BaseClient {
1919

2020
private final String commandName;
21+
private final String requestType;
2122
private final String responseName;
2223
private final String documentation;
2324

24-
public BaseClient(final String commandName, final String responseName, final String documentation) {
25+
public BaseClient(final String commandName, final String requestType, final String responseName, final String documentation) {
2526
this.commandName = commandName;
27+
this.requestType = requestType;
2628
this.responseName = responseName;
2729
this.documentation = documentation;
2830
}
@@ -31,6 +33,10 @@ public String getCommandName() {
3133
return commandName;
3234
}
3335

36+
public String getRequestType() {
37+
return requestType;
38+
}
39+
3440
public String getResponseName() {
3541
return responseName;
3642
}

ds3-autogen-python/src/main/resources/tmpls/python/commands/client/client_class.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ class Client(object):
1010
<#list clientCommands as cmd>
1111
${cmd.documentation}
1212
def ${cmd.commandName}(self, request):
13+
if not isinstance(request, ${cmd.requestType}):
14+
raise TypeError('request for ${cmd.commandName} should be of type ${cmd.requestType} but was ' + request.__class__.__name__)
1315
return ${cmd.responseName}(self.net_client.get_response(request), request)
1416
</#list>

ds3-autogen-python/src/test/java/com.spectralogic.ds3autogen.python/utils/FunctionalTestHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ public static void hasClient(final ImmutableList<String> requestNames, final Str
210210
* Determines if the code contains the client command for the specified request
211211
*/
212212
public static boolean hasClientCommand(final String requestName, final String code) {
213+
final String commandName = camelToUnderscore(toCommandName(requestName));
213214
final Pattern search = Pattern.compile(
214-
"def " + camelToUnderscore(toCommandName(requestName)) + "\\(self, request\\):"
215-
+ "\\s+return " + toResponseName(requestName)
216-
+ "\\(self\\.net_client.get_response\\(request\\), request\\)",
215+
"def " + commandName + "\\(self, request\\):"
216+
+ "\\s+if not isinstance\\(request, " + requestName + "\\):"
217+
+ "\\s+raise TypeError\\('request for " + commandName + " should be of type " + requestName + " but was ' \\+ request\\.__class__\\.__name__\\)"
218+
+ "\\s+return " + toResponseName(requestName) + "\\(self\\.net_client.get_response\\(request\\), request\\)",
217219
Pattern.MULTILINE | Pattern.UNIX_LINES);
218220
return search.matcher(code).find();
219221
}

0 commit comments

Comments
 (0)