-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdevin_api_client.py
More file actions
100 lines (82 loc) · 2.69 KB
/
Copy pathdevin_api_client.py
File metadata and controls
100 lines (82 loc) · 2.69 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
from typing import Literal, Optional, TypedDict
import aiohttp
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class DevinAPIAuthResponse(TypedDict):
status: str
org_id: str
class DevinAPISessionResponse(TypedDict):
session_id: str
url: str
is_new_session: Optional[bool]
class DevinAPISessionStatusResponse(TypedDict):
session_id: str
status: str
title: str
created_at: str
updated_at: str
snapshot_id: Optional[str]
playbook_id: Optional[str]
structured_output: dict
status_enum: (
Literal[
"working",
"blocked",
"finished",
"suspend_requested",
"resume_requested",
"resumed",
]
| None
)
class DevinAPIClient:
def __init__(self, api_key: str):
self.api_key = api_key
self.headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
self.base_url = "https://api.devin.ai/v1"
async def check_auth(self) -> DevinAPIAuthResponse:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/auth_status",
headers=self.headers,
) as response:
return await response.json()
async def start_session(self, prompt: str) -> DevinAPISessionResponse:
async with aiohttp.ClientSession() as session:
async with session.post(
f"{self.base_url}/sessions",
headers=self.headers,
json={"prompt": prompt},
) as response:
response_data = await response.json()
return response_data
async def get_session_status(
self, session_id: str
) -> DevinAPISessionStatusResponse | None:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/session/{session_id}",
headers=self.headers,
) as response:
response_data = await response.json()
if (
"detail" in response_data
and response_data["detail"] == "Session not found"
):
return None
return response_data
async def main():
api_key = os.getenv("DEVIN_API_KEY")
if not api_key:
raise ValueError("DEVIN_API_KEY environment variable is required")
client = DevinAPIClient(api_key)
auth_status = await client.check_auth()
print("AUTH STATUS: ", auth_status)
if __name__ == "__main__":
import asyncio
asyncio.run(main())