Skip to content

Commit 48eb465

Browse files
committed
Refactor OpenAPI basePath support added by #140
1 parent 5e4a40f commit 48eb465

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

http_prompt/cli.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,15 @@ 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', '')
117115
except json.JSONDecodeError:
118116
click.secho("Warning: Specification file '%s' is not JSON" %
119117
spec, err=True, fg='red')
120118
spec = None
121119
finally:
122120
f.close()
123121

124-
if url == '':
125-
url = 'http://localhost:8000'
126-
127-
url = fix_incomplete_url(url)
122+
if url:
123+
url = fix_incomplete_url(url)
128124
context = Context(url, spec=spec)
129125

130126
output_style = cfg.get('output_style')
@@ -149,8 +145,8 @@ def cli(spec, env, url, http_options):
149145
else:
150146
if env:
151147
load_context(context, env)
152-
if url != 'http://localhost:8000':
153-
# overwrite the env url if not default
148+
if url:
149+
# Overwrite the env url if not default
154150
context.url = url
155151

156152
if http_options:

http_prompt/context/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,32 @@ 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,
18+
if not self.url:
19+
schemes = spec.get('schemes')
20+
scheme = schemes[0] if schemes else 'https'
21+
self.url = (scheme + '://' +
22+
spec.get('host', 'http://localhost:8000') +
23+
spec.get('basePath', ''))
24+
25+
base_path_tokens = list(filter(lambda s: s,
1926
spec.get('basePath', '').split('/')))
2027
paths = spec.get('paths')
2128
if paths:
2229
for path in paths:
23-
path_tokens = list(filter(lambda s: s, path.split('/')))
24-
self.root.add_path(*(base_path + path_tokens))
30+
path_tokens = (base_path_tokens +
31+
list(filter(lambda s: s, path.split('/'))))
32+
self.root.add_path(*path_tokens)
2533
endpoint = paths[path]
2634
for method, info in endpoint.items():
2735
params = info.get('parameters')
2836
if params:
2937
for param in params:
3038
if param.get('in') != 'path':
31-
full_path = base_path \
32-
+ path_tokens \
33-
+ [param['name']]
39+
full_path = path_tokens + [param['name']]
3440
self.root.add_path(*full_path,
3541
node_type='file')
42+
elif not self.url:
43+
self.url = 'http://localhost:8000'
3644

3745
def __eq__(self, other):
3846
return (self.url == other.url and

tests/test_cli.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ def test_spec_from_local(self):
164164
self.assertEqual(set([n.name for n in context.root.children]),
165165
set(['users', 'orgs']))
166166

167+
def test_spec_basePath(self):
168+
spec_filepath = self.make_tempfile(json.dumps({
169+
'basePath': '/api/v1',
170+
'paths': {
171+
'/users': {},
172+
'/orgs': {}
173+
}
174+
}))
175+
result, context = run_and_exit(['example.com', "--spec",
176+
spec_filepath])
177+
self.assertEqual(result.exit_code, 0)
178+
self.assertEqual(context.url, 'http://example.com')
179+
180+
lv1_names = set([node.name for node in context.root.ls()])
181+
lv2_names = set([node.name for node in context.root.ls('api')])
182+
lv3_names = set([node.name for node in context.root.ls('api', 'v1')])
183+
184+
self.assertEqual(lv1_names, set(['api']))
185+
self.assertEqual(lv2_names, set(['v1']))
186+
self.assertEqual(lv3_names, set(['users', 'orgs']))
187+
167188
def test_spec_from_http(self):
168189
spec_url = 'https://api.apis.guru/v2/specs/github.com/v3/swagger.json'
169190
result, context = run_and_exit(['https://api.github.com', '--spec',
@@ -175,6 +196,19 @@ def test_spec_from_http(self):
175196
self.assertIn('repos', top_level_paths)
176197
self.assertIn('users', top_level_paths)
177198

199+
def test_spec_from_http_only(self):
200+
spec_url = (
201+
'https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.json')
202+
result, context = run_and_exit(['--spec', spec_url])
203+
self.assertEqual(result.exit_code, 0)
204+
self.assertEqual(context.url, 'https://api.medium.com/v1')
205+
206+
lv1_names = set([node.name for node in context.root.ls()])
207+
lv2_names = set([node.name for node in context.root.ls('v1')])
208+
209+
self.assertEqual(lv1_names, set(['v1']))
210+
self.assertEqual(lv2_names, set(['me', 'publications', 'users']))
211+
178212
def test_env_only(self):
179213
env_filepath = self.make_tempfile(
180214
"cd http://example.com\nname=bob\nid==10")

0 commit comments

Comments
 (0)