cli/internal/generators/python/python.tmpl

122 lines
4.3 KiB
Cheetah

# AUTOMATICALLY GENERATED BY OPENFEATURE CLI, DO NOT EDIT.
from typing import Optional
from openfeature.client import OpenFeatureClient
from openfeature.evaluation_context import EvaluationContext
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagEvaluationOptions
from openfeature.hook import Hook
class GeneratedClient:
def __init__(
self,
client: OpenFeatureClient,
) -> None:
self.client = client
{{ printf "" }}
{{- range .Flagset.Flags }}
def {{ .Key | ToSnake }}(
self,
evaluation_context: Optional[EvaluationContext] = None,
flag_evaluation_options: Optional[FlagEvaluationOptions] = None,
) -> {{ .Type | OpenFeatureType }}:
"""
{{ .Description }}
**Details:**
- flag key: `{{ .Key }}`
- default value: `{{ .DefaultValue | PythonBoolLiteral }}`
- type: `{{ .Type | OpenFeatureType }}`
Performs a flag evaluation that returns a `{{ .Type | OpenFeatureType }}`.
"""
return self.client.{{ .Type | TypedGetMethodSync }}(
flag_key={{ .Key | Quote }},
default_value={{ .DefaultValue | QuoteString | PythonBoolLiteral }},
evaluation_context=evaluation_context,
flag_evaluation_options=flag_evaluation_options,
)
def {{ .Key | ToSnake }}_details(
self,
evaluation_context: Optional[EvaluationContext] = None,
flag_evaluation_options: Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails:
"""
{{ .Description }}
**Details:**
- flag key: `{{ .Key }}`
- default value: `{{ .DefaultValue | PythonBoolLiteral }}`
- type: `{{ .Type | OpenFeatureType }}`
Performs a flag evaluation that returns a `FlagEvaluationDetails` instance.
"""
return self.client.{{ .Type | TypedDetailsMethodSync }}(
flag_key={{ .Key | Quote }},
default_value={{ .DefaultValue | QuoteString | PythonBoolLiteral }},
evaluation_context=evaluation_context,
flag_evaluation_options=flag_evaluation_options,
)
async def {{ .Key | ToSnake }}_async(
self,
evaluation_context: Optional[EvaluationContext] = None,
flag_evaluation_options: Optional[FlagEvaluationOptions] = None,
) -> {{ .Type | OpenFeatureType }}:
"""
{{ .Description }}
**Details:**
- flag key: `{{ .Key }}`
- default value: `{{ .DefaultValue | PythonBoolLiteral }}`
- type: `{{ .Type | OpenFeatureType }}`
Performs a flag evaluation asynchronously and returns a `{{ .Type | OpenFeatureType }}`.
"""
return await self.client.{{ .Type | TypedGetMethodAsync }}(
flag_key={{ .Key | Quote }},
default_value={{ .DefaultValue | QuoteString | PythonBoolLiteral }},
evaluation_context=evaluation_context,
flag_evaluation_options=flag_evaluation_options,
)
async def {{ .Key | ToSnake }}_details_async(
self,
evaluation_context: Optional[EvaluationContext] = None,
flag_evaluation_options: Optional[FlagEvaluationOptions] = None,
) -> FlagEvaluationDetails:
"""
{{ .Description }}
**Details:**
- flag key: `{{ .Key }}`
- default value: `{{ .DefaultValue | PythonBoolLiteral }}`
- type: `{{ .Type | OpenFeatureType }}`
Performs a flag evaluation asynchronously and returns a `FlagEvaluationDetails` instance.
"""
return await self.client.{{ .Type | TypedDetailsMethodAsync }}(
flag_key={{ .Key | Quote }},
default_value={{ .DefaultValue | QuoteString | PythonBoolLiteral }},
evaluation_context=evaluation_context,
flag_evaluation_options=flag_evaluation_options,
)
{{ end -}}
{{ printf "\n" }}
def get_generated_client(
client: Optional[OpenFeatureClient] = None,
domain: Optional[str] = None,
version: Optional[str] = None,
context: Optional[EvaluationContext] = None,
hooks: Optional[list[Hook]] = None,
) -> GeneratedClient:
if not client:
client = OpenFeatureClient(
domain=domain,
version=version,
context=context,
hooks=hooks,
)
return GeneratedClient(client)