Added the ability to pass query string parameters.

This commit is contained in:
Jake Basile 2012-07-18 20:44:09 -05:00
parent c2d70e2bb1
commit 06ea36aaa4
3 changed files with 17 additions and 5 deletions

View File

@ -41,6 +41,7 @@ def _get_response(args):
# the `Content-Type` for us.
args.headers['Content-Type'] = TYPE_FORM
# Fire the request.
try:
credentials = None
@ -61,6 +62,7 @@ def _get_response(args):
proxies=dict((p.key, p.value) for p in args.proxy),
files=args.files,
allow_redirects=args.allow_redirects,
params=args.queries,
)
except (KeyboardInterrupt, SystemExit):

View File

@ -203,6 +203,7 @@ parser.add_argument(
metavar='ITEM',
type=cliparse.KeyValueType(
cliparse.SEP_COMMON,
cliparse.SEP_QUERY,
cliparse.SEP_DATA,
cliparse.SEP_DATA_RAW_JSON,
cliparse.SEP_FILES

View File

@ -25,6 +25,7 @@ SEP_HEADERS = SEP_COMMON
SEP_DATA = '='
SEP_DATA_RAW_JSON = ':='
SEP_FILES = '@'
SEP_QUERY = '=:'
DATA_ITEM_SEPARATORS = [
SEP_DATA,
SEP_DATA_RAW_JSON,
@ -102,6 +103,7 @@ class Parser(argparse.ArgumentParser):
item = KeyValueType(
SEP_COMMON,
SEP_QUERY,
SEP_DATA,
SEP_DATA_RAW_JSON,
SEP_FILES).__call__(args.url)
@ -118,16 +120,17 @@ class Parser(argparse.ArgumentParser):
def _parse_items(self, args):
"""
Parse `args.items` into `args.headers`, `args.data` and `args.files`.
Parse `args.items` into `args.headers`, `args.data`, `args.queries`, and `args.files`.
"""
args.headers = CaseInsensitiveDict()
args.headers['User-Agent'] = DEFAULT_UA
args.data = OrderedDict()
args.files = OrderedDict()
args.queries = CaseInsensitiveDict()
try:
parse_items(items=args.items, headers=args.headers,
data=args.data, files=args.files)
data=args.data, files=args.files, queries=args.queries)
except ParseError as e:
if args.traceback:
raise
@ -207,6 +210,8 @@ class KeyValueType(object):
if start >= estart and end <= eend:
inside_escape = True
break
if start in found and len(found[start]) > len(sep):
break
if not inside_escape:
found[start] = sep
@ -264,19 +269,23 @@ class AuthCredentialsType(KeyValueType):
)
def parse_items(items, data=None, headers=None, files=None):
"""Parse `KeyValueType` `items` into `data`, `headers` and `files`."""
def parse_items(items, data=None, headers=None, files=None, queries=None):
"""Parse `KeyValueType` `items` into `data`, `headers`, `files`, and `queries`."""
if headers is None:
headers = {}
if data is None:
data = {}
if files is None:
files = {}
if queries is None:
queries = {}
for item in items:
value = item.value
key = item.key
if item.sep == SEP_HEADERS:
target = headers
elif item.sep == SEP_QUERY:
target = queries
elif item.sep == SEP_FILES:
try:
value = open(os.path.expanduser(item.value), 'r')
@ -301,4 +310,4 @@ def parse_items(items, data=None, headers=None, files=None):
target[key] = value
return headers, data, files
return headers, data, files, queries