This commit is contained in:
66
src/utils/search.py
Normal file
66
src/utils/search.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from typing import List, Dict, Any
|
||||
from ..core.parser import load_spec_file
|
||||
|
||||
|
||||
def search_endpoints(spec_path: str, query: str, limit: int = 10) -> List[Dict[str, Any]]:
|
||||
"""Search for endpoints matching the query.
|
||||
|
||||
Args:
|
||||
spec_path: Path to the OpenAPI spec file
|
||||
query: Search query string
|
||||
limit: Maximum number of results to return
|
||||
|
||||
Returns:
|
||||
List of matching endpoints
|
||||
"""
|
||||
spec = load_spec_file(spec_path)
|
||||
results = []
|
||||
query_lower = query.lower()
|
||||
|
||||
for path, methods in spec.get('paths', {}).items():
|
||||
for method, details in methods.items():
|
||||
if method.lower() not in ['get', 'post', 'put', 'delete', 'patch', 'options', 'head']:
|
||||
continue
|
||||
|
||||
match_score = 0
|
||||
matches = []
|
||||
|
||||
path_match = query_lower in path.lower()
|
||||
if path_match:
|
||||
match_score += 10
|
||||
matches.append(f"Path: {path}")
|
||||
|
||||
summary = details.get('summary', '') or ''
|
||||
if query_lower in summary.lower():
|
||||
match_score += 5
|
||||
matches.append(f"Summary: {summary}")
|
||||
|
||||
description = details.get('description', '') or ''
|
||||
if query_lower in description.lower():
|
||||
match_score += 3
|
||||
matches.append(f"Description: {description[:100]}...")
|
||||
|
||||
tags = details.get('tags', [])
|
||||
for tag in tags:
|
||||
if query_lower in tag.lower():
|
||||
match_score += 4
|
||||
matches.append(f"Tag: {tag}")
|
||||
|
||||
operation_id = details.get('operationId', '') or ''
|
||||
if query_lower in operation_id.lower():
|
||||
match_score += 2
|
||||
|
||||
if match_score > 0:
|
||||
results.append({
|
||||
'path': path,
|
||||
'method': method.upper(),
|
||||
'summary': details.get('summary'),
|
||||
'description': details.get('description'),
|
||||
'tags': tags,
|
||||
'operation_id': operation_id,
|
||||
'score': match_score,
|
||||
'matches': matches
|
||||
})
|
||||
|
||||
results.sort(key=lambda x: x['score'], reverse=True)
|
||||
return results[:limit]
|
||||
Reference in New Issue
Block a user