Skip to content

Commit

Permalink
[feature] Add api method for python scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Zarubin committed Nov 25, 2024
1 parent 7a959a9 commit 1ec8e06
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
24 changes: 22 additions & 2 deletions aurora_cli/src/api/group_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import io
import json
import sys

import click
Expand Down Expand Up @@ -346,6 +347,25 @@ def group_api(route: str):
if argv_is_test():
sys.argv.append('api')
AppConfig.create_test()
# Run api
common_api(route)


# Using api like lib
def aurora_cli_api(route: str):
sys.argv.append('api')
sys.argv.append('_lib')
result = []
_stdout = sys.stdout
sys.stdout = _stringio = io.StringIO()
common_api(route)
result.extend(_stringio.getvalue().splitlines())
del _stringio
sys.stdout = _stdout
return json.loads(''.join(result))


def common_api(route: str):
try:
for func in [
search_route_app,
Expand All @@ -361,7 +381,7 @@ def group_api(route: str):
]:
if func(route):
echo_verbose(get_arg_bool(route, 'verbose'))
exit(0)
return

echo_stdout(OutResultError(TextError.route_not_found()))
except Exception as e:
Expand Down
6 changes: 6 additions & 0 deletions aurora_cli/src/base/utils/argv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def argv_is_api() -> bool:
return False


def argv_is_lib() -> bool:
if '_lib' in sys.argv:
return True
return False


def argv_is_emulator_recording() -> bool:
if 'emulator' in sys.argv and 'recording' in sys.argv:
return True
Expand Down
12 changes: 12 additions & 0 deletions scripts/examples/aurora_cli_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python3

from aurora_cli.src.api.group_api import aurora_cli_api


def main():
result = aurora_cli_api('/app/info')
print(result)


if __name__ == '__main__':
main()

0 comments on commit 1ec8e06

Please sign in to comment.