Skip to content

Commit

Permalink
[python-nextgen] better sample code (OpenAPITools#15248)
Browse files Browse the repository at this point in the history
* better python-nextgen sample code

* remove future import

* fix signing doc

* better test

* use hasHttpBearerMethods instead
  • Loading branch information
wing328 authored Apr 19, 2023
1 parent e3fdac0 commit 0176957
Show file tree
Hide file tree
Showing 28 changed files with 325 additions and 880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ public String getTypeDeclaration(Schema p) {
* @param pydantic pydantic imports
* @param datetimeImports datetime imports
* @param modelImports model imports
* @param exampleImports example imports
* @param classname class name
* @return pydantic type
*
Expand All @@ -423,6 +424,7 @@ private String getPydanticType(CodegenParameter cp,
Set<String> pydanticImports,
Set<String> datetimeImports,
Set<String> modelImports,
Set<String> exampleImports,
String classname) {
if (cp == null) {
// if codegen parameter (e.g. map/dict of undefined type) is null, default to string
Expand All @@ -444,12 +446,12 @@ private String getPydanticType(CodegenParameter cp,
}
pydanticImports.add("conlist");
return String.format(Locale.ROOT, "conlist(%s%s)",
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, classname),
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, classname),
constraints);
} else if (cp.isMap) {
typingImports.add("Dict");
return String.format(Locale.ROOT, "Dict[str, %s]",
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, classname));
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, classname));
} else if (cp.isString || cp.isBinary || cp.isByteArray) {
if (cp.hasValidation) {
List<String> fieldCustomization = new ArrayList<>();
Expand Down Expand Up @@ -643,14 +645,15 @@ private String getPydanticType(CodegenParameter cp,
// add model prefix
hasModelsToImport = true;
modelImports.add(cp.dataType);
exampleImports.add(cp.dataType);
return cp.dataType;
} else if (cp.getContent() != null) {
LinkedHashMap<String, CodegenMediaType> contents = cp.getContent();
for (String key : contents.keySet()) {
CodegenMediaType cmt = contents.get(key);
// TODO process the first one only at the moment
if (cmt != null)
return getPydanticType(cmt.getSchema(), typingImports, pydanticImports, datetimeImports, modelImports, classname);
return getPydanticType(cmt.getSchema(), typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, classname);
}
throw new RuntimeException("Error! Failed to process getPydanticType when getting the content: " + cp);
} else {
Expand All @@ -666,6 +669,7 @@ private String getPydanticType(CodegenParameter cp,
* @param pydantic pydantic imports
* @param datetimeImports datetime imports
* @param modelImports model imports
* @param exampleImports example imports
* @param classname class name
* @return pydantic type
*
Expand All @@ -675,6 +679,7 @@ private String getPydanticType(CodegenProperty cp,
Set<String> pydanticImports,
Set<String> datetimeImports,
Set<String> modelImports,
Set<String> exampleImports,
String classname) {
if (cp == null) {
// if codegen property (e.g. map/dict of undefined type) is null, default to string
Expand Down Expand Up @@ -715,11 +720,11 @@ private String getPydanticType(CodegenProperty cp,
pydanticImports.add("conlist");
typingImports.add("List"); // for return type
return String.format(Locale.ROOT, "conlist(%s%s)",
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, classname),
getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, classname),
constraints);
} else if (cp.isMap) {
typingImports.add("Dict");
return String.format(Locale.ROOT, "Dict[str, %s]", getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, classname));
return String.format(Locale.ROOT, "Dict[str, %s]", getPydanticType(cp.items, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, classname));
} else if (cp.isString) {
if (cp.hasValidation) {
List<String> fieldCustomization = new ArrayList<>();
Expand Down Expand Up @@ -917,6 +922,7 @@ private String getPydanticType(CodegenProperty cp,
// for parameter model, import directly
hasModelsToImport = true;
modelImports.add(cp.dataType);
exampleImports.add(cp.dataType);
} else {
if (circularImports.containsKey(cp.dataType)) {
if (circularImports.get(cp.dataType).contains(classname)) {
Expand All @@ -926,6 +932,7 @@ private String getPydanticType(CodegenProperty cp,
// not circular import, so ok to import it
hasModelsToImport = true;
modelImports.add(cp.dataType);
exampleImports.add(cp.dataType);
}
} else {
LOGGER.error("Failed to look up {} from the imports (map of set) of models.", cp.dataType);
Expand All @@ -948,10 +955,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
OperationMap objectMap = objs.getOperations();
List<CodegenOperation> operations = objectMap.getOperation();
for (CodegenOperation operation : operations) {

TreeSet<String> exampleImports = new TreeSet<>(); // import for each operation to be show in sample code
List<CodegenParameter> params = operation.allParams;

for (CodegenParameter param : params) {
String typing = getPydanticType(param, typingImports, pydanticImports, datetimeImports, modelImports, null);
String typing = getPydanticType(param, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, null);
List<String> fields = new ArrayList<>();
String firstField = "";

Expand Down Expand Up @@ -1003,9 +1011,18 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
// update typing import for operation return type
if (!StringUtils.isEmpty(operation.returnType)) {
String typing = getPydanticType(operation.returnProperty, typingImports,
new TreeSet<>() /* skip pydantic import for return type */, datetimeImports, modelImports, null);
new TreeSet<>() /* skip pydantic import for return type */, datetimeImports, modelImports, exampleImports, null);
}

// add import for code samples
// import models one by one
if (!exampleImports.isEmpty()) {
List<String> imports = new ArrayList<>();
for (String exampleImport : exampleImports) {
imports.add("from " + packageName + ".models." + underscore(exampleImport) + " import " + exampleImport);
}
operation.vendorExtensions.put("x-py-example-import", imports);
}
}

List<Map<String, String>> newImports = new ArrayList<>();
Expand Down Expand Up @@ -1040,21 +1057,6 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}
}

/* TODO
// need models import
if (hasModelsToImport) {
Map<String, String> item = new HashMap<>();
item.put("import", String.format(Locale.ROOT, "from %s import models", packageName));
newImports.add(item);
}
// models import
if (hasModelsToImport) {
Map<String, String> item = new HashMap<>();
item.put("import", String.format(Locale.ROOT, "from %s import %s", modelPackage, StringUtils.join(modelImports, ", ")));
newImports.add(item);
}*/

// reset imports with newImports
objs.setImports(newImports);
return objs;
Expand Down Expand Up @@ -1185,6 +1187,7 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
TreeSet<String> modelImports = new TreeSet<>();

for (ModelMap m : objs.getModels()) {
TreeSet<String> exampleImports = new TreeSet<>();
List<String> readOnlyFields = new ArrayList<>();
hasModelsToImport = false;
int property_count = 1;
Expand Down Expand Up @@ -1229,7 +1232,7 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {

//loop through properties/schemas to set up typing, pydantic
for (CodegenProperty cp : codegenProperties) {
String typing = getPydanticType(cp, typingImports, pydanticImports, datetimeImports, modelImports, model.classname);
String typing = getPydanticType(cp, typingImports, pydanticImports, datetimeImports, modelImports, exampleImports, model.classname);
List<String> fields = new ArrayList<>();
String firstField = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ from {{packageName}}.exceptions import ApiValueError
from {{packageName}}.exceptions import ApiKeyError
from {{packageName}}.exceptions import ApiAttributeError
from {{packageName}}.exceptions import ApiException
{{#hasHttpSignatureMethods}}
from {{packageName}}.signing import HttpSigningConfiguration
{{/hasHttpSignatureMethods}}

# import models into sdk package
{{#models}}
{{#model}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ Method | HTTP request | Description
{{#isOAuth}}
* OAuth Authentication ({{name}}):
{{/isOAuth }}
{{> api_doc_example }}
{{/authMethods}}
{{/hasAuthMethods}}
{{^hasAuthMethods}}
{{> api_doc_example }}
{{/hasAuthMethods}}

### Parameters
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
Name | Type | Description | Notes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
```python
from __future__ import print_function
import time
import os
import {{{packageName}}}
{{#vendorExtensions.x-py-example-import}}
{{{.}}}
{{/vendorExtensions.x-py-example-import}}
from {{{packageName}}}.rest import ApiException
from pprint import pprint

{{> python_doc_auth_partial}}
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with {{{packageName}}}.ApiClient(configuration) as api_client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ configuration = {{{packageName}}}.Configuration(
# the API server.
#
# See {{{packageName}}}.signing for a list of all supported parameters.
from {{{packageName}}} import signing
import datetime

configuration = {{{packageName}}}.Configuration(
host = "{{{basePath}}}",
signing_info = {{{packageName}}}.signing.HttpSigningConfiguration(
signing_info = {{{packageName}}}.HttpSigningConfiguration(
key_id = 'my-key-id',
private_key_path = 'private_key.pem',
private_key_passphrase = 'YOUR_PASSPHRASE',
Expand Down
11 changes: 8 additions & 3 deletions samples/client/echo_api/python-nextgen/docs/BodyApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ Test binary (gif) response body
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -46,6 +46,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling BodyApi->test_binary_gif: %s\n" % e)
```


### Parameters
This endpoint does not need any parameter.

Expand Down Expand Up @@ -79,12 +80,13 @@ Test body parameter(s)
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.models.pet import Pet
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -107,6 +109,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling BodyApi->test_echo_body_pet: %s\n" % e)
```


### Parameters

Name | Type | Description | Notes
Expand Down Expand Up @@ -143,12 +146,13 @@ Test empty response body
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.models.pet import Pet
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -171,6 +175,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling BodyApi->test_echo_body_pet_response_string: %s\n" % e)
```


### Parameters

Name | Type | Description | Notes
Expand Down
3 changes: 2 additions & 1 deletion samples/client/echo_api/python-nextgen/docs/FormApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Test form parameter(s)
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -47,6 +47,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling FormApi->test_form_integer_boolean_string: %s\n" % e)
```


### Parameters

Name | Type | Description | Notes
Expand Down
3 changes: 2 additions & 1 deletion samples/client/echo_api/python-nextgen/docs/HeaderApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Test header parameter(s)
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -47,6 +47,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling HeaderApi->test_header_integer_boolean_string: %s\n" % e)
```


### Parameters

Name | Type | Description | Notes
Expand Down
3 changes: 2 additions & 1 deletion samples/client/echo_api/python-nextgen/docs/PathApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Test path parameter(s)
### Example

```python
from __future__ import print_function
import time
import os
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

# Defining the host is optional and defaults to http://localhost:3000
# See configuration.py for a list of all supported configuration parameters.
configuration = openapi_client.Configuration(
Expand All @@ -46,6 +46,7 @@ with openapi_client.ApiClient(configuration) as api_client:
print("Exception when calling PathApi->tests_path_string_path_string_integer_path_integer: %s\n" % e)
```


### Parameters

Name | Type | Description | Notes
Expand Down
Loading

0 comments on commit 0176957

Please sign in to comment.