-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (136 loc) · 5.4 KB
/
app.py
File metadata and controls
169 lines (136 loc) · 5.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from functools import wraps
import json
from flask import Flask, jsonify, request, session, redirect
from google_auth_oauthlib.flow import Flow
import jwt
import os
from dotenv import load_dotenv
from services.calendar_service import CalendarService
from services import llm_service
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY')
# Path to the credentials JSON file
CREDENTIALS_FILE = os.getenv('CREDENTIALS_FILE')
# OAuth 2.0 scopes for the Google Calendar API
SCOPES = [os.getenv('SCOPES')]
# Redirect URI for OAuth 2.0
REDIRECT_URI = os.getenv('REDIRECT_URI')
STREAMLIT_URL = os.getenv('STREAMLIT_URL')
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" # Enable HTTP (insecure) for local testing
llm = llm_service.init_llm()
@app.route('/')
def index():
return 'Welcome to Chronologic!'
@app.route('/authorize/google')
def authorize():
flow = Flow.from_client_secrets_file(CREDENTIALS_FILE, scopes=SCOPES, redirect_uri=REDIRECT_URI)
authorization_url, state = flow.authorization_url()
session['state'] = state
return redirect(authorization_url)
@app.route('/google/callback')
def callback():
state = session['state']
flow = Flow.from_client_secrets_file(CREDENTIALS_FILE, scopes=SCOPES, state=state, redirect_uri=REDIRECT_URI)
authorization_response = request.url
flow.fetch_token(authorization_response=authorization_response)
credentials = flow.credentials
credentials = credentials_to_dict(credentials)
token = jwt.encode(credentials, app.secret_key, algorithm='HS256')
return redirect(f'{STREAMLIT_URL}/?token={token}')
def token_required(f):
@wraps(f)
def wrap(*args, **kwargs):
token = request.headers.get('Authorization').split()[1]
try:
jwt.decode(token, app.secret_key, algorithms=['HS256'])
except:
return jsonify({'message': 'Token is invalid!'}), 403
return f(*args, **kwargs)
return wrap
@app.route('/calendar')
@token_required
def calendar_events():
token = request.headers.get('Authorization').split()[1]
decoded = jwt.decode(token, app.secret_key, algorithms=['HS256'])
calendar_service = CalendarService({'google': decoded})
google_service = calendar_service.clients['google'].service
calendar = google_service.calendarList().get(calendarId='primary').execute()
calendar_id = calendar['id']
timezone = calendar['timeZone']
calendar_name = calendar['summary']
embed_link = f"https://calendar.google.com/calendar/embed?src={calendar_id}&ctz={timezone}"
return jsonify({
'calendar_name': calendar_name,
'embed_link': embed_link
})
def credentials_to_dict(credentials):
return {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes
}
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
prompt = data.get('prompt')
if not prompt:
return jsonify({"error": "Prompt is required"}), 400
response = llm_service.chat(prompt, llm)
return jsonify(response)
@app.route('/create_event', methods=['POST'])
@token_required
def create_event():
token = request.headers.get('Authorization').split()[1]
decoded = jwt.decode(token, app.secret_key, algorithms=['HS256'])
data = request.get_json()
event = data.get('event')
api_types = ['google']
if not event:
return jsonify({"error": "Event is required"}), 400
if not api_types:
return jsonify({"error": "API types are required"}), 400
calendar_service = CalendarService({'google': decoded})
response = calendar_service.create_event(api_types, event)
return jsonify({"message": response})
@app.route('/update_event', methods=['POST'])
@token_required
def update_event():
token = request.headers.get('Authorization').split()[1]
decoded = jwt.decode(token, app.secret_key, algorithms=['HS256'])
data = request.get_json()
event = data.get('event')
updated_event = event
event_name = event.get('summary')
api_types = ['google']
if not event_name:
return jsonify({"error": "Event name is required"}), 400
if not updated_event:
return jsonify({"error": "Updated event is required"}), 400
if not api_types:
return jsonify({"error": "API types are required"}), 400
calendar_service = CalendarService({'google': decoded})
response = calendar_service.update_event(api_types, event_name, updated_event)
return jsonify({"message": response})
@app.route('/delete_event', methods=['POST'])
@token_required
def delete_event():
token = request.headers.get('Authorization').split()[1]
decoded = jwt.decode(token, app.secret_key, algorithms=['HS256'])
data = request.get_json()
event = data.get('event')
event_name = event.get('summary')
api_types = ['google']
if not event_name:
return jsonify({"error": "Event name is required"}), 400
if not api_types:
return jsonify({"error": "API types are required"}), 400
calendar_service = CalendarService({'google': decoded})
response = calendar_service.delete_event(api_types, event_name)
return jsonify({"message": response})
if __name__ == '__main__':
app.run(debug=True)