Skip to content

Commit

Permalink
feat(python-sdk): generate and test both openfga_sdk and openfga_sdk_…
Browse files Browse the repository at this point in the history
…sync
  • Loading branch information
booniepepper committed Oct 20, 2023
1 parent 5185beb commit 6771482
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,20 @@ tag-client-python: test-client-python

.PHONY: build-client-python
build-client-python:
# Sync SDK
make build-client sdk_language=python tmpdir=${TMP_DIR} package_name=openfga_sdk_sync
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'patch -p1 /module/openfga_sdk_sync/api/open_fga_api.py /config/clients/python/patches/open_fga_sync_api.py.patch'"
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'mv /module/openfga_sdk/* /module/openfga_sdk_sync/ && rmdir /module/openfga_sdk'"
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'cd /module/test && find * -name test\* -exec mv {} sync_{} \;'"
# Preserve sync SDK & tests
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'rm -rf /module/preserve && mkdir -p /module/preserve && mv /module/openfga_sdk_sync /module/preserve/openfga_sdk_sync && mv /module/test /module/preserve/'"

# Async SDK
make build-client sdk_language=python tmpdir=${TMP_DIR} library="asyncio"
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'patch -p1 /module/openfga_sdk/api/open_fga_api.py /config/clients/python/patches/open_fga_api.py.patch'"
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'patch -p1 /module/docs/OpenFgaApi.md /config/clients/python/patches/OpenFgaApi.md.patch'"
# Re-apply the preserved sync SDK
make run-in-docker sdk_language=python image=busybox:${BUSYBOX_DOCKER_TAG} command="/bin/sh -c 'mv /module/preserve/openfga_sdk_sync /module/ && mv /module/preserve/test/sync_test* /module/test && rm -r preserve'"

make run-in-docker sdk_language=python image=python:${PYTHON_DOCKER_TAG} command="/bin/sh -c 'python -m pip install pycodestyle==2.10.0 autopep8==2.0.2; autopep8 --in-place --ignore E402 --recursive openfga_sdk; autopep8 --in-place --recursive test'"
make run-in-docker sdk_language=python image=python:${PYTHON_DOCKER_TAG} command="/bin/sh -c 'python -m pip install pycodestyle==2.10.0 autopep8==2.0.2; autopep8 --in-place --ignore E402 --recursive openfga_sdk_sync; autopep8 --in-place --recursive test'"
Expand Down
24 changes: 12 additions & 12 deletions config/clients/python/template/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from datetime import datetime

import urllib3

import {{packageName}}{{^asyncio}} as openfga_sdk{{/asyncio}}
import {{packageName}}
from {{packageName}} import rest
from {{packageName}}.api import open_fga_api
from {{packageName}}.credentials import Credentials, CredentialConfiguration
Expand Down Expand Up @@ -399,7 +399,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
body = ReadRequest(
tuple_key=TupleKey(
Expand Down Expand Up @@ -453,7 +453,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
mock_request.return_value = mock_response(response_body, 200)
configuration = self.configuration
configuration.store_id = store_id
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
api_instance = open_fga_api.OpenFgaApi(api_client)
api_response = {{#asyncio}}await {{/asyncio}}api_instance.read_assertions(
"01G5JAVJ41T49E9TT3SKVS7X1J",
Expand Down Expand Up @@ -521,7 +521,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -589,7 +589,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -628,7 +628,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -671,7 +671,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -714,7 +714,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
configuration = self.configuration
configuration.store_id = store_id
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -757,7 +757,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
mock_request.return_value = mock_response(response_body, 201)
configuration = self.configuration
configuration.store_id = store_id
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down Expand Up @@ -887,7 +887,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
"""
Test whether ApiValueError is raised if host has query
"""
configuration = openfga_sdk.Configuration(
configuration = {{packageName}}.Configuration(
api_host='localhost',
api_scheme='http',
store_id="abcd"
Expand All @@ -905,7 +905,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
)
configuration.store_id = 'xyz123'
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand All @@ -927,7 +927,7 @@ class {{#operations}}Test{{classname}}(IsolatedAsyncioTestCase):
)
# Notice the store_id is not set
# Enter a context with an instance of the API client
{{#asyncio}}async {{/asyncio}}with openfga_sdk.ApiClient(configuration) as api_client:
{{#asyncio}}async {{/asyncio}}with {{packageName}}.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = open_fga_api.OpenFgaApi(api_client)

Expand Down
2 changes: 1 addition & 1 deletion scripts/build_client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cp "${CONFIG_DIR}/clients/${SDK_LANGUAGE}/CHANGELOG.md.mustache" "${TMP_DIR}/tem

# Clear existing directory
# shellcheck disable=SC2010
cd "${CLIENTS_OUTPUT_DIR}/fga-${SDK_LANGUAGE}-sdk" && ls -A | grep -Ev '.git|node_modules|.idea|venv|.gradle' | xargs rm -r && cd -
cd "${CLIENTS_OUTPUT_DIR}/fga-${SDK_LANGUAGE}-sdk" && ls -A | grep -Ev 'preserve|.git|node_modules|.idea|venv|.gradle' | xargs rm -r && cd -

# Copy the generator ignore file into target directory (we need to do this before build otherwise openapi-generator ignores it)
cp "${CONFIG_DIR}/clients/${SDK_LANGUAGE}/.openapi-generator-ignore" "${CLIENTS_OUTPUT_DIR}/fga-${SDK_LANGUAGE}-sdk/"
Expand Down

0 comments on commit 6771482

Please sign in to comment.