-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from JOJ0/raw_cmd_and_common_opts
Add `raw` command, a new module for common CLI options, and improve `matrix raw` help
- Loading branch information
Showing
7 changed files
with
162 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ Command Line Reference | |
synadm.cli.matrix | ||
synadm.cli.regtok | ||
synadm.cli.notice | ||
synadm.cli.raw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Raw | ||
==== | ||
|
||
.. click:: synadm.cli.raw:raw_request_cmd | ||
:prog: synadm raw | ||
:nested: full |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# synadm | ||
# Copyright (C) 2020-2023 Johannes Tiefenbacher | ||
# | ||
# synadm is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# synadm is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
"Common CLI options, option groups, helpers and utilities." | ||
|
||
import click | ||
|
||
from click_option_group import MutuallyExclusiveOptionGroup | ||
|
||
|
||
def common_opts_raw_command(function): | ||
return click.argument( | ||
"endpoint", type=str | ||
)( | ||
click.option( | ||
"--method", "-m", | ||
type=click.Choice(["get", "post", "put", "delete"]), | ||
help="The HTTP method used for the request.", | ||
default="get", show_default=True | ||
)(function) | ||
) | ||
|
||
|
||
data_group_raw_command = MutuallyExclusiveOptionGroup( | ||
"Data", | ||
help="" | ||
) | ||
|
||
|
||
def data_opts_raw_command(function): | ||
return data_group_raw_command.option( | ||
"--data", "-d", type=str, default='{}', show_default=True, | ||
help="""The JSON string sent in the body of post, put and delete | ||
requests - provided as a string. Make sure to escape it from shell | ||
interpretation by using single quotes. E.g '{"key1": "value1", | ||
"key2": 123}'""" | ||
)( | ||
data_group_raw_command.option( | ||
"--data-file", "-f", type=click.File("rt"), | ||
show_default=True, | ||
help="""Read JSON data from file. To read from stdin use "-" as the | ||
filename argument.""" | ||
)(function) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
# synadm | ||
# Copyright (C) 2020-2023 Johannes Tiefenbacher | ||
# | ||
# synadm is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# synadm is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""This module holds the `raw` command only.""" | ||
|
||
import click | ||
|
||
from synadm import cli | ||
from synadm.cli._common import common_opts_raw_command, data_opts_raw_command | ||
|
||
|
||
@cli.root.command(name="raw") | ||
@common_opts_raw_command | ||
@data_opts_raw_command | ||
@click.pass_obj | ||
def raw_request_cmd(helper, endpoint, method, data, data_file): | ||
""" Issue a custom request to the Synapse Admin API. | ||
The endpoint argument is the part of the URL _after_ the configured | ||
"Synapse base URL" and "Synapse Admin API path" (see ``synadm config``). | ||
A get request to the "Query User Account API" would look like this: | ||
``synadm raw v2/users/%40testuser%3Aexample.org``. URL encoding must be | ||
handled at this point. Consider enabling debug outputs via synadm's global | ||
flag ``-vv`` | ||
""" | ||
if data_file: | ||
data = data_file.read() | ||
|
||
raw_request = helper.api.raw_request(endpoint, method, data) | ||
|
||
if helper.no_confirm: | ||
if raw_request is None: | ||
raise SystemExit(1) | ||
helper.output(raw_request) | ||
else: | ||
if raw_request is None: | ||
click.echo("The Admin API's response was empty or JSON data " | ||
"could not be loaded.") | ||
raise SystemExit(1) | ||
else: | ||
helper.output(raw_request) |