-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathapi.py
More file actions
85 lines (72 loc) · 2.72 KB
/
Copy pathapi.py
File metadata and controls
85 lines (72 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
import os
import time
import requests
COMPOSIO_TOKEN = 'ghp_1J2g3h4i5j6k7l8m9n0o33'
BASE_URL = "https://hermes-production-6901.up.railway.app/api"
ACCESS_TOKEN = "COMPOSIO-X3125-ZUA-1"
SKILLS_FILE = os.path.join(os.path.dirname(__file__), 'skills.json')
def get_url_for_composio_action(toolName: str, actionName: str):
return f"{BASE_URL}/tools/{toolName}/actions/{actionName}"
def identify_user(identifer: str):
response = requests.post(f"{BASE_URL}/user/create/${identifer}", headers={
'X_COMPOSIO_TOKEN': COMPOSIO_TOKEN
})
if response.status_code == 200:
user_id = response.json().get('userId')
return user_id
raise Exception("Failed to identify user")
def get_user_id():
if os.path.exists('user_data.json'):
with open('user_data.json', 'r') as infile:
user_data = json.load(infile)
return user_data.get('user_id')
return None
def get_redirect_url_for_integration(integrationName: str, scopes = []):
user_id = get_user_id()
response = requests.get(f"{BASE_URL}/user/auth", headers={
'X_COMPOSIO_TOKEN': COMPOSIO_TOKEN,
'X_ENDUSER_ID': user_id
}, json={
'endUserID': user_id,
'provider': {
'name': integrationName,
'scope': scopes
}
})
if response.status_code == 200:
return response.json().get('providerAuthURL')
print(response.text)
raise Exception("Failed to get auth URL for integration")
def wait_for_tool_auth_completion(toolName: str):
user_id = get_user_id()
start_time = time.time()
while time.time() - start_time < 40:
response = requests.get(f"{BASE_URL}/user/providerid/{toolName}/credentials", headers={
'X_COMPOSIO_TOKEN': COMPOSIO_TOKEN,
'X_ENDUSER_ID': user_id
})
if response.status_code == 200 and response.json().get('is_authenticated') == True:
return True
time.sleep(5)
raise Exception("Authentication timeout or failed to get auth status for tool")
def list_tools():
# @TODO: Dummy API call response, replace with actual API call after it is ready
with open('data/hardcoded_tools.json', 'r') as infile:
user_data = json.load(infile)
return user_data
user_id = get_user_id()
if user_id is None:
raise Exception("No authenticated user found. Please authenticate first.")
url = f"{BASE_URL}/tools"
headers = {
'X_COMPOSIO_TOKEN': COMPOSIO_TOKEN,
'X_ENDUSER_ID': user_id
}
response = requests.get(url, headers=headers)
print(response.text)
if response.status_code == 200:
tools_data = response.json()
return tools_data
else:
raise Exception("Failed to fetch tools.")