Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit de0dfe0

Browse files
committed
Support for file arguments in command line client
1 parent 458c004 commit de0dfe0

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

coreapi/codecs/jsondata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
from coreapi.codecs.base import BaseCodec
33
from coreapi.exceptions import ParseError
4+
import collections
45
import json
56

67

@@ -12,6 +13,6 @@ def load(self, bytes, base_url=None):
1213
Return raw JSON data.
1314
"""
1415
try:
15-
return json.loads(bytes.decode('utf-8'))
16+
return json.loads(bytes.decode('utf-8'), object_pairs_hook=collections.OrderedDict)
1617
except ValueError as exc:
1718
raise ParseError('Malformed JSON. %s' % exc)

coreapi/commandline.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,32 +206,33 @@ def show(path):
206206
click.echo(display(doc))
207207

208208

209-
def validate_params(ctx, param, value):
210-
if any(['=' not in item for item in value]):
211-
raise click.BadParameter('Parameters need to be in format <field name>=<value>')
212-
213-
# Convert to dict.
214-
params = dict([tuple(item.split('=', 1)) for item in value])
209+
def parse_json(ctx, param, value):
210+
ret = []
215211

216-
# Gracefully handle non-string values.
217-
# Eg treat "-p complete=true" as {"complete": True}
218-
for key, value in params.items():
212+
for field, data in value:
219213
try:
220-
value = json.loads(value)
214+
pair = (field, json.loads(data))
221215
except:
222-
pass
223-
else:
224-
params[key] = value
216+
raise click.BadParameter('Invalid JSON for data argument "%s"' % field)
217+
ret.append(pair)
225218

226-
return params
219+
return ret
227220

228221

229-
@click.command(help='Interact with the active document.\n\nRequires a PATH to a link in the document.')
222+
@click.command(help='Interact with the active document.\n\nRequires a PATH to a link in the document.\n\nExample:\n\ncoreapi action users add_user --str username tom --data is_admin true')
230223
@click.argument('path', nargs=-1)
231-
@click.option('--param', '-p', multiple=True, callback=validate_params, help='Parameter in the form <field name>=<value>.')
232-
@click.option('--action', '-a', help='Set the link action explicitly.', default=None)
233-
@click.option('--transform', '-t', help='Set the linke transform explicitly.', default=None)
234-
def action(path, param, action, transform):
224+
@click.option('strings', '--str', '-s', type=(unicode, unicode), multiple=True, metavar="FIELD STRING", help='String parameter for the action.')
225+
@click.option('data', '--data', '-d', type=(unicode, unicode), multiple=True, callback=parse_json, metavar="FIELD DATA", help='Data parameter for the action.')
226+
@click.option('files', '--file', '-f', type=(unicode, click.File('rb')), multiple=True, metavar="FIELD FILENAME", help='File parameter for the action.')
227+
@click.option('--action', '-a', metavar="ACTION", help='Set the link action explicitly.', default=None)
228+
@click.option('--encoding', '-e', metavar="ENCODING", help='Set the link encoding explicitly.', default=None)
229+
@click.option('--transform', '-t', metavar="TRANSFORM", help='Set the link transform explicitly.', default=None)
230+
def action(path, strings, data, files, action, encoding, transform):
231+
params = {}
232+
params.update(dict(strings))
233+
params.update(dict(data))
234+
params.update(dict(files))
235+
235236
if not path:
236237
click.echo('Missing PATH to a link in the document.')
237238
sys.exit(1)
@@ -245,7 +246,10 @@ def action(path, param, action, transform):
245246
history = get_history()
246247
keys = coerce_key_types(doc, path)
247248
try:
248-
doc = client.action(doc, keys, params=param, action=action, transform=transform)
249+
doc = client.action(
250+
doc, keys, params=params,
251+
action=action, encoding=encoding, transform=transform
252+
)
249253
except coreapi.exceptions.ErrorMessage as exc:
250254
click.echo(display(exc.error))
251255
sys.exit(1)

coreapi/transports/http.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _get_method(action):
2525

2626
def _get_params(method, fields, params=None):
2727
"""
28-
Separate the params into their location types: path, query, or form.
28+
Separate the params into their location types.
2929
"""
3030
if params is None:
3131
return empty_params
@@ -117,11 +117,11 @@ def _get_content_type(file_obj):
117117
"""
118118
When a raw file upload is made, determine a content-type where possible.
119119
"""
120-
content_type = getattr(file_obj, 'content_type')
121-
if content_type is None:
122-
name = getattr(file_obj, 'name')
123-
if name is not None:
124-
content_type, encoding = mimetypes.guess_type(name)
120+
name = getattr(file_obj, 'name', None)
121+
if name is not None:
122+
content_type, encoding = mimetypes.guess_type(name)
123+
else:
124+
content_type = None
125125
return content_type
126126

127127

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy