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

View File

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

View File

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