Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Add '@=' separator to pass JSON objects via CLI #386

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions hyper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
SEP_HEADERS = ':'
SEP_QUERY = '=='
SEP_DATA = '='
SEP_JSON = '@='

SEP_GROUP_ITEMS = [
SEP_JSON,
SEP_HEADERS,
SEP_QUERY,
SEP_DATA,
Expand Down Expand Up @@ -93,9 +95,13 @@ def make_positional_argument(parser):

search==hyper

'=' Data fields to be serialized into a JSON object:
'=' String data fields to be serialized into a JSON object:

name=Hyper language=Python description='CLI HTTP client'

'@=' JSON data fields

name='{"name": "John", "surname": "Doe"}' list='[1, 2, 3]'
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it have @= there too, like name@='{"name": "John", "surname": "Doe"}' list@='[1, 2, 3]', or am I missing something?

Copy link
Author

Choose a reason for hiding this comment

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

You're completely right, I'll fix that. Thanks for noticing

"""))


Expand Down Expand Up @@ -167,7 +173,13 @@ def set_url_info(args):
def set_request_data(args):
body, headers, params = {}, {}, {}
for i in args.items:
if i.sep == SEP_HEADERS:
if i.sep == SEP_JSON:
try:
value = json.loads(i.value)
body[i.key] = value
except ValueError:
log.warning('Unable to decode JSON, ignoring it (%s)', i.value)
elif i.sep == SEP_HEADERS:
if i.key:
headers[i.key] = i.value
else:
Expand Down
6 changes: 6 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,27 @@ def test_cli_with_system_exit(argv):
{'method': 'GET', 'url.path': '/?param=test'}),
(['POST', 'example.com', 'data=test'],
{'method': 'POST', 'body': '{"data": "test"}'}),
(['POST', 'example.com', 'data@={"json":[1,2,3]}'],
{'method': 'POST', 'body': '{"data": {"json": [1, 2, 3]}}'}),
(['GET', 'example.com', ':authority:example.org'],
{'method': 'GET', 'headers': {
':authority': 'example.org'}}),
(['GET', 'example.com', ':authority:example.org', 'x-test:header'],
{'method': 'GET', 'headers': {
':authority': 'example.org',
'x-test': 'header'}}),
(['POST', 'example.com', 'data@={"invalidjson":1,2,3}'],
{'body': None}),
], ids=[
'specified "--debug" option',
'specify host with lower get method',
'specified host and additional header',
'specified host and get parameter',
'specified host and post data',
'specified host and post JSON data',
'specified host and override default header',
'specified host and override default header and additional header',
'specified host and post invalid JSON data',
])
def test_parse_argument(argv, expected):
args = parse_argument(argv)
Expand Down