diff options
author | Chang-Hung Liang <eliang.cs@gmail.com> | 2017-01-29 15:33:36 +0800 |
---|---|---|
committer | Chang-Hung Liang <eliang.cs@gmail.com> | 2017-01-29 15:33:36 +0800 |
commit | 850213c34e53e2920eefed58a445461b3b7f0548 (patch) | |
tree | 3bceaa139ff26ae5538b90e51885c8f485a8a0a6 | |
parent | 219bc351f7e0c71a6d95476bb0adc9b44fd1a292 (diff) |
-rw-r--r-- | http_prompt/cli.py | 17 | ||||
-rw-r--r-- | http_prompt/context/__init__.py | 3 | ||||
-rw-r--r-- | http_prompt/execution.py | 10 |
3 files changed, 26 insertions, 4 deletions
diff --git a/http_prompt/cli.py b/http_prompt/cli.py index 087487e..4d0403d 100644 --- a/http_prompt/cli.py +++ b/http_prompt/cli.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import json import os import sys @@ -15,6 +16,7 @@ from prompt_toolkit.styles.from_pygments import style_from_pygments from pygments.styles import get_style_by_name from pygments.util import ClassNotFound from six.moves.http_cookies import SimpleCookie +from six.moves.urllib.request import urlopen from . import __version__ from . import config @@ -73,10 +75,11 @@ class ExecutionListener(object): @click.command(context_settings=dict( ignore_unknown_options=True, )) +@click.option('--spec', help="API specification file in Swagger format.") @click.argument('url', default='http://localhost:8000') @click.argument('http_options', nargs=-1, type=click.UNPROCESSED) @click.version_option(message='%(version)s') -def cli(url, http_options): +def cli(spec, url, http_options): click.echo('Version: %s' % __version__) copied, config_path = config.initialize() @@ -90,8 +93,18 @@ def cli(url, http_options): os.environ['PAGER'] = cfg['pager'] os.environ['LESS'] = '-RXF' + if spec: + with urlopen(spec) as f: + content = f.read().decode('utf-8') + try: + spec = json.loads(content) + except json.JSONDecodeError: + click.secho("Warning: Specification file '%s' is not JSON" % + spec, err=True, fg='red') + pass + url = fix_incomplete_url(url) - context = Context(url) + context = Context(url, spec=spec) output_style = cfg.get('output_style') if output_style: diff --git a/http_prompt/context/__init__.py b/http_prompt/context/__init__.py index af363fa..d6ae2de 100644 --- a/http_prompt/context/__init__.py +++ b/http_prompt/context/__init__.py @@ -1,6 +1,6 @@ class Context(object): - def __init__(self, url=None): + def __init__(self, url=None, spec=None): self.url = url self.headers = {} self.querystring_params = {} @@ -8,6 +8,7 @@ class Context(object): self.body_json_params = {} self.options = {} self.should_exit = False + self.spec = spec def __eq__(self, other): return (self.url == other.url and diff --git a/http_prompt/execution.py b/http_prompt/execution.py index eb4cb1c..bbba42f 100644 --- a/http_prompt/execution.py +++ b/http_prompt/execution.py @@ -29,7 +29,7 @@ grammar = r""" command = mutation / immutation mutation = concat_mut+ / nonconcat_mut - immutation = preview / action / env / help / exit / exec / source / _ + immutation = preview / action / ls / env / help / exit / exec / source / _ concat_mut = option_mut / full_quoted_mut / value_quoted_mut / unquoted_mut nonconcat_mut = cd / rm @@ -41,6 +41,7 @@ grammar = r""" help = _ "help" _ exit = _ "exit" _ + ls = _ "ls" _ (redir_out)? env = _ "env" _ (redir_out)? source = _ "source" _ filepath _ exec = _ "exec" _ filepath _ @@ -300,6 +301,13 @@ class ExecutionVisitor(NodeVisitor): execute(line, self.context, self.listener) return node + def visit_ls(self, node, chlidren): + if self.context.spec: + paths = self.context.spec.get('paths') + for path in paths: + self.output.write(path + '\n') + return node + def visit_env(self, node, children): text = format_to_http_prompt(self.context) self.output.write(text) |