-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[69624] Scheduler API Support (#173)
This PR enables SDK support for the Scheduler API.
- Loading branch information
1 parent
8ed44e7
commit f412d73
Showing
7 changed files
with
756 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from nylas.client.restful_models import RestfulModel | ||
|
||
|
||
class SchedulerTimeSlot(RestfulModel): | ||
attrs = ["account_id", "calendar_id", "host_name", "emails"] | ||
datetime_attrs = {"start": "start", "end": "end"} | ||
|
||
def __init__(self, api): | ||
RestfulModel.__init__(self, SchedulerTimeSlot, api) | ||
|
||
|
||
class SchedulerBookingConfirmation(RestfulModel): | ||
attrs = [ | ||
"id", | ||
"account_id", | ||
"additional_field_values", | ||
"calendar_event_id", | ||
"calendar_id", | ||
"edit_hash", | ||
"is_confirmed", | ||
"location", | ||
"recipient_email", | ||
"recipient_locale", | ||
"recipient_name", | ||
"recipient_tz", | ||
"title", | ||
] | ||
datetime_attrs = {"start_time": "start_time", "end_time": "end_time"} | ||
|
||
def __init__(self, api): | ||
RestfulModel.__init__(self, SchedulerBookingConfirmation, api) | ||
|
||
|
||
class SchedulerBookingRequest(RestfulModel): | ||
attrs = [ | ||
"additional_values", | ||
"additional_emails", | ||
"email", | ||
"locale", | ||
"name", | ||
"page_hostname", | ||
"replaces_booking_hash", | ||
"timezone", | ||
"slot", | ||
] | ||
|
||
def __init__(self, api): | ||
RestfulModel.__init__(self, SchedulerBookingRequest, api) | ||
|
||
def as_json(self): | ||
dct = RestfulModel.as_json(self) | ||
if "additional_values" not in dct or dct["additional_values"] is None: | ||
dct["additional_values"] = {} | ||
if "additional_emails" not in dct or dct["additional_emails"] is None: | ||
dct["additional_emails"] = [] | ||
if "slot" in dct and isinstance(dct["slot"], SchedulerTimeSlot): | ||
dct["slot"] = dct["slot"].as_json() | ||
|
||
return dct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import copy | ||
|
||
from nylas.client.restful_model_collection import RestfulModelCollection | ||
from nylas.client.restful_models import Scheduler | ||
from nylas.client.scheduler_models import ( | ||
SchedulerTimeSlot, | ||
SchedulerBookingConfirmation, | ||
) | ||
|
||
|
||
class SchedulerRestfulModelCollection(RestfulModelCollection): | ||
def __init__(self, api): | ||
# Make a copy of the API as we need to change the base url for Scheduler calls | ||
scheduler_api = copy.copy(api) | ||
scheduler_api.api_server = "https://api.schedule.nylas.com" | ||
RestfulModelCollection.__init__(self, Scheduler, scheduler_api) | ||
|
||
def get_google_availability(self): | ||
return self._execute_provider_availability("google") | ||
|
||
def get_office_365_availability(self): | ||
return self._execute_provider_availability("o365") | ||
|
||
def get_page_slug(self, slug): | ||
page_response = self.api._get_resource_raw( | ||
self.model_class, slug, extra="info", path="schedule" | ||
).json() | ||
return Scheduler.create(self.api, **page_response) | ||
|
||
def get_available_time_slots(self, slug): | ||
response = self.api._get_resource_raw( | ||
self.model_class, slug, extra="timeslots", path="schedule" | ||
).json() | ||
return [ | ||
SchedulerTimeSlot.create(self.api, **x) for x in response if x is not None | ||
] | ||
|
||
def book_time_slot(self, slug, timeslot): | ||
response = self.api._post_resource( | ||
self.model_class, slug, "timeslots", timeslot.as_json(), path="schedule" | ||
) | ||
return SchedulerBookingConfirmation.create(self.api, **response) | ||
|
||
def cancel_booking(self, slug, edit_hash, reason): | ||
return self.api._post_resource( | ||
self.model_class, | ||
slug, | ||
"{}/cancel".format(edit_hash), | ||
{"reason": reason}, | ||
path="schedule", | ||
) | ||
|
||
def confirm_booking(self, slug, edit_hash): | ||
booking_response = self.api._post_resource( | ||
self.model_class, slug, "{}/confirm".format(edit_hash), {}, path="schedule" | ||
) | ||
return SchedulerBookingConfirmation.create(self.api, **booking_response) | ||
|
||
def _execute_provider_availability(self, provider): | ||
return self.api._get_resource_raw( | ||
self.model_class, | ||
None, | ||
extra="availability/{}".format(provider), | ||
path="schedule", | ||
).json() |
Oops, something went wrong.