import requests
def send_authenticated_request(
url: str,
data: str,
*,
tools=None,
shared_state=None,
**kwargs
) -> str:
"""Send authenticated API request with credential."""
try:
# Access credentials via tools instance
if not tools or not hasattr(tools, 'credential_manager'):
return "Error: Credential manager not available"
api_key = tools.credential_manager.get_credential("API_KEY")
# Check if we've made too many requests
if shared_state and shared_state.step_number > 15:
return "Error: Too many API calls"
# Send authenticated request
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.post(url, json={"data": data}, headers=headers, timeout=10)
response.raise_for_status()
return f"Request successful: {response.status_code}"
except Exception as e:
return f"Error: {str(e)}"
custom_tools = {
"send_authenticated_request": {
"arguments": ["url", "data"],
"description": "Send authenticated API request using stored credentials",
"function": send_authenticated_request
}
}
# Usage with credentials
credentials = {"API_KEY": "sk-1234567890"}
agent = DroidAgent(
goal="Send data to API",
config=config,
custom_tools=custom_tools,
credentials=credentials
)