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

Add REST API to ArduPilotManager #119

Conversation

rafaellehmkuhl
Copy link
Member

@rafaellehmkuhl rafaellehmkuhl commented Mar 19, 2021

API is running as a uvicorn process, started with core.

core/services/ardupilot_manager/rest_api.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/rest_api.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/rest_api.py Outdated Show resolved Hide resolved
core/start-companion-core Outdated Show resolved Hide resolved
core/services/ardupilot_manager/setup.py Show resolved Hide resolved
@rafaellehmkuhl rafaellehmkuhl force-pushed the add-rest-api-ardupilot-manager branch 2 times, most recently from 1846a28 to 7b7d805 Compare March 22, 2021 20:01
core/services/ardupilot_manager/main.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/main.py Outdated Show resolved Hide resolved
@rafaellehmkuhl rafaellehmkuhl force-pushed the add-rest-api-ardupilot-manager branch 3 times, most recently from 028ae13 to 237ece0 Compare March 26, 2021 00:34
Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small review, will do a deeper one tomorrow

core/services/ardupilot_manager/main.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/main.py Outdated Show resolved Hide resolved
Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did run python setup.py install and getting:

./main.py                                                         
Traceback (most recent call last):
  File "/home/patrick/git/blue/companion-docker/core/services/ardupilot_manager/./main.py", line 7, in <module>
    from fastapi import Body, FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/applications.py", line 18, in <module>
    from fastapi.openapi.utils import get_openapi
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/openapi/utils.py", line 17, in <module>
    from fastapi.responses import Response
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse as UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse' from 'starlette.responses' (/home/patrick/.local/lib/python3.9/site-packages/starlette/responses.py)

@rafaellehmkuhl
Copy link
Member Author

rafaellehmkuhl commented Mar 26, 2021

I did run python setup.py install and getting:

./main.py                                                         
Traceback (most recent call last):
  File "/home/patrick/git/blue/companion-docker/core/services/ardupilot_manager/./main.py", line 7, in <module>
    from fastapi import Body, FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/applications.py", line 18, in <module>
    from fastapi.openapi.utils import get_openapi
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/openapi/utils.py", line 17, in <module>
    from fastapi.responses import Response
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse as UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse' from 'starlette.responses' (/home/patrick/.local/lib/python3.9/site-packages/starlette/responses.py)

Could you run pip list and send it here?

Edit: there was a change in starlette and they removed UJSONResponse in the newest version, but this should not occur here, since FastAPI froze starlette's version on 0.13.6 for backwards compatibility.

@patrickelectric
Copy link
Member

I did run python setup.py install and getting:

./main.py                                                         
Traceback (most recent call last):
  File "/home/patrick/git/blue/companion-docker/core/services/ardupilot_manager/./main.py", line 7, in <module>
    from fastapi import Body, FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/applications.py", line 18, in <module>
    from fastapi.openapi.utils import get_openapi
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/openapi/utils.py", line 17, in <module>
    from fastapi.responses import Response
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse as UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse' from 'starlette.responses' (/home/patrick/.local/lib/python3.9/site-packages/starlette/responses.py)

Could you run pip list and send it here?

Edit: there was a change in starlette and they removed UJSONResponse in the newest version, but this should not occur here, since FastAPI froze starlette's version on 0.13.6 for backwards compatibility.

image

@rafaellehmkuhl
Copy link
Member Author

I did run python setup.py install and getting:

./main.py                                                         
Traceback (most recent call last):
  File "/home/patrick/git/blue/companion-docker/core/services/ardupilot_manager/./main.py", line 7, in <module>
    from fastapi import Body, FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/applications.py", line 18, in <module>
    from fastapi.openapi.utils import get_openapi
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/openapi/utils.py", line 17, in <module>
    from fastapi.responses import Response
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse as UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse' from 'starlette.responses' (/home/patrick/.local/lib/python3.9/site-packages/starlette/responses.py)

Could you run pip list and send it here?
Edit: there was a change in starlette and they removed UJSONResponse in the newest version, but this should not occur here, since FastAPI froze starlette's version on 0.13.6 for backwards compatibility.

image

There's probably some other package in your environment taking starlette to an upper version.
The github action is installing the correct one, so the build is fine:

Best match: starlette 0.13.6
Processing starlette-0.13.6-py3-none-any.whl
Installing starlette-0.13.6-py3-none-any.whl to /home/runner/.local/lib/python3.7/site-packages
Adding starlette 0.13.6 to easy-install.pth file

I suggest using a dedicated python virtual environment to isolate package's versions or downgrading your version of starlette to 0.13.6.

@patrickelectric
Copy link
Member

I did run python setup.py install and getting:

./main.py                                                         
Traceback (most recent call last):
  File "/home/patrick/git/blue/companion-docker/core/services/ardupilot_manager/./main.py", line 7, in <module>
    from fastapi import Body, FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/__init__.py", line 7, in <module>
    from .applications import FastAPI as FastAPI
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/applications.py", line 18, in <module>
    from fastapi.openapi.utils import get_openapi
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/openapi/utils.py", line 17, in <module>
    from fastapi.responses import Response
  File "/home/patrick/.local/lib/python3.9/site-packages/fastapi-0.63.0-py3.9.egg/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse as UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse' from 'starlette.responses' (/home/patrick/.local/lib/python3.9/site-packages/starlette/responses.py)

Could you run pip list and send it here?
Edit: there was a change in starlette and they removed UJSONResponse in the newest version, but this should not occur here, since FastAPI froze starlette's version on 0.13.6 for backwards compatibility.

image

There's probably some other package in your environment taking starlette to an upper version.
The github action is installing the correct one, so the build is fine:

Best match: starlette 0.13.6
Processing starlette-0.13.6-py3-none-any.whl
Installing starlette-0.13.6-py3-none-any.whl to /home/runner/.local/lib/python3.7/site-packages
Adding starlette 0.13.6 to easy-install.pth file

I suggest using a dedicated python virtual environment to isolate package's versions or downgrading your version of starlette to 0.13.6.

Moved to a virtualenv solves the problem.

Copy link
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you post an endpoint that already exists, it returns false but with a number code of 200.
Is that the expected behaviour ? Should we return 200 or true/false at all ? Maybe 409 (Conflict) ?

core/services/ardupilot_manager/mavlink_proxy/Endpoint.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/mavlink_proxy/Endpoint.py Outdated Show resolved Hide resolved
core/services/ardupilot_manager/mavlink_proxy/test_all.py Outdated Show resolved Hide resolved
mypy.ini Show resolved Hide resolved
This is exactly what IntEnum does, but for strings.
In this way, json files and rest frameworks (like FastAPI) represent them as strings.
@rafaellehmkuhl rafaellehmkuhl requested review from patrickelectric and jaxxzer and removed request for jaxxzer March 29, 2021 14:38
if "/" in place:
raise ValueError(f"Bad filename: {place}. Valid filenames do not contain '/' characters.")
if argument is not None:
raise ValueError(f"File endpoint has should have no argument. Received {argument}.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File endpoint has should have no argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solved.

@@ -79,14 +79,11 @@ def start_navigator(self) -> None:
# does not accept TCP master endpoints
# self.start_mavlink_manager(Endpoint(local_endpoint))

# Check if subprocess is running and wait until it finishes
# Necessary since we don't have mavlink_manager running for navigator yet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor detail, you don't need to fix it, but these comments should have come out in the previous commit :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!
Solved!

- Make it a pydantic dataclass, for REST API validation auto-parsing
- Adds validation to all arguments
- Add dependency for endpoint validation
- Add mypy plugin through configuration file
Initial version contains routes for getting, adding and deleting mavlink endpoints.
@patrickelectric patrickelectric merged commit 1b59af7 into bluerobotics:master Mar 30, 2021
@rafaellehmkuhl rafaellehmkuhl deleted the add-rest-api-ardupilot-manager branch March 31, 2021 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants