Skip to content

Commit 93bc034

Browse files
authored
[TSDK-286] Improve usage of read only fields in models (#215)
This PR improves our usage of read only fields, adding a general set of read only fields used by all API models, and read only fields for specific API models.
1 parent 4e59137 commit 93bc034

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

nylas/client/restful_models.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def as_json(self):
151151

152152

153153
class NylasAPIObject(RestfulModel):
154+
read_only_attrs = {"id", "account_id", "object", "job_status_id"}
155+
154156
def __init__(self, cls, api):
155157
RestfulModel.__init__(self, cls, api)
156158

@@ -638,6 +640,7 @@ class Calendar(NylasAPIObject):
638640

639641
def __init__(self, api):
640642
NylasAPIObject.__init__(self, Calendar, api)
643+
self.read_only_attrs.update({"is_primary", "read_only"})
641644

642645
@property
643646
def events(self):
@@ -674,6 +677,16 @@ class Event(NylasAPIObject):
674677

675678
def __init__(self, api):
676679
NylasAPIObject.__init__(self, Event, api)
680+
self.read_only_attrs.update(
681+
{
682+
"ical_uid",
683+
"message_id",
684+
"owner",
685+
"status",
686+
"master_event_id",
687+
"original_start_time",
688+
}
689+
)
677690

678691
def as_json(self):
679692
dct = NylasAPIObject.as_json(self)
@@ -804,6 +817,13 @@ class JobStatus(NylasAPIObject):
804817

805818
def __init__(self, api):
806819
NylasAPIObject.__init__(self, JobStatus, api)
820+
self.read_only_attrs.update(
821+
{
822+
"action",
823+
"status",
824+
"original_data",
825+
}
826+
)
807827

808828
def is_successful(self):
809829
return self.status == "successful"
@@ -871,13 +891,19 @@ class Component(NylasAPIObject):
871891
"created_at": "created_at",
872892
"updated_at": "updated_at",
873893
}
874-
read_only_attrs = {"id", "public_application_id", "created_at", "updated_at"}
875894

876895
collection_name = None
877896
api_root = "component"
878897

879898
def __init__(self, api):
880899
NylasAPIObject.__init__(self, Component, api)
900+
self.read_only_attrs.update(
901+
{
902+
"public_application_id",
903+
"created_at",
904+
"updated_at",
905+
}
906+
)
881907

882908
def as_json(self):
883909
dct = NylasAPIObject.as_json(self)
@@ -896,13 +922,13 @@ class Webhook(NylasAPIObject):
896922
"application_id",
897923
"version",
898924
)
899-
read_only_attrs = {"id", "application_id", "version"}
900925

901926
collection_name = "webhooks"
902927
api_root = "a"
903928

904929
def __init__(self, api):
905930
NylasAPIObject.__init__(self, Webhook, api)
931+
self.read_only_attrs.update({"application_id", "version"})
906932

907933
def as_json(self):
908934
dct = {}

tests/test_events.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import datetime, timedelta
33
import pytest
44
from urlobject import URLObject
5-
from requests import RequestException
65
from nylas.client.restful_models import Event
76

87

@@ -15,15 +14,39 @@ def blank_event(api_client):
1514

1615

1716
@pytest.mark.usefixtures("mock_event_create_response")
18-
def test_event_crud(api_client):
17+
def test_event_crud(mocked_responses, api_client):
1918
event1 = blank_event(api_client)
19+
event1.object = "should not send"
20+
event1.account_id = "should not send"
21+
event1.job_status_id = "should not send"
22+
event1.ical_uid = "should not send"
23+
event1.message_id = "should not send"
24+
event1.owner = "should not send"
25+
event1.status = "should not send"
26+
event1.master_event_id = "should not send"
27+
event1.original_start_time = "should not send"
2028
event1.save()
29+
request = mocked_responses.calls[0].request
30+
body = json.loads(request.body)
2131
assert event1.id == "cv4ei7syx10uvsxbs21ccsezf"
32+
assert "title" in body
33+
assert "object" not in body
34+
assert "account_id" not in body
35+
assert "job_status_id" not in body
36+
assert "ical_uid" not in body
37+
assert "message_id" not in body
38+
assert "owner" not in body
39+
assert "status" not in body
40+
assert "master_event_id" not in body
41+
assert "original_start_time" not in body
2242

2343
event1.title = "blah"
2444
event1.save()
45+
request = mocked_responses.calls[1].request
46+
body = json.loads(request.body)
2547
assert event1.title == "loaded from JSON"
2648
assert event1.get("ignored") is None
49+
assert "id" not in body
2750

2851

2952
@pytest.mark.usefixtures("mock_event_create_notify_response")

0 commit comments

Comments
 (0)