-
Notifications
You must be signed in to change notification settings - Fork 0
/
experimenting.py
178 lines (151 loc) · 5.35 KB
/
experimenting.py
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
170
171
172
173
174
175
176
177
178
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
from decouple import config
from dateutil import parser
from datetime import datetime, timedelta
from pydantic import BaseModel
import logging
from nylas_integration import check_time_and_book
# from fastapi import FastAPI, HTTPException
# from fastapi.responses import JSONResponse
# from fastapi.logger import logger as fastapi_logger
app = FastAPI()
# Configure the root logger
logging.basicConfig(level=logging.INFO)
# Create a logger specifically for your application
logger = logging.getLogger('app')
class BookingRequest(BaseModel):
calendar_id: str
time: str
name: str
@app.get("/")
async def index():
return {"msg": "working"}
@app.get("/check_availability")
async def index():
return {"msg": "available?"}
@app.post("/check_availability")
async def check_availability(booking_request: BookingRequest):
# date = booking_request.date
# time = booking_request.time
print(type(booking_request))
time = booking_request.time
calendar_id = booking_request.calendar_id
name = booking_request.name
# print(booking_request)
# print(time, calendar_id, name)
print(check_time_and_book(calendar_id, time, name))
# if not is_valid_datetime(date, time):
# raise HTTPException(status_code=400, detail="Invalid date or time format")
#
# if not is_available(date, time):
# alternatives = get_alternatives(date, time)
# return {"available": False, "alternatives": alternatives}
# if is_available(booking_request.msg):
# return {"available": True}
#
#
# return {"available": False}
# def is_available(iso_datetime: str) -> bool:
# start_time = parser.parse(iso_datetime)
# start_time = start_time + timedelta(hours=-2)
# end_time = start_time + timedelta(minutes=30)
# print(start_time, end_time, 'start and endtime')
#
# url = "https://api.calendly.com/scheduled_events"
# headers = {"Authorization": f"Bearer {CALD_API_TOKEN}"}
#
# params = {
# "min_start_time": start_time,
# "max_end_time": end_time
# }
#
# response = requests.get(url, headers=headers, params=params)
# # print the response
# print(response.json())
#
# if response.status_code == 200:
# event_data = response.json()
# return len(event_data["collection"]) == 0
# else:
# logger.error(f"Failed to check availability: {response.status_code}")
# raise HTTPException(status_code=500, detail="Failed to check availability")
# def get_alternatives(date: str, time: str) -> list:
# # Make a request to the Calendly API to retrieve alternative slots
# # Example: Fetch the next three available slots after the specified date and time
# url = "https://api.calendly.com/scheduled_events"
# headers = {"Authorization": f"Bearer {CALD_API_TOKEN}"}
# params = {
# "count": 3,
# "min_start_time": f"{date}T{time}",
# "status": "active"
# }
#
# response = requests.get(url, headers=headers, params=params)
#
# if response.status_code == 200:
# event_data = response.json()
# alternatives = []
# for event in event_data["collection"]:
# alternative = {
# "date": event["start_time"].split("T")[0],
# "time": event["start_time"].split("T")[1].split(":00Z")[0]
# }
# alternatives.append(alternative)
# return alternatives
# else:
# raise HTTPException(status_code=500, detail="Failed to retrieve alternatives")
#
# def confirm_booking() -> bool:
# # Implement user confirmation logic, you can use a frontend framework or UI library for this
# # Return True if the user confirms the booking, False otherwise
# return True
#
# def create_booking(date: str, time: str) -> bool:
# # Make a request to the Calendly API to create the booking
# url = "https://api.calendly.com/scheduled_events"
# headers = {"Authorization": f"Bearer {CALD_API_TOKEN}"}
# payload = {
# "event_type": "YOUR_EVENT_TYPE_UUID",
# "start_time": f"{date}T{time}:00Z",
# "end_time": f"{date}T{time}:00Z"
# }
#
# response = requests.post(url, headers=headers, json=payload)
#
# if response.status_code == 201:
# return True
# else:
# return False
#
#
#
#
#
# @app.exception_handler(HTTPException)
# async def http_exception_handler(request, exc):
# # Log the error using your application logger
# logger.error(f"HTTPException: {exc.detail}")
#
# # Return an appropriate response to the client
# return JSONResponse(status_code=exc.status_code, content={"error": exc.detail})
#
# @app.exception_handler(Exception)
# async def generic_exception_handler(request, exc):
# # Log the error using your application logger
# logger.exception("An unhandled exception occurred")
#
# # Return an appropriate response to the client
# return JSONResponse(status_code=500, content={"error": "Internal server error"})
#
#
# @app.on_event("startup")
# async def startup_event():
# # You can log startup events or other application initialization tasks here
# logger.info("Application startup")
#
# @app.on_event("shutdown")
# async def shutdown_event():
# # You can log shutdown events or perform cleanup tasks here
# logger.info("Application shutdown")