Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python oauth sdk generator #592

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
af62974
chore: python mustache file implementation
manisha1997 Jul 3, 2024
41d1ea6
chore: python mustache file implementation
manisha1997 Jul 6, 2024
3be30f4
chore: python mustache file implementation
manisha1997 Jul 9, 2024
c91a264
chore: python mustache file implementation
manisha1997 Jul 11, 2024
38d97e7
Merge branch 'main' into python-oauth-sdk-generator
manisha1997 Jul 11, 2024
ce01e1f
chore: python mustache file implementation
manisha1997 Jul 12, 2024
145732f
chore: python mustache file implementation
manisha1997 Jul 12, 2024
b75437f
chore: python mustache file implementation
manisha1997 Jul 16, 2024
4064413
chore: python mustache file implementation
manisha1997 Jul 17, 2024
d0f36cf
chore: python mustache file implementation
manisha1997 Jul 17, 2024
805311e
chore: python mustache file implementation
manisha1997 Jul 17, 2024
13b637b
chore: python mustache file implementation
manisha1997 Jul 17, 2024
6e1d450
chore: python mustache file implementation
manisha1997 Jul 17, 2024
3378503
chore: python mustache file implementation
manisha1997 Jul 17, 2024
8b591a3
chore: python mustache file implementation
manisha1997 Jul 17, 2024
dca0a28
chore: python mustache file implementation
manisha1997 Jul 17, 2024
fecff8b
chore: python mustache file implementation
manisha1997 Jul 17, 2024
3b6a511
fix: add support for required query params in node (#596)
tiwarishubham635 Aug 1, 2024
70a2e80
chore: removing unused params from python model constructor (#597)
tiwarishubham635 Aug 2, 2024
507441e
chore: remove local file paths (#598)
tiwarishubham635 Aug 5, 2024
e530477
chore: librarian time check (#594)
manisha1997 Aug 12, 2024
229f84d
chore: fixing nullable attributes (#591)
manisha1997 Aug 19, 2024
0624084
chore: revert changes (#603)
manisha1997 Aug 19, 2024
966cd0e
chore: ignore content_v1 spec for release and iam_organizations spec …
sbansla Aug 23, 2024
02f52ad
chore: added a null check for content v1 spec file (#610)
sbansla Sep 4, 2024
47d99b5
chore: setting contentType correctly in java (#608)
tiwarishubham635 Sep 5, 2024
8ab4805
chore: skipping isOverridden flag in distinctResponseModels (#611)
tiwarishubham635 Sep 18, 2024
ecefb59
Generate init files for all subdomains for version less domains
AsabuHere Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/go/Dockerfile-goimports
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.18

ENV GOPATH /go
RUN go install golang.org/x/tools/cmd/goimports@latest
RUN go install golang.org/x/tools/cmd/goimports@v0.24.0

CMD goimports -w /local
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public User create(final BearerTokenTwilioRestClient client){
Domains.PREVIEWIAM.toString(),
path
);
request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
request.setContentType(EnumConstants.ContentType.JSON);
addPostParams(request, client);
Response response = client.request(request);
if (response == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public User update(final BearerTokenTwilioRestClient client){
Domains.PREVIEWIAM.toString(),
path
);
request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
request.setContentType(EnumConstants.ContentType.JSON);
addPostParams(request, client);
addHeaderParams(request);
Response response = client.request(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

package com.twilio.rest.previewiam.organizations;
package com.twilio.rest.previewiam.v1;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Do not edit the class manually.
*/

package com.twilio.rest.previewiam.organizations;
package com.twilio.rest.previewiam.v1;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.twilio.constant.EnumConstants;
Expand Down
20 changes: 4 additions & 16 deletions examples/python/twilio/rest/api/v2010/account/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ def delete(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return self._version.delete(
method="DELETE",
uri=self._uri,
)
return self._version.delete(method="DELETE", uri=self._uri)

async def delete_async(self) -> bool:
"""
Expand All @@ -243,10 +240,7 @@ async def delete_async(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return await self._version.delete_async(
method="DELETE",
uri=self._uri,
)
return await self._version.delete_async(method="DELETE", uri=self._uri)

def fetch(self) -> AccountInstance:
"""
Expand All @@ -256,10 +250,7 @@ def fetch(self) -> AccountInstance:
:returns: The fetched AccountInstance
"""

payload = self._version.fetch(
method="GET",
uri=self._uri,
)
payload = self._version.fetch(method="GET", uri=self._uri)

return AccountInstance(
self._version,
Expand All @@ -275,10 +266,7 @@ async def fetch_async(self) -> AccountInstance:
:returns: The fetched AccountInstance
"""

payload = await self._version.fetch_async(
method="GET",
uri=self._uri,
)
payload = await self._version.fetch_async(method="GET", uri=self._uri)

return AccountInstance(
self._version,
Expand Down
20 changes: 4 additions & 16 deletions examples/python/twilio/rest/api/v2010/account/call/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ def delete(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return self._version.delete(
method="DELETE",
uri=self._uri,
)
return self._version.delete(method="DELETE", uri=self._uri)

async def delete_async(self) -> bool:
"""
Expand All @@ -206,10 +203,7 @@ async def delete_async(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return await self._version.delete_async(
method="DELETE",
uri=self._uri,
)
return await self._version.delete_async(method="DELETE", uri=self._uri)

def fetch(self) -> CallInstance:
"""
Expand All @@ -219,10 +213,7 @@ def fetch(self) -> CallInstance:
:returns: The fetched CallInstance
"""

payload = self._version.fetch(
method="GET",
uri=self._uri,
)
payload = self._version.fetch(method="GET", uri=self._uri)

return CallInstance(
self._version,
Expand All @@ -239,10 +230,7 @@ async def fetch_async(self) -> CallInstance:
:returns: The fetched CallInstance
"""

payload = await self._version.fetch_async(
method="GET",
uri=self._uri,
)
payload = await self._version.fetch_async(method="GET", uri=self._uri)

return CallInstance(
self._version,
Expand Down
20 changes: 4 additions & 16 deletions examples/python/twilio/rest/flex_api/v1/credential/aws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ def delete(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return self._version.delete(
method="DELETE",
uri=self._uri,
)
return self._version.delete(method="DELETE", uri=self._uri)

async def delete_async(self) -> bool:
"""
Expand All @@ -190,10 +187,7 @@ async def delete_async(self) -> bool:

:returns: True if delete succeeds, False otherwise
"""
return await self._version.delete_async(
method="DELETE",
uri=self._uri,
)
return await self._version.delete_async(method="DELETE", uri=self._uri)

def fetch(self) -> AwsInstance:
"""
Expand All @@ -203,10 +197,7 @@ def fetch(self) -> AwsInstance:
:returns: The fetched AwsInstance
"""

payload = self._version.fetch(
method="GET",
uri=self._uri,
)
payload = self._version.fetch(method="GET", uri=self._uri)

return AwsInstance(
self._version,
Expand All @@ -222,10 +213,7 @@ async def fetch_async(self) -> AwsInstance:
:returns: The fetched AwsInstance
"""

payload = await self._version.fetch_async(
method="GET",
uri=self._uri,
)
payload = await self._version.fetch_async(method="GET", uri=self._uri)

return AwsInstance(
self._version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ def fetch(self) -> FleetInstance:
:returns: The fetched FleetInstance
"""

payload = self._version.fetch(
method="GET",
uri=self._uri,
)
payload = self._version.fetch(method="GET", uri=self._uri)

return FleetInstance(
self._version,
Expand All @@ -129,10 +126,7 @@ async def fetch_async(self) -> FleetInstance:
:returns: The fetched FleetInstance
"""

payload = await self._version.fetch_async(
method="GET",
uri=self._uri,
)
payload = await self._version.fetch_async(method="GET", uri=self._uri)

return FleetInstance(
self._version,
Expand Down
34 changes: 23 additions & 11 deletions scripts/build_twilio_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'php': 'Rest'
}
generateForLanguages = {
'twilio_iam_organizations.json' : ['java']
'twilio_iam_organizations.json' : ['java','python']
}
CLEANUP_IMPORT_LANGUAGES = ['java', 'php']
REMOVE_DUPLICATE_IMPORT_LANGUAGES = ['node']
Expand All @@ -32,7 +32,8 @@ def build(openapi_spec_path: str, output_path: str, language: str) -> None:
else:
spec_folder = openapi_spec_path
spec_files = sorted(os.listdir(spec_folder))

if 'twilio_content_v1.json' in spec_files:
spec_files.remove('twilio_content_v1.json')
generate(spec_folder, spec_files, output_path, language)


Expand All @@ -50,6 +51,18 @@ def generate(spec_folder: str, spec_files: List[str], output_path: str, language
if language in generateForLanguages.get(spec_file):
generate_domain_for_language(spec_file, config_path, spec_folder, output_path, language, parent_dir)
else: generate_domain_for_language(spec_file, config_path, spec_folder, output_path, language, parent_dir)
if spec_files[0] in generateForLanguages and language in generateForLanguages.get(spec_files[0]):
print(f'Generating {output_path} from {spec_folder}')
run_openapi_generator(parent_dir, language)
print(f'Code generation completed at {output_path}')
elif spec_files[0] not in generateForLanguages:
print(f'Generating {output_path} from {spec_folder}')
run_openapi_generator(parent_dir, language)
print(f'Code generation completed at {output_path}')
if language in CLEANUP_IMPORT_LANGUAGES:
remove_unused_imports(output_path, language)
if language in REMOVE_DUPLICATE_IMPORT_LANGUAGES:
remove_duplicate_imports(output_path, language)

def generate_domain_for_language(spec_file: str, config_path: str, spec_folder: str, output_path: str, language: str, parent_dir: str) -> None:
full_path = os.path.join(spec_folder, spec_file)
Expand All @@ -62,18 +75,17 @@ def generate_domain_for_language(spec_file: str, config_path: str, spec_folder:
'arrayItemSuffix': ''
},
}

# print(config)
with open(full_config_path, 'w') as f:
f.write(json.dumps(config))

print(f'Generating {output_path} from {spec_folder}')
run_openapi_generator(parent_dir, language)
print(f'Code generation completed at {output_path}')

if language in CLEANUP_IMPORT_LANGUAGES:
remove_unused_imports(output_path, language)
if language in REMOVE_DUPLICATE_IMPORT_LANGUAGES:
remove_duplicate_imports(output_path, language)
# print(f'Generating {output_path} from {spec_folder}')
# run_openapi_generator(parent_dir, language)
# print(f'Code generation completed at {output_path}')
# if language in CLEANUP_IMPORT_LANGUAGES:
# remove_unused_imports(output_path, language)
# if language in REMOVE_DUPLICATE_IMPORT_LANGUAGES:
# remove_duplicate_imports(output_path, language)

def run_openapi_generator(parent_dir: Path, language: str) -> None:
properties = '-DapiTests=false'
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/twilio/oai/TwilioCodegenAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twilio.oai.common.ApplicationConstants;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.servers.Server;
Expand Down Expand Up @@ -89,6 +86,12 @@ public String getVersionFromOpenAPI(final OpenAPI openAPI) {
public void setDomain(final String domain) {
final String domainPackage = domain.replaceAll("[-.]", "");
setOutputDir(domainPackage, getInputSpecVersion());
if(getInputSpecVersion() == ""){
codegen.additionalProperties().put("isVersionLess", true);
}
else{
codegen.additionalProperties().put("isVersionLess", false);
}

codegen.additionalProperties().put("domainName", StringHelper.camelize(domain));
codegen.additionalProperties().put("domainPackage", domainPackage);
Expand Down Expand Up @@ -146,6 +149,9 @@ private String getInputSpecDomain() {
}

private String getInputSpecVersion() {
return codegen.getInputSpec().replaceAll(INPUT_SPEC_PATTERN, "${version}");
String version = codegen.getInputSpec().replaceAll(INPUT_SPEC_PATTERN, "${version}");
boolean textExists = Arrays.stream(ApplicationConstants.VERSION_LESS_SPECS)
.anyMatch(ignoredVersion -> ignoredVersion.equals(version));
return textExists ? "" : version;
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/twilio/oai/TwilioPythonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void processOpts() {
public void processOpenAPI(final OpenAPI openAPI) {
String domain = StringHelper.toSnakeCase(twilioCodegen.getDomainFromOpenAPI(openAPI));
String version = StringHelper.toSnakeCase(twilioCodegen.getVersionFromOpenAPI(openAPI));
populateVersionDetails(actionTemplate, version);
twilioCodegen.setDomain(domain);
twilioCodegen.setVersion(version);
twilioCodegen.setOutputDir(domain, version);
Expand All @@ -70,6 +71,15 @@ public void processOpenAPI(final OpenAPI openAPI) {
directoryStructureService.configure(openAPI);
}

private void populateVersionDetails(PythonApiActionTemplate actionTemplate, String version) {
if(version.equals("")){
actionTemplate.setIsVersionLess(true);
}
else{
actionTemplate.setIsVersionLess(false);
}
}

@Override
public String apiFilename(final String templateName, final String tag) {
final String initFilename = "__init__.py";
Expand All @@ -87,10 +97,11 @@ public String apiFilename(final String templateName, final String tag) {
return isMatchingPath && pathHasDependents;
});

//Create init files in sub folders, this return path of init file in this subfolder
if (isInitFile && !filename.endsWith(initFilename)) {
return PathUtils.removeExtension(filename) + File.separator + initFilename;
}

//Processing files other than init files
return filename;
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/twilio/oai/api/ApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ApiResourceBuilder updateOperations(Resolver<CodegenParameter> codegenPar
}

public void updateHttpMethod(CodegenOperation co) {

}

protected void resolveParam(final Resolver<CodegenParameter> codegenParameterIResolver,
Expand Down Expand Up @@ -191,7 +191,7 @@ public CodegenModel getModel(String modelName ) {
}
return null;
}

public void addNestedModel(final CodegenModel codegenModel) {
// TODO: Temporary fix, planning to use hashmap, set is inserting duplicate values.
Iterator<CodegenModel> iterator = nestedModels.iterator();
Expand Down Expand Up @@ -290,12 +290,17 @@ protected Set<CodegenProperty> getDistinctResponseModel(List<CodegenModel> respo
for (CodegenModel codegenModel : responseModels) {
for (CodegenProperty property : codegenModel.vars) {
property.nameInCamelCase = StringHelper.camelize(property.nameInSnakeCase);
// When we have responseModel referencing a schema, isOverriden flag is set to false (which is null by default). This makes the property different. See assistants api ListAssistantResponse
Boolean isOverridden = property.isOverridden;
if(isOverridden != null && isOverridden == false)
property.isOverridden = null;
if (Arrays
.stream(Operation.values())
.anyMatch(value -> value.getValue().equals(property.nameInCamelCase))) {
property.nameInCamelCase = "_" + property.nameInCamelCase;
}
distinctResponseModels.add(property);
property.isOverridden = isOverridden;
}
}
return distinctResponseModels;
Expand Down
Loading
Loading