Skip to content

Commit

Permalink
feat: Support for 'servers' in openapi
Browse files Browse the repository at this point in the history
This commit also fixes a test with the previous spec file
(https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.json) that is
no longer available.

Signed-off-by: Arturo Volpe <[email protected]>
  • Loading branch information
aVolpe committed Apr 24, 2023
1 parent 6602b81 commit 8e873b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
14 changes: 9 additions & 5 deletions http_prompt/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ def __init__(self, url=None, spec=None):
self.root = Node('root')
if spec:
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', ''))
self.url = spec.get('servers')[0].get('url')
if 'servers' in spec:
self.url = spec.get('servers')[0].get('url')
else:
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('/')))
Expand Down
10 changes: 5 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,16 @@ def test_spec_from_http(self):

def test_spec_from_http_only(self):
spec_url = (
'https://api.apis.guru/v2/specs/medium.com/1.0.0/swagger.json')
'https://api.apis.guru/v2/specs/medium.com/1.0/openapi.json')
result, context = run_and_exit(['--spec', spec_url])
self.assertEqual(result.exit_code, 0)
self.assertEqual(context.url, 'https://api.medium.com/v1')
self.assertEqual(context.url, 'https://medium2.p.rapidapi.com')

lv1_names = set([node.name for node in context.root.ls()])
lv2_names = set([node.name for node in context.root.ls('v1')])
lv2_names = set([node.name for node in context.root.ls('article')])

self.assertEqual(lv1_names, set(['v1']))
self.assertEqual(lv2_names, set(['me', 'publications', 'users']))
self.assertEqual(lv1_names, set(['/', 'topfeeds', 'related_tags', 'user', 'article', 'list', 'latestposts', 'search', 'top_writer', 'publication']))
self.assertEqual(lv2_names, set(['{article_id}']))

def test_spec_with_trailing_slash(self):
spec_filepath = self.make_tempfile(json.dumps({
Expand Down

0 comments on commit 8e873b8

Please sign in to comment.