Skip to content

Commit

Permalink
separate wording for optional and required path params
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed May 26, 2024
1 parent 9e439c4 commit c24a34b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 3 additions & 1 deletion dlt_init_openapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class Config(BaseModel):
"""Which class to use for detecting"""
global_limit: int = 0
"""Set a limit on how many items are emitted from a resource"""
parameter_default_value: str = "FILL_ME_IN"
required_parameter_default_value: str = "FILL_ME_IN"
"""default to render for required parameters that do not have a default in the spec"""
unrequired_parameter_default_value: str = "OPTIONAL_CONFIG"
"""default to render for unrequired parameters that do not have a default in the spec"""
allow_openapi_2: bool = False
"""Allow to use OpenAPI 2 specs"""

Expand Down
33 changes: 21 additions & 12 deletions dlt_init_openapi/parser/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,36 @@ def unresolvable_path_param_names(self) -> List[str]:
return [p for p in params if p != transformer_param]

@property
def unresolvable_query_param_names(self) -> List[str]:
def unresolvable_query_params(self) -> List[Parameter]:
"""returns a list of required query param names with params that are used by the paginator excluded"""
print("HERE")
paginator_params = self.detected_pagination.param_names if self.detected_pagination else []
query_param_names = []
query_params: List[Parameter] = []
for param in self.list_all_parameters:
if param.name not in paginator_params and param.location == "query":
query_param_names.append(param.name)
return query_param_names
query_params.append(param)
return query_params

def is_query_param_required(self, param_name: str) -> bool:
for key, p in self.parameters.items():
if p.name == param_name and p.location == "query":
return p.required
return False
@property
def unresolvable_required_query_param_names(self) -> List[str]:
"""returns a list of required query param names with params that are used by the paginator excluded"""
return [param.name for param in self.unresolvable_query_params if param.required]

@property
def unresolvable_unrequired_query_param_names(self) -> List[str]:
return [param.name for param in self.unresolvable_query_params if not param.required]

def default_for_param(self, location: Literal["path", "query"], param_name: str) -> str:
"""get's a default value for the given param, returns"""
param: Parameter = None
for key, p in self.parameters.items():
if p.name == param_name and p.location == location and p.default:
return p.default
return self.context.config.parameter_default_value
if p.name == param_name and p.location == location:
param = p
if param and param.default:
return param.default
if not param or param.required or location == "path":
return self.context.config.required_parameter_default_value
return self.context.config.unrequired_parameter_default_value

@property
def render_description(self) -> Optional[str]:
Expand Down
15 changes: 11 additions & 4 deletions dlt_init_openapi/renderer/default/templates/source.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def {{ source_name }}(
"data_selector": "{{ endpoint.data_json_path }}",
{% endif %}
"path": "{{endpoint.path }}",
{% if endpoint.transformer or endpoint.unresolvable_path_param_names or endpoint.unresolvable_query_param_names %}
{% if endpoint.transformer or endpoint.unresolvable_path_param_names or endpoint.unresolvable_query_params %}
"params": {
{% if endpoint.transformer %}
{% for key, value in endpoint.transformer.path_params_mapping.items()%}
Expand All @@ -72,11 +72,18 @@ def {{ source_name }}(
{% endfor %}
{% endif %}
{% for param_name in endpoint.unresolvable_path_param_names %}
"{{param_name}}": "{{ endpoint.default_for_param('path', param_name) }}", # TODO: fill in path parameter
"{{param_name}}": "{{ endpoint.default_for_param('path', param_name) }}", # TODO: fill in required path parameter
{% endfor %}
{% for param_name in endpoint.unresolvable_query_param_names %}
{% if not endpoint.is_query_param_required(param_name) %}# {% endif %}"{{param_name}}": "{{ endpoint.default_for_param('query', param_name) }}", # TODO: fill in query parameter
{% for param_name in endpoint.unresolvable_required_query_param_names %}
"{{param_name}}": "{{ endpoint.default_for_param('query', param_name) }}", # TODO: fill in required query parameter
{% endfor %}
{% if endpoint.unresolvable_unrequired_query_param_names %}
# the parameters below can optionally be configured
{% for param_name in endpoint.unresolvable_unrequired_query_param_names %}
# "{{param_name}}": "{{ endpoint.default_for_param('query', param_name) }}",
{% endfor %}
{% endif %}

},
{% endif %}
{% if endpoint.render_pagination_args %}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/basics/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dlt_init_openapi.config import Config
from tests.integration.utils import get_indexed_resources

DEFAULT_VALUE = Config().parameter_default_value
DEFAULT_VALUE = Config().required_parameter_default_value


@pytest.fixture(scope="module")
Expand Down

0 comments on commit c24a34b

Please sign in to comment.