Permalink
Browse files

Refactor OpenAPI basePath support added by #140

  • Loading branch information...
eliangcs committed Feb 24, 2018
1 parent 5e4a40f commit 48eb465ffbed7abd6654f1775bc696d3f5de834c
Showing with 52 additions and 14 deletions.
  1. +4 −8 http_prompt/cli.py
  2. +14 −6 http_prompt/context/__init__.py
  3. +34 −0 tests/test_cli.py
View
@@ -112,19 +112,15 @@ def cli(spec, env, url, http_options):
content = f.read().decode('utf-8')
try:
spec = json.loads(content)
if url == '' and spec:
url = spec.get('host', '') + spec.get('basePath', '')
except json.JSONDecodeError:
click.secho("Warning: Specification file '%s' is not JSON" %
spec, err=True, fg='red')
spec = None
finally:
f.close()
if url == '':
url = 'http://localhost:8000'
url = fix_incomplete_url(url)
if url:
url = fix_incomplete_url(url)
context = Context(url, spec=spec)
output_style = cfg.get('output_style')
@@ -149,8 +145,8 @@ def cli(spec, env, url, http_options):
else:
if env:
load_context(context, env)
if url != 'http://localhost:8000':
# overwrite the env url if not default
if url:
# Overwrite the env url if not default
context.url = url
if http_options:
@@ -15,24 +15,32 @@ def __init__(self, url=None, spec=None):
# Create a tree for supporting API spec and ls command
self.root = Node('root')
if spec:
base_path = list(filter(lambda s: s,
if not self.url:
schemes = spec.get('schemes')
scheme = schemes[0] if schemes else 'https'
self.url = (scheme + '://' +
spec.get('host', 'http://localhost:8000') +
spec.get('basePath', ''))
base_path_tokens = list(filter(lambda s: s,
spec.get('basePath', '').split('/')))
paths = spec.get('paths')
if paths:
for path in paths:
path_tokens = list(filter(lambda s: s, path.split('/')))
self.root.add_path(*(base_path + path_tokens))
path_tokens = (base_path_tokens +
list(filter(lambda s: s, path.split('/'))))
self.root.add_path(*path_tokens)
endpoint = paths[path]
for method, info in endpoint.items():
params = info.get('parameters')
if params:
for param in params:
if param.get('in') != 'path':
full_path = base_path \
+ path_tokens \
+ [param['name']]
full_path = path_tokens + [param['name']]
self.root.add_path(*full_path,
node_type='file')
elif not self.url:
self.url = 'http://localhost:8000'
def __eq__(self, other):
return (self.url == other.url and
View
@@ -164,6 +164,27 @@ def test_spec_from_local(self):
self.assertEqual(set([n.name for n in context.root.children]),
set(['users', 'orgs']))
def test_spec_basePath(self):
spec_filepath = self.make_tempfile(json.dumps({
'basePath': '/api/v1',
'paths': {
'/users': {},
'/orgs': {}
}
}))
result, context = run_and_exit(['example.com', "--spec",
spec_filepath])
self.assertEqual(result.exit_code, 0)
self.assertEqual(context.url, 'http://example.com')
lv1_names = set([node.name for node in context.root.ls()])
lv2_names = set([node.name for node in context.root.ls('api')])
lv3_names = set([node.name for node in context.root.ls('api', 'v1')])
self.assertEqual(lv1_names, set(['api']))
self.assertEqual(lv2_names, set(['v1']))
self.assertEqual(lv3_names, set(['users', 'orgs']))
def test_spec_from_http(self):
spec_url = 'https://api.apis.guru/v2/specs/github.com/v3/swagger.json'
result, context = run_and_exit(['https://api.github.com', '--spec',
@@ -175,6 +196,19 @@ def test_spec_from_http(self):
self.assertIn('repos', top_level_paths)
self.assertIn('users', top_level_paths)
def test_spec_from_http_only(self):
spec_url = (
'https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.json')
result, context = run_and_exit(['--spec', spec_url])
self.assertEqual(result.exit_code, 0)
self.assertEqual(context.url, 'https://api.medium.com/v1')
lv1_names = set([node.name for node in context.root.ls()])
lv2_names = set([node.name for node in context.root.ls('v1')])
self.assertEqual(lv1_names, set(['v1']))
self.assertEqual(lv2_names, set(['me', 'publications', 'users']))
def test_env_only(self):
env_filepath = self.make_tempfile(
"cd http://example.com\nname=bob\nid==10")

0 comments on commit 48eb465

Please sign in to comment.