client.py•2.54 kB
"""
UseKeen API Client
This module provides a client for interacting with the UseKeen API,
which allows searching for package documentation.
"""
import json
import logging
from typing import Any, Dict, Optional, Union
import requests
logger = logging.getLogger("usekeen_mcp.client")
class UseKeenClient:
"""
Client for interacting with the UseKeen API.
This class handles authentication and making requests to the API
for searching package documentation.
"""
def __init__(self, api_key: str) -> None:
"""
Create a new UseKeenClient.
Args:
api_key: The API key for authenticating with the UseKeen API
"""
self.api_key = api_key
self.base_url = "https://usekeen-api-283956349806.us-central1.run.app"
def search_package_documentation(
self, package_name: str, query: Optional[str] = None
) -> Dict[str, Any]:
"""
Search for documentation of a package.
Args:
package_name: The name of the package to search for
query: Optional search term to find specific information
Returns:
The search results from the UseKeen API
Raises:
Exception: If the API request fails
"""
try:
# Create URL with query parameters for the API key
url = f"{self.base_url}/tools/package_doc_search"
# Log the request details for debugging
logger.debug(f"API Request URL: {url}")
# Create the request body with direct parameters
request_body = {
"package_name": package_name,
"query": query or ""
}
logger.debug(f"API Request Body: {json.dumps(request_body)}")
# Make the request
response = requests.post(
url,
params={"api_key": self.api_key},
headers={"Content-Type": "application/json"},
json=request_body
)
# Check for errors
if not response.ok:
error_text = response.text
raise Exception(f"API request failed: {response.status_code} {error_text}")
# Return the response data
return response.json()
except Exception as e:
logger.error(f"Error calling UseKeen API: {e}")
raise