Spaces:
Running
Running
Commit
·
41f17f2
1
Parent(s):
f23b49b
fixed circular imports
Browse files- backend/agent.py +1 -1
- backend/api.py +2 -17
- backend/g_cal.py +21 -0
backend/agent.py
CHANGED
@@ -32,7 +32,7 @@ from langgraph.checkpoint.memory import MemorySaver
|
|
32 |
# from googleapiclient.discovery import build
|
33 |
|
34 |
# import function from api.py
|
35 |
-
from .
|
36 |
|
37 |
GOOGLE_SCOPES = ["https://www.googleapis.com/auth/calendar.events"]
|
38 |
|
|
|
32 |
# from googleapiclient.discovery import build
|
33 |
|
34 |
# import function from api.py
|
35 |
+
from .g_cal import get_gcal_service
|
36 |
|
37 |
GOOGLE_SCOPES = ["https://www.googleapis.com/auth/calendar.events"]
|
38 |
|
backend/api.py
CHANGED
@@ -12,12 +12,7 @@ from uuid import uuid4
|
|
12 |
from pathlib import Path
|
13 |
import json, secrets, urllib.parse, os
|
14 |
from google_auth_oauthlib.flow import Flow
|
15 |
-
from google.oauth2.credentials import Credentials
|
16 |
-
from googleapiclient.discovery import build
|
17 |
|
18 |
-
# --- import your compiled LangGraph app ---
|
19 |
-
# agent.py exports: app = graph.compile(...)
|
20 |
-
# ensure backend/__init__.py exists and you run uvicorn from repo root.
|
21 |
from .agent import app as lg_app
|
22 |
|
23 |
api = FastAPI(title="LangGraph Chat API")
|
@@ -38,12 +33,12 @@ api.add_middleware(
|
|
38 |
allow_credentials=True,
|
39 |
)
|
40 |
|
41 |
-
SCOPES
|
|
|
42 |
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
43 |
CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
44 |
BASE_URL = os.getenv("PUBLIC_BASE_URL", "http://localhost:8000")
|
45 |
REDIRECT_URI = f"{BASE_URL}/oauth/google/callback"
|
46 |
-
TOKEN_FILE = Path("/data/google_token.json") # persistent on HF Spaces
|
47 |
|
48 |
def _client_config():
|
49 |
return {
|
@@ -81,16 +76,6 @@ def oauth_callback(request: Request):
|
|
81 |
TOKEN_FILE.write_text(creds.to_json())
|
82 |
return PlainTextResponse("Google Calendar connected. You can close this tab.")
|
83 |
|
84 |
-
def get_gcal_service():
|
85 |
-
if TOKEN_FILE.exists():
|
86 |
-
creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES)
|
87 |
-
else:
|
88 |
-
# Not authorized yet
|
89 |
-
raise RuntimeError(
|
90 |
-
f"Google not connected. Visit {BASE_URL}/oauth/google/start to connect."
|
91 |
-
)
|
92 |
-
return build("calendar", "v3", credentials=creds)
|
93 |
-
|
94 |
@api.get("/health")
|
95 |
def health():
|
96 |
return {"ok": True}
|
|
|
12 |
from pathlib import Path
|
13 |
import json, secrets, urllib.parse, os
|
14 |
from google_auth_oauthlib.flow import Flow
|
|
|
|
|
15 |
|
|
|
|
|
|
|
16 |
from .agent import app as lg_app
|
17 |
|
18 |
api = FastAPI(title="LangGraph Chat API")
|
|
|
33 |
allow_credentials=True,
|
34 |
)
|
35 |
|
36 |
+
from .g_cal import SCOPES, TOKEN_FILE
|
37 |
+
|
38 |
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
39 |
CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
40 |
BASE_URL = os.getenv("PUBLIC_BASE_URL", "http://localhost:8000")
|
41 |
REDIRECT_URI = f"{BASE_URL}/oauth/google/callback"
|
|
|
42 |
|
43 |
def _client_config():
|
44 |
return {
|
|
|
76 |
TOKEN_FILE.write_text(creds.to_json())
|
77 |
return PlainTextResponse("Google Calendar connected. You can close this tab.")
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
@api.get("/health")
|
80 |
def health():
|
81 |
return {"ok": True}
|
backend/g_cal.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# backend/gcal.py
|
2 |
+
import os
|
3 |
+
from pathlib import Path
|
4 |
+
from google.oauth2.credentials import Credentials
|
5 |
+
from googleapiclient.discovery import build
|
6 |
+
|
7 |
+
# Same scope you used everywhere
|
8 |
+
SCOPES = ["https://www.googleapis.com/auth/calendar.events"]
|
9 |
+
|
10 |
+
# HF Spaces persistent disk path (Settings → Persistent storage)
|
11 |
+
TOKEN_FILE = Path(os.getenv("GCAL_TOKEN_PATH", "/data/google_token.json"))
|
12 |
+
|
13 |
+
def get_gcal_service():
|
14 |
+
"""
|
15 |
+
Load saved Google OAuth credentials and return a Calendar API client.
|
16 |
+
Raise a clear error if not connected yet.
|
17 |
+
"""
|
18 |
+
if not TOKEN_FILE.exists():
|
19 |
+
raise RuntimeError("Google Calendar not connected. Visit /oauth/google/start to connect.")
|
20 |
+
creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES)
|
21 |
+
return build("calendar", "v3", credentials=creds)
|