Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor OpenAPI basePath support added by #140 #141

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions http_prompt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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:
Expand Down
20 changes: 14 additions & 6 deletions http_prompt/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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")
Expand Down