Skip to content

Commit

Permalink
refactor(python-sdk): extract oauth2 from credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Oct 26, 2023
1 parent 4493481 commit 72c9006
Show file tree
Hide file tree
Showing 18 changed files with 426 additions and 567 deletions.
3 changes: 2 additions & 1 deletion config/clients/python/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ test/*
!test/test_client_sync.py
!test/test_open_fga_api_sync.py
!test/test_validation.py
!test/test_credentials_sync.py
!test/test_oauth2.py
!test/test_oauth2_sync.py
.github/workflows/python.yml
.gitlab-ci.yml
.travis.yml
Expand Down
24 changes: 16 additions & 8 deletions config/clients/python/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,29 @@
"destinationFilename": "test/test_open_fga_api_sync.py",
"templateType": "SupportingFiles"
},
"credentials_sync.mustache": {
"destinationFilename": "openfga_sdk/sync/credentials.py",
"templateType": "SupportingFiles"
},
"credentials_test_sync.mustache": {
"destinationFilename": "test/test_credentials_sync.py",
"templateType": "SupportingFiles"
},
"client/client_sync.mustache": {
"destinationFilename": "openfga_sdk/sync/client/client.py",
"templateType": "SupportingFiles"
},
"client/test_client_sync.mustache": {
"destinationFilename": "test/test_client_sync.py",
"templateType": "SupportingFiles"
},
"oauth2.mustache": {
"destinationFilename": "openfga_sdk/oauth2.py",
"templateType": "SupportingFiles"
},
"oauth2_test.mustache": {
"destinationFilename": "test/test_oauth2.py",
"templateType": "SupportingFiles"
},
"oauth2_sync.mustache": {
"destinationFilename": "openfga_sdk/sync/oauth2.py",
"templateType": "SupportingFiles"
},
"oauth2_test_sync.mustache": {
"destinationFilename": "test/test_oauth2_sync.py",
"templateType": "SupportingFiles"
}
}
}
4 changes: 2 additions & 2 deletions config/clients/python/patches/open_fga_api.py.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
--- clients/fga-python-sdk/openfga_sdk/api/open_fga_api.py 2022-09-13 14:15:46.000000000 -0400
+++ open_fga_api.py 2022-09-13 14:14:01.000000000 -0400
@@ -193,13 +193,15 @@
collection_formats=collection_formats,
_request_auth=local_var_params.get('_request_auth')))
_request_auth=local_var_params.get('_request_auth'),
_oauth2_client=self._oauth2_client))

- async def create_store(self, **kwargs): # noqa: E501
+ async def create_store(self, body, **kwargs): # noqa: E501
Expand Down
4 changes: 2 additions & 2 deletions config/clients/python/patches/open_fga_api_sync.py.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
--- clients/fga-python-sdk/openfga_sdk/sync/open_fga_api.py 2022-09-13 14:15:46.000000000 -0400
+++ open_fga_api.py 2022-09-13 14:14:01.000000000 -0400
@@ -193,13 +193,15 @@
collection_formats=collection_formats,
_request_auth=local_var_params.get('_request_auth'))
_request_auth=local_var_params.get('_request_auth'),
_oauth2_client=self._oauth2_client)

- def create_store(self, **kwargs): # noqa: E501
+ def create_store(self, body, **kwargs): # noqa: E501
Expand Down
1 change: 0 additions & 1 deletion config/clients/python/template/__init__sync.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

from {{packageName}}.sync.client.client import OpenFgaClient
from {{packageName}}.sync.api_client import ApiClient
from {{packageName}}.sync.credentials import Credentials
10 changes: 9 additions & 1 deletion config/clients/python/template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import re # noqa: F401
import six

from {{packageName}}.api_client import ApiClient
from {{packageName}}.oauth2 import OAuth2Client
from {{packageName}}.exceptions import ( # noqa: F401
FgaValidationException,
ApiValueError
Expand All @@ -28,6 +29,12 @@ class {{classname}}(object):
api_client = ApiClient()
self.api_client = api_client

self._oauth2_client = None
if api_client.configuration is not None:
credentials = api_client.configuration.credentials
if credentials is not None and credentials.method == 'client_credentials':
self._oauth2_client = OAuth2Client(credentials)

{{#asyncio}}
async def __aenter__(self):
return self
Expand Down Expand Up @@ -336,6 +343,7 @@ class {{classname}}(object):
_host=local_var_host,
{{/servers.0}}
collection_formats=collection_formats,
_request_auth=local_var_params.get('_request_auth')){{#asyncio}}){{/asyncio}}
_request_auth=local_var_params.get('_request_auth'),
_oauth2_client=self._oauth2_client){{#asyncio}}){{/asyncio}}
{{/operation}}
{{/operations}}
33 changes: 22 additions & 11 deletions config/clients/python/template/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import tornado.gen

from {{packageName}}.configuration import Configuration
import {{modelPackage}}
from {{packageName}} import rest
from {{packageName}} import rest, oauth2
from {{packageName}}.exceptions import ApiValueError, ApiException, FgaValidationException, RateLimitExceededError


Expand Down Expand Up @@ -152,7 +152,7 @@ class ApiClient(object):
response_types_map=None, auth_settings=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None, _host=None,
_request_auth=None, _retry_params=None):
_request_auth=None, _retry_params=None, _oauth2_client=None):

self.configuration.is_valid()
config = self.configuration
Expand Down Expand Up @@ -195,7 +195,7 @@ class ApiClient(object):
# auth setting
{{#asyncio}}await {{/asyncio}}self.update_params_for_auth(
header_params, query_params, auth_settings,
request_auth=_request_auth)
request_auth=_request_auth, oauth2_client=_oauth2_client)

# body
if body:
Expand Down Expand Up @@ -393,7 +393,7 @@ class ApiClient(object):
async_req=None, _return_http_data_only=None,
collection_formats=None,_preload_content=True,
_request_timeout=None, _host=None, _request_auth=None,
_retry_params=None):
_retry_params=None, _oauth2_client=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.

To make an async_req request, set the async_req parameter.
Expand Down Expand Up @@ -441,7 +441,7 @@ class ApiClient(object):
response_types_map, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout, _host,
_request_auth, _retry_params){{#asyncio}}){{/asyncio}}
_request_auth, _retry_params, _oauth2_client){{#asyncio}}){{/asyncio}}

return self.pool.apply_async(self.__call_api, (resource_path,
method, path_params,
Expand All @@ -454,7 +454,9 @@ class ApiClient(object):
collection_formats,
_preload_content,
_request_timeout,
_host, _request_auth, _retry_params))
_host, _request_auth,
_retry_params,
_oauth2_client))

{{#asyncio}}async {{/asyncio}}def request(self, method, url, query_params=None, headers=None,
post_params=None, body=None, _preload_content=True,
Expand Down Expand Up @@ -585,19 +587,28 @@ class ApiClient(object):
return content_types[0]

{{#asyncio}}async {{/asyncio}}def update_params_for_auth(self, headers, queries, auth_settings,
request_auth=None):
request_auth=None, oauth2_client=None):
"""Updates header and query params based on authentication setting.

:param headers: Header parameters dict to be updated.
:param queries: Query parameters tuple list to be updated.
:param auth_settings: Authentication setting identifiers list.
:param request_auth: if set, the provided settings will
override the token in the configuration.
:param oauth2_client: if set, will be used for credential exchange.
"""
if self.configuration.credentials is not None:
added_headers = {{#asyncio}}await {{/asyncio}}self.configuration.credentials.get_authentication_header(self.rest_client)
for key, value in added_headers.items():
headers[key] = value
credentials = self.configuration.credentials
if credentials is not None:
if credentials.method == 'none':
pass
if credentials.method == 'api_token':
headers['Authorization'] = 'Bearer {}'.format(credentials.configuration.api_token)
if credentials.method == 'client_credentials':
if oauth2_client is None:
oauth2_client = oauth2.OAuth2Client(credentials)
oauth2_headers = await oauth2_client.get_authentication_header(self.rest_client)
for key, value in oauth2_headers.items():
headers[key] = value

if not auth_settings:
return
Expand Down
33 changes: 22 additions & 11 deletions config/clients/python/template/api_client_sync.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import tornado.gen

from {{packageName}}.configuration import Configuration
import {{modelPackage}}
from {{packageName}}.sync import rest
from {{packageName}}.sync import rest, oauth2
from {{packageName}}.exceptions import ApiValueError, ApiException, FgaValidationException, RateLimitExceededError


Expand Down Expand Up @@ -135,7 +135,7 @@ class ApiClient(object):
response_types_map=None, auth_settings=None,
_return_http_data_only=None, collection_formats=None,
_preload_content=True, _request_timeout=None, _host=None,
_request_auth=None, _retry_params=None):
_request_auth=None, _retry_params=None, _oauth2_client=None):

self.configuration.is_valid()
config = self.configuration
Expand Down Expand Up @@ -178,7 +178,7 @@ class ApiClient(object):
# auth setting
self.update_params_for_auth(
header_params, query_params, auth_settings,
request_auth=_request_auth)
request_auth=_request_auth, oauth2_client=_oauth2_client)

# body
if body:
Expand Down Expand Up @@ -375,7 +375,7 @@ class ApiClient(object):
async_req=None, _return_http_data_only=None,
collection_formats=None,_preload_content=True,
_request_timeout=None, _host=None, _request_auth=None,
_retry_params=None):
_retry_params=None, _oauth2_client=None):
"""Makes the HTTP request (synchronous) and returns deserialized data.

To make an async_req request, set the async_req parameter.
Expand Down Expand Up @@ -423,7 +423,7 @@ class ApiClient(object):
response_types_map, auth_settings,
_return_http_data_only, collection_formats,
_preload_content, _request_timeout, _host,
_request_auth, _retry_params)
_request_auth, _retry_params, _oauth2_client)

return self.pool.apply_async(self.__call_api, (resource_path,
method, path_params,
Expand All @@ -436,7 +436,9 @@ class ApiClient(object):
collection_formats,
_preload_content,
_request_timeout,
_host, _request_auth, _retry_params))
_host, _request_auth,
_retry_params,
_oauth2_client))

def request(self, method, url, query_params=None, headers=None,
post_params=None, body=None, _preload_content=True,
Expand Down Expand Up @@ -567,19 +569,28 @@ class ApiClient(object):
return content_types[0]

def update_params_for_auth(self, headers, queries, auth_settings,
request_auth=None):
request_auth=None, oauth2_client=None):
"""Updates header and query params based on authentication setting.

:param headers: Header parameters dict to be updated.
:param queries: Query parameters tuple list to be updated.
:param auth_settings: Authentication setting identifiers list.
:param request_auth: if set, the provided settings will
override the token in the configuration.
:param oauth2_client: if set, will be used for credential exchange.
"""
if self.configuration.credentials is not None:
added_headers = self.configuration.credentials.get_authentication_header(self.rest_client)
for key, value in added_headers.items():
headers[key] = value
credentials = self.configuration.credentials
if credentials is not None:
if credentials.method == 'none':
pass
if credentials.method == 'api_token':
headers['Authorization'] = 'Bearer {}'.format(credentials.configuration.api_token)
if credentials.method == 'client_credentials':
if oauth2_client is None:
oauth2_client = oauth2.OAuth2Client(credentials)
oauth2_headers = oauth2_client.get_authentication_header(self.rest_client)
for key, value in oauth2_headers.items():
headers[key] = value

if not auth_settings:
return
Expand Down
11 changes: 10 additions & 1 deletion config/clients/python/template/api_sync.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import re # noqa: F401
import six

from {{packageName}}.sync.api_client import ApiClient
from {{packageName}}.sync.oauth2 import OAuth2Client
from {{packageName}}.exceptions import ( # noqa: F401
FgaValidationException,
ApiValueError
Expand All @@ -27,6 +28,13 @@ class {{classname}}(object):
if api_client is None:
api_client = ApiClient()
self.api_client = api_client

self._oauth2_client = None
if api_client.configuration is not None:
credentials = api_client.configuration.credentials
if credentials is not None and credentials.method == 'client_credentials':
self._oauth2_client = OAuth2Client(credentials)


def __enter__(self):
return self
Expand Down Expand Up @@ -324,6 +332,7 @@ class {{classname}}(object):
_host=local_var_host,
{{/servers.0}}
collection_formats=collection_formats,
_request_auth=local_var_params.get('_request_auth'))
_request_auth=local_var_params.get('_request_auth'),
_oauth2_client=self._oauth2_client)
{{/operation}}
{{/operations}}
4 changes: 2 additions & 2 deletions config/clients/python/template/api_test_sync.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ from datetime import datetime
import urllib3

import {{packageName}}.sync
from {{packageName}}.sync import rest, open_fga_api, Credentials
from {{packageName}}.sync import rest, open_fga_api
from {{packageName}}.sync.api_client import ApiClient
from {{packageName}}.credentials import CredentialConfiguration
from {{packageName}}.credentials import Credentials, CredentialConfiguration
from {{packageName}}.configuration import Configuration
from {{packageName}}.exceptions import FgaValidationException, ApiValueError, NotFoundException, RateLimitExceededError, ServiceException, ValidationException, FGA_REQUEST_ID
from {{packageName}}.models.assertion import Assertion
Expand Down
Loading

0 comments on commit 72c9006

Please sign in to comment.