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

There needs to be a Optional[...] return type annotation for endpoints API #646

Closed
jpkrajewski opened this issue May 9, 2024 · 0 comments · Fixed by #683
Closed

There needs to be a Optional[...] return type annotation for endpoints API #646

jpkrajewski opened this issue May 9, 2024 · 0 comments · Fixed by #683
Assignees
Labels
enhancement New feature or request

Comments

@jpkrajewski
Copy link
Collaborator

Description

Some endpoints can return data and empty response payload.

For example this PR #477 is introducing URL Monitoring endpoints, but get endpoint can return Datasequence and None which is breaking the api.

Traceback (most recent call last):
  File "/home/jakub/work/demo/snadbox.py", line 65, in <module>
    response = session.endpoints.url_monitoring.get_url_monitor()
  File "/home/jakub/work/catalystwan/catalystwan/endpoints/__init__.py", line 568, in wrapper
    return response.dataseq(self.return_spec.payload_type, self.resp_json_key)
  File "/home/jakub/work/catalystwan/catalystwan/response.py", line 198, in dataseq
    return DataSequence(cls, [cls.model_validate(item) for item in sequence])  # type: ignore
  File "/home/jakub/work/catalystwan/catalystwan/response.py", line 198, in <listcomp>
    return DataSequence(cls, [cls.model_validate(item) for item in sequence])  # type: ignore
  File "/home/jakub/.cache/pypoetry/virtualenvs/catalystwan-UKVG-1nx-py3.10/lib/python3.10/site-packages/pydantic/main.py", line 551, in model_validate
    return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 1 validation error for UrlMonitoringInfo
  Input should be a valid dictionary or instance of UrlMonitoringInfo [type=model_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.7/v/model_type

Replication script (URL monitoring have to be empty)

with create_manager_session(
    **sjlaas40cpu19,
    logger=logger,
) as session:
    session.endpoints.url_monitoring.add_url_monitor(UrlMonitoringConfig(
        url="/client/server/ready",
        threshold=10,
    ))
    response = session.endpoints.url_monitoring.get_url_monitor()

We can't add the Optional field for the endpoint because we will get error

Traceback (most recent call last):
  File "/home/jakub/work/demo/snadbox.py", line 6, in <module>
    from catalystwan.endpoints.url_monitoring import UrlMonitoring
  File "/home/jakub/work/catalystwan/catalystwan/endpoints/url_monitoring.py", line 12, in <module>
    class UrlMonitoring(APIEndpoints):
  File "/home/jakub/work/catalystwan/catalystwan/endpoints/url_monitoring.py", line 14, in UrlMonitoring
    def get_url_monitor(self) -> Optional[DataSequence[UrlMonitoringInfo]]:
  File "/home/jakub/work/catalystwan/catalystwan/endpoints/__init__.py", line 529, in __call__
    self.return_spec = self.specify_return_type()
  File "/home/jakub/work/catalystwan/catalystwan/endpoints/__init__.py", line 393, in specify_return_type
    raise APIEndpointError(f"Expected: {ReturnType} but return type {annotation}")
catalystwan.exceptions.APIEndpointError: Expected: typing.Union[NoneType, str, int, float, bool, typing.Dict[str, ForwardRef('JSON')], typing.List[ForwardRef('JSON')], bytes, dict, pydantic.main.BaseModel, catalystwan.typed_list.DataSequence[pydantic.main.BaseModel]] but return type typing.Optional[catalystwan.typed_list.DataSequence[catalystwan.models.url_monitoring.UrlMonitoringInfo]]
@jpkrajewski jpkrajewski added the enhancement New feature or request label May 9, 2024
@smialy smialy self-assigned this May 20, 2024
@smialy smialy linked a pull request May 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants