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

[FERN-1985] Dynamic Client Instantiation for Python Template Factory #3852

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
75 changes: 28 additions & 47 deletions generators/python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generators/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pydantic = "^1.9.2,<=1.10.14"
typer = {extras = ["all"], version = "^0.6.1"}
fern-fern-generator-exec-sdk = {version = "0.0.846", source = "fern-prod"}
ordered-set = "^4.1.0"
fern-fern-fdr-sdk = {version = "0.0.5435", source = "fern-prod"}
fern-fern-fdr-sdk = {version = "0.0.5496", source = "fern-prod"}
fern-fern-ir-v39 = "^0.0.3473"

[tool.poetry.dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from dataclasses import dataclass
from typing import List, Optional

import fern.ir.resources as ir_types

from fern_python.codegen import AST, SourceFile
from fern_python.codegen.ast.nodes.code_writer.code_writer import CodeWriterFunction
from fern_python.generators.sdk.client_generator.endpoint_response_code_writer import (
EndpointResponseCodeWriter,
)
from fern_python.snippet import SnippetRegistry, SnippetWriter

import fern.ir.resources as ir_types

from ..context.sdk_generator_context import SdkGeneratorContext
from .constants import DEFAULT_BODY_PARAMETER_VALUE
from .endpoint_function_generator import EndpointFunctionGenerator
Expand Down Expand Up @@ -93,7 +93,9 @@ def _create_class_declaration(self, *, is_async: bool) -> AST.ClassDeclaration:
signature=AST.FunctionSignature(
named_parameters=named_parameters,
),
body=AST.CodeWriter(self._get_write_constructor_body(is_async=is_async)),
body=AST.CodeWriter(
self._get_write_constructor_body(is_async=is_async)
),
),
)

Expand Down Expand Up @@ -122,23 +124,33 @@ def _create_class_declaration(self, *, is_async: bool) -> AST.ClassDeclaration:
for snippet in generated_endpoint_function.snippets or []:
if is_async:
self._snippet_registry.register_async_client_endpoint_snippet(
endpoint=endpoint, expr=snippet.snippet, example_id=snippet.example_id
endpoint=endpoint,
expr=snippet.snippet,
example_id=snippet.example_id,
)
else:
self._snippet_registry.register_sync_client_endpoint_snippet(
endpoint=endpoint, expr=snippet.snippet, example_id=snippet.example_id
endpoint=endpoint,
expr=snippet.snippet,
example_id=snippet.example_id,
)

return class_declaration

def _get_constructor_parameters(self, *, is_async: bool) -> List[ConstructorParameter]:
def _get_constructor_parameters(
self, *, is_async: bool
) -> List[ConstructorParameter]:
parameters: List[ConstructorParameter] = []

parameters.append(
ConstructorParameter(
constructor_parameter_name=self._get_client_wrapper_constructor_parameter_name(),
private_member_name=self._get_client_wrapper_member_name(),
type_hint=AST.TypeHint(self._context.core_utilities.get_reference_to_client_wrapper(is_async=is_async)),
type_hint=AST.TypeHint(
self._context.core_utilities.get_reference_to_client_wrapper(
is_async=is_async
)
),
)
)

Expand All @@ -152,20 +164,33 @@ def _write_constructor_body(writer: AST.NodeWriter) -> None:
constructor_parameters = self._get_constructor_parameters(is_async=is_async)
for param in constructor_parameters:
if param.private_member_name is not None:
writer.write_line(f"self.{param.private_member_name} = {param.constructor_parameter_name}")
writer.write_line(
f"self.{param.private_member_name} = {param.constructor_parameter_name}"
)
for subpackage_id in self._package.subpackages:
subpackage = self._context.ir.subpackages[subpackage_id]
if subpackage.has_endpoints_in_tree:
writer.write_node(AST.Expression(f"self.{subpackage.name.snake_case.safe_name} = "))
writer.write_node(
AST.Expression(
f"self.{subpackage.name.snake_case.safe_name} = "
)
)
kwargs = [
(param.constructor_parameter_name, AST.Expression(f"self.{param.private_member_name}"))
(
param.constructor_parameter_name,
AST.Expression(f"self.{param.private_member_name}"),
)
for param in self._get_constructor_parameters(is_async=is_async)
]
writer.write_node(
AST.ClassInstantiation(
class_=self._context.get_reference_to_async_subpackage_service(subpackage_id)
class_=self._context.get_reference_to_async_subpackage_service(
subpackage_id
)
if is_async
else self._context.get_reference_to_subpackage_service(subpackage_id),
else self._context.get_reference_to_subpackage_service(
subpackage_id
),
kwargs=kwargs,
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from typing import List

from fdr import Template
from fern_python.codegen import AST
from fern_python.generators.sdk.core_utilities.client_wrapper_generator import (
ConstructorParameter,
Expand All @@ -15,7 +16,7 @@ class RootClient:

@dataclass
class GeneratedRootClient:
async_instantiation: AST.Expression
async_instantiation_template: Template
async_client: RootClient
sync_instantiation: AST.Expression
sync_instantiation_template: Template
sync_client: RootClient
Loading
Loading