Skip to content

Commit

Permalink
chore: python mustache file implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
manisha1997 committed Jul 11, 2024
1 parent 3be30f4 commit c91a264
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 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 Down
6 changes: 3 additions & 3 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function generate() {
rm -rf tmp
mkdir -p tmp
for api_spec in $files_regex; do
if [ "$1" != "twilio-java" ] && [[ $api_spec == "examples/spec/twilio_iam_organizations.yaml" ]]; then
continue
fi
# if [ "$1" != "twilio-java" ] && [[ $api_spec == "examples/spec/twilio_iam_organizations.yaml" ]]; then
# continue
# fi
echo "generatorName: $1
inputSpec: $api_spec
outputDir: $OUT_DIR
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/twilio-python/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ from twilio.base import deserialize, serialize, values
{{#responseModel}}from twilio.base.instance_resource import InstanceResource{{/responseModel}}
from twilio.base.list_resource import ListResource
from twilio.base.version import Version
{{#vendorExtensions.x-auth-attributes.x-oauth}}
{{#operations}}{{#vendorExtensions.x-auth-attributes.x-oauth}}
from twilio.http.bearer_token_http_client import BearerTokenHTTPClient
from twilio.http.token_manager import TokenManager
{{/vendorExtensions.x-auth-attributes.x-oauth}}
{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/operations}}
{{#hasPaginationOperation}}from twilio.base.page import Page{{/hasPaginationOperation}}
{{#dependents}}from twilio.rest.{{domainPackage}}.{{apiVersion}}.{{namespaceSubPart}}.{{filename}} import {{resourceName}}List
{{/dependents}}
Expand Down
60 changes: 45 additions & 15 deletions src/main/resources/twilio-python/context.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ class {{apiName}}Context(InstanceContext):
data = values.of({ {{#allParams}}{{#isFormParam}}
'{{{baseName}}}': {{#if vendorExtensions.x-serialize}}{{vendorExtensions.x-serialize}}({{paramName}}{{#if isArray}}, lambda e: e){{else}}){{/if}}{{else}}{{paramName}}{{/if}},{{/isFormParam}}{{/allParams}}
})
{{#allParams}}{{^isFormParam}}headers = values.of({'{{{baseName}}}': {{paramName}}, }){{/isFormParam}}{{/allParams}}
{{#allParams}}{{^isFormParam}}headers = values.of({'{{{baseName}}}': {{paramName}}, })
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
{{/vendorExtensions.x-auth-attributes.x-oauth}}
payload = self._version.update(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data,{{#allParams}}{{#if isFormParam}}{{else}} headers=headers{{/if}}{{/allParams}})
{{/isFormParam}}{{/allParams}}
{{#allParams}}{{#isFormParam}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers = {'Authorization' : 'Bearer ' + auth_token }
{{/vendorExtensions.x-auth-attributes.x-oauth}}
{{/isFormParam}}{{/allParams}}
payload = self._version.update(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data,{{#allParams}}{{#if isFormParam}}{{else}} headers=headers{{/if}}{{/allParams}} {{#allParams}}{{#isFormParam}}{{#vendorExtensions.x-auth-attributes.x-oauth}}headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/isFormParam}}{{/allParams}})

return {{instanceName}}(
self._version,
Expand All @@ -55,13 +63,21 @@ class {{apiName}}Context(InstanceContext):
data = values.of({ {{#allParams}}{{#isFormParam}}
'{{{baseName}}}': {{#if vendorExtensions.x-serialize}}{{vendorExtensions.x-serialize}}({{paramName}}{{#if isArray}}, lambda e: e){{else}}){{/if}}{{else}}{{paramName}}{{/if}},{{/isFormParam}}{{/allParams}}
})
{{#allParams}}{{^isFormParam}}headers = values.of({'{{{baseName}}}': {{paramName}}, }){{/isFormParam}}{{/allParams}}
{{#allParams}}{{^isFormParam}}headers = values.of({'{{{baseName}}}': {{paramName}}, })
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
{{/vendorExtensions.x-auth-attributes.x-oauth}}
payload = await self._version.update_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data,{{#allParams}}{{#if isFormParam}}{{else}} headers=headers{{/if}}{{/allParams}})
{{/isFormParam}}{{/allParams}}
{{#allParams}}{{#isFormParam}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers = {'Authorization' : 'Bearer ' + auth_token }
{{/vendorExtensions.x-auth-attributes.x-oauth}}
{{/isFormParam}}{{/allParams}}
payload = await self._version.update_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data,{{#allParams}}{{#if isFormParam}}{{else}} headers=headers{{/if}}{{/allParams}} {{#allParams}}{{#isFormParam}}{{#vendorExtensions.x-auth-attributes.x-oauth}}headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/isFormParam}}{{/allParams}})

return {{instanceName}}(
self._version,
Expand All @@ -86,9 +102,9 @@ class {{apiName}}Context(InstanceContext):
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
headers = {'Authorization': 'Bearer ' + auth_token}
{{/vendorExtensions.x-auth-attributes.x-oauth}}
payload = self._version.fetch(method='{{vendorExtensions.x-http-method}}', uri=self._uri, {{#if allParams}}params=data{{/if}})
payload = self._version.fetch(method='{{vendorExtensions.x-http-method}}', uri=self._uri{{#if allParams}}, params=data{{/if}}{{#vendorExtensions.x-auth-attributes.x-oauth}}, headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}})

return {{instanceName}}(
self._version,
Expand All @@ -114,9 +130,9 @@ class {{apiName}}Context(InstanceContext):
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
headers = {'Authorization': 'Bearer ' + auth_token}
{{/vendorExtensions.x-auth-attributes.x-oauth}}
payload = await self._version.fetch_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri, {{#if allParams}}params=data{{/if}})
payload = await self._version.fetch_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri{{#if allParams}}, params=data{{/if}}{{#vendorExtensions.x-auth-attributes.x-oauth}}, headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}})

return {{instanceName}}(
self._version,
Expand All @@ -134,13 +150,20 @@ class {{apiName}}Context(InstanceContext):
:returns: True if delete succeeds, False otherwise
"""{{#if allParams}}
headers = values.of({{#allParams}}{'{{{baseName}}}': {{paramName}}, }{{/allParams}})
{{/if}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
{{/vendorExtensions.x-auth-attributes.x-oauth}}
return self._version.delete(method='{{vendorExtensions.x-http-method}}', uri=self._uri,{{#if allParams}} headers=headers{{/if}})
{{/if}}
{{^if allParams}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers = { 'Authorization': 'Bearer ' + auth_token}
{{/vendorExtensions.x-auth-attributes.x-oauth}}
{{/if}}
return self._version.delete(method='{{vendorExtensions.x-http-method}}', uri=self._uri,{{#if allParams}} headers=headers{{/if}}{{^allParams}}{{#vendorExtensions.x-auth-attributes.x-oauth}} headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/allParams}})

async def {{vendorExtensions.x-name-lower}}_async(self{{#allParams}}, {{paramName}}: {{#if required}}{{{dataType}}}{{else}}Union[{{{dataType}}}, object]=values.unset{{/if}}{{/allParams}}) -> bool:
"""
Expand All @@ -151,13 +174,20 @@ class {{apiName}}Context(InstanceContext):
:returns: True if delete succeeds, False otherwise
"""{{#if allParams}}
headers = values.of({{#allParams}}{'{{{baseName}}}': {{paramName}}, }{{/allParams}})
{{/if}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
{{/vendorExtensions.x-auth-attributes.x-oauth}}
return await self._version.delete_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri,{{#if allParams}} headers=headers{{/if}})
{{/if}}
{{^if allParams}}
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers = { 'Authorization': 'Bearer ' + auth_token }
{{/vendorExtensions.x-auth-attributes.x-oauth}}
{{/if}}
return await self._version.delete_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri,{{#if allParams}} headers=headers{{/if}}{{^allParams}}{{#vendorExtensions.x-auth-attributes.x-oauth}} headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/allParams}})
{{/vendorExtensions.x-is-delete-operation}}{{#vendorExtensions.x-is-create-operation}}
def {{vendorExtensions.x-name-lower}}(self{{#allParams}}, {{paramName}}: {{#if required}}{{{dataType}}}{{else}}Union[{{{dataType}}}, object]=values.unset{{/if}}{{/allParams}}) -> {{instanceName}}:
"""
Expand All @@ -176,7 +206,7 @@ class {{apiName}}Context(InstanceContext):
headers = {'Authorization': 'Bearer ' + auth_token}
{{/vendorExtensions.x-auth-attributes.x-oauth}}

payload = self._version.create(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data)
payload = self._version.create(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data{{#vendorExtensions.x-auth-attributes.x-oauth}}, headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}})

return {{instanceName}}(
self._version,
Expand All @@ -198,10 +228,10 @@ class {{apiName}}Context(InstanceContext):
{{#vendorExtensions.x-auth-attributes.x-oauth}}
orgs_token_manager = TokenManager().get_token_manager(self._version)
auth_token = BearerTokenHTTPClient(orgs_token_manager).get_headers(self._version)
headers['Authorization'] = 'Bearer ' + auth_token
headers = {'Authorization': 'Bearer ' + auth_token}
{{/vendorExtensions.x-auth-attributes.x-oauth}}

payload = await self._version.create_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data)
payload = await self._version.create_async(method='{{vendorExtensions.x-http-method}}', uri=self._uri, data=data{{#vendorExtensions.x-auth-attributes.x-oauth}}, headers=headers{{/vendorExtensions.x-auth-attributes.x-oauth}})

return {{instanceName}}(
self._version,
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/twilio-python/version.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
from typing import Optional
from twilio.base.version import Version
from twilio.base.domain import Domain
{{#operations}}{{#vendorExtensions.x-auth-attributes.x-oauth}}
from twilio.http.bearer_token_http_client import BearerTokenHTTPClient
from twilio.http.token_manager import TokenManager
{{/vendorExtensions.x-auth-attributes.x-oauth}}{{/operations}}
{{#versionResources}}from twilio.rest.{{domainPackage}}.{{apiVersion}}.{{filename}} import {{listName}}
{{/versionResources}}

Expand Down

0 comments on commit c91a264

Please sign in to comment.