From d1a5b4d0f8d1b040cf8ad3c13a7a99b8e025f3ab Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Mon, 27 Jun 2022 14:05:21 -0400 Subject: [PATCH] Add enforce_read_only to children as_json --- CHANGELOG.md | 4 ++++ nylas/client/authentication_models.py | 14 ++++++++---- nylas/client/restful_models.py | 32 +++++++++++++++------------ nylas/client/scheduler_models.py | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 344a52db..0eb35af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ nylas-python Changelog ====================== +Unreleased +---------------- +* Add `enforce_read_only` parameter to overriding `as_json` functions + v5.9.1 ---------------- * Add option to include read only params in `as_json` diff --git a/nylas/client/authentication_models.py b/nylas/client/authentication_models.py index 7574b117..f90ebd9f 100644 --- a/nylas/client/authentication_models.py +++ b/nylas/client/authentication_models.py @@ -53,8 +53,11 @@ def create(cls, api, **kwargs): return obj - def as_json(self): - dct = super(Integration, self).as_json() + def as_json(self, enforce_read_only=True): + dct = super(Integration, self).as_json(enforce_read_only) + if enforce_read_only is False: + return dct + if not self.id: if isinstance(self.provider, Authentication.Provider): dct["provider"] = self.provider.value @@ -108,8 +111,11 @@ def create(cls, api, **kwargs): obj = super(Grant, cls).create(api, **kwargs) return obj - def as_json(self): - dct = super(Grant, self).as_json() + def as_json(self, enforce_read_only=True): + dct = super(Grant, self).as_json(enforce_read_only) + if enforce_read_only is False: + return dct + # provider and state can not be updated if self.id: del dct["provider"] diff --git a/nylas/client/restful_models.py b/nylas/client/restful_models.py index 214c7ad3..94172a84 100644 --- a/nylas/client/restful_models.py +++ b/nylas/client/restful_models.py @@ -691,8 +691,11 @@ def __init__(self, api): } ) - def as_json(self): - dct = NylasAPIObject.as_json(self) + def as_json(self, enforce_read_only=True): + dct = NylasAPIObject.as_json(self, enforce_read_only) + if enforce_read_only is False: + return dct + # Filter some parameters we got from the API if dct.get("when"): # Currently, the event (self) and the dict (dct) share the same @@ -920,8 +923,11 @@ def __init__(self, api): } ) - def as_json(self): - dct = NylasAPIObject.as_json(self) + def as_json(self, enforce_read_only=True): + dct = NylasAPIObject.as_json(self, enforce_read_only) + if enforce_read_only is False: + return dct + # "type" cannot be modified after created if self.id: dct.pop("type") @@ -945,13 +951,13 @@ def __init__(self, api): NylasAPIObject.__init__(self, Webhook, api) self.read_only_attrs.update({"application_id", "version"}) - def as_json(self): + def as_json(self, enforce_read_only=True): dct = {} # Only 'state' can get updated - if self.id: + if self.id and enforce_read_only is True: dct["state"] = self.state else: - dct = NylasAPIObject.as_json(self) + dct = NylasAPIObject.as_json(self, enforce_read_only) return dct class Trigger(str, Enum): @@ -1035,9 +1041,11 @@ class Account(NylasAPIObject): def __init__(self, api): NylasAPIObject.__init__(self, Account, api) - def as_json(self): - dct = {"metadata": self.metadata} - return dct + def as_json(self, enforce_read_only=True): + if enforce_read_only is False: + return NylasAPIObject.as_json(self, enforce_read_only) + else: + return {"metadata": self.metadata} def upgrade(self): return self.api._call_resource_method(self, self.account_id, "upgrade", None) @@ -1064,10 +1072,6 @@ class APIAccount(NylasAPIObject): def __init__(self, api): NylasAPIObject.__init__(self, APIAccount, api) - def as_json(self): - dct = NylasAPIObject.as_json(self) - return dct - class SingletonAccount(APIAccount): # This is an APIAccount that lives under /account. diff --git a/nylas/client/scheduler_models.py b/nylas/client/scheduler_models.py index 14a47470..0d19e5d5 100644 --- a/nylas/client/scheduler_models.py +++ b/nylas/client/scheduler_models.py @@ -47,7 +47,7 @@ class SchedulerBookingRequest(RestfulModel): def __init__(self, api): RestfulModel.__init__(self, SchedulerBookingRequest, api) - def as_json(self): + def as_json(self, enforce_read_only=True): dct = RestfulModel.as_json(self) if "additional_values" not in dct or dct["additional_values"] is None: dct["additional_values"] = {}