Skip to content

Commit 5e4a40f

Browse files
authored
Merge pull request #140 from nheisterkamp/master
Use host and basePath from OpenAPI spec
2 parents c31c891 + 977f329 commit 5e4a40f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

http_prompt/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def normalize_url(ctx, param, value):
8989
callback=normalize_url)
9090
@click.option('--env', help="Environment file to preload.",
9191
type=click.Path(exists=True))
92-
@click.argument('url', default='http://localhost:8000')
92+
@click.argument('url', default='')
9393
@click.argument('http_options', nargs=-1, type=click.UNPROCESSED)
9494
@click.version_option(message='%(version)s')
9595
def cli(spec, env, url, http_options):
@@ -112,13 +112,18 @@ def cli(spec, env, url, http_options):
112112
content = f.read().decode('utf-8')
113113
try:
114114
spec = json.loads(content)
115+
if url == '' and spec:
116+
url = spec.get('host', '') + spec.get('basePath', '')
115117
except json.JSONDecodeError:
116118
click.secho("Warning: Specification file '%s' is not JSON" %
117119
spec, err=True, fg='red')
118120
spec = None
119121
finally:
120122
f.close()
121123

124+
if url == '':
125+
url = 'http://localhost:8000'
126+
122127
url = fix_incomplete_url(url)
123128
context = Context(url, spec=spec)
124129

http_prompt/context/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ def __init__(self, url=None, spec=None):
1515
# Create a tree for supporting API spec and ls command
1616
self.root = Node('root')
1717
if spec:
18+
base_path = list(filter(lambda s: s,
19+
spec.get('basePath', '').split('/')))
1820
paths = spec.get('paths')
1921
if paths:
2022
for path in paths:
2123
path_tokens = list(filter(lambda s: s, path.split('/')))
22-
self.root.add_path(*path_tokens)
24+
self.root.add_path(*(base_path + path_tokens))
2325
endpoint = paths[path]
2426
for method, info in endpoint.items():
2527
params = info.get('parameters')
2628
if params:
2729
for param in params:
2830
if param.get('in') != 'path':
29-
full_path = path_tokens + [param['name']]
31+
full_path = base_path \
32+
+ path_tokens \
33+
+ [param['name']]
3034
self.root.add_path(*full_path,
3135
node_type='file')
3236

0 commit comments

Comments
 (0)