Skip to content

Commit 370ce81

Browse files
Henry Chanhenrylamchan
authored andcommitted
Support Audit Trail get endpoint
1 parent 687efc8 commit 370ce81

File tree

6 files changed

+156
-20
lines changed

6 files changed

+156
-20
lines changed

workos/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
api_key = os.getenv("WORKOS_API_KEY")
77
project_id = os.getenv("WORKOS_PROJECT_ID")
8-
base_api_url = "https://api.workos.com/"
8+
base_api_url = "http://localhost:7000/"

workos/audit_trail.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import workos
22
from workos.exceptions import ConfigurationException
3-
from workos.utils.request import RequestHelper, REQUEST_METHOD_POST
3+
from workos.resources.event import WorkOSEvent
4+
from workos.utils.request import RequestHelper, REQUEST_METHOD_GET, REQUEST_METHOD_POST
45
from workos.utils.validation import AUDIT_TRAIL_MODULE, validate_settings
56

67
EVENTS_PATH = "events"
78
METADATA_LIMIT = 50
9+
DEFAULT_EVENT_LIMIT = 10
810

911

1012
class AuditTrail(object):
@@ -51,7 +53,7 @@ def create_event(self, event, idempotency_key=None):
5153
dict: Response from WorkOS
5254
"""
5355
if len(event.get("metadata", {})) > METADATA_LIMIT:
54-
raise Exception(
56+
raise ValueError(
5557
"Number of metadata keys exceeds {}.".format(METADATA_LIMIT)
5658
)
5759

@@ -66,3 +68,80 @@ def create_event(self, event, idempotency_key=None):
6668
headers=headers,
6769
token=workos.api_key,
6870
)
71+
72+
def get_events(
73+
self,
74+
before=None,
75+
after=None,
76+
limit=DEFAULT_EVENT_LIMIT,
77+
group=None,
78+
action=None,
79+
action_type=None,
80+
actor_name=None,
81+
actor_id=None,
82+
target_name=None,
83+
target_id=None,
84+
occurred_at=None,
85+
occurred_at_gt=None,
86+
occurred_at_gte=None,
87+
occurred_at_lt=None,
88+
occurred_at_lte=None,
89+
search=None,
90+
):
91+
if before and after:
92+
raise ValueError("Specify either before or after")
93+
94+
params = {
95+
"before": before,
96+
"after": after,
97+
"limit": limit,
98+
}
99+
100+
if group:
101+
params["group"] = list(group)
102+
103+
if action:
104+
params["action"] = list(action)
105+
106+
if action_type:
107+
params["action_type"] = list(action_type)
108+
109+
if actor_name:
110+
params["actor_name"] = list(actor_name)
111+
112+
if actor_id:
113+
params["actor_id"] = list(actor_id)
114+
115+
if target_name:
116+
params["target_name"] = list(target_name)
117+
118+
if target_id:
119+
params["target_id"] = list(target_id)
120+
121+
if occurred_at:
122+
params["occurred_at"] = occurred_at
123+
else:
124+
if occurred_at_gte:
125+
params["occurred_at_gte"] = occurred_at_gte
126+
elif occurred_at_gt:
127+
params["occurred_at_gt"] = occurred_at_gt
128+
129+
if occurred_at_lte:
130+
params["occurred_at_lte"] = occurred_at_lte
131+
elif occurred_at_lt:
132+
params["occurred_at_lt"] = occurred_at_lt
133+
134+
if search:
135+
params["search"] = search
136+
137+
response = self.request_helper.request(
138+
EVENTS_PATH, method=REQUEST_METHOD_GET, params=params, token=workos.api_key,
139+
)
140+
141+
events = [
142+
WorkOSEvent.construct_from_response(data) for data in response["data"]
143+
]
144+
before = response["listMetadata"]["before"]
145+
after = response["listMetadata"]["after"]
146+
147+
return (events, before, after)

workos/resources/base.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
class WorkOSBaseResource(object):
2-
"""Representation of a WorkOS Resource as returned through the API.
2+
"""Representation of a WorkOS Resource as returned through the API.
33
44
Attributes:
55
OBJECT_FIELDS (list): List of fields a Resource is comprised of.
66
"""
77

8-
OBJECT_FIELDS = []
8+
OBJECT_FIELDS = []
99

10-
@classmethod
11-
def construct_from_response(cls, response):
12-
"""Returns an instance of WorkOSBaseResource.
10+
@classmethod
11+
def construct_from_response(cls, response):
12+
"""Returns an instance of WorkOSBaseResource.
1313
1414
Args:
1515
response (dict): Resource data from a WorkOS API response
1616
1717
Returns:
1818
WorkOSBaseResource: Instance of a WorkOSBaseResource with OBJECT_FIELDS fields set
1919
"""
20-
print(response)
21-
obj = cls()
22-
for field in cls.OBJECT_FIELDS:
23-
setattr(obj, field, response[field])
20+
obj = cls()
21+
for field in cls.OBJECT_FIELDS:
22+
setattr(obj, field, response[field])
2423

25-
return obj
24+
return obj
2625

27-
def to_dict(self):
28-
"""Returns a dict representation of the WorkOSBaseResource.
26+
def to_dict(self):
27+
"""Returns a dict representation of the WorkOSBaseResource.
2928
3029
Returns:
3130
dict: A dict representation of the WorkOSBaseResource
3231
"""
33-
obj_dict = {}
34-
for field in self.OBJECT_FIELDS:
35-
obj_dict[field] = getattr(self, field, None)
32+
obj_dict = {}
33+
for field in self.OBJECT_FIELDS:
34+
obj_dict[field] = getattr(self, field, None)
3635

37-
return obj_dict
36+
return obj_dict

workos/resources/event.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from workos.resources.base import WorkOSBaseResource
2+
from workos.resources.event_action import WorkOSEventAction
3+
4+
5+
class WorkOSEvent(WorkOSBaseResource):
6+
"""Representation of an Event as returned by WorkOS through the Audit Trail feature.
7+
8+
Attributes:
9+
OBJECT_FIELDS (list): List of fields a WorkOSEvent is comprised of.
10+
"""
11+
12+
OBJECT_FIELDS = [
13+
"id",
14+
"group",
15+
"location",
16+
"latitude",
17+
"longitude",
18+
"type",
19+
"actor_name",
20+
"actor_id",
21+
"target_name",
22+
"target_id",
23+
"metadata",
24+
"occurred_at",
25+
]
26+
27+
@classmethod
28+
def construct_from_response(cls, response):
29+
event = super(WorkOSEvent, cls).construct_from_response(response)
30+
31+
event_action = WorkOSEventAction.construct_from_response(response["action"])
32+
event.action = event_action
33+
34+
return event
35+
36+
def to_dict(self):
37+
event_dict = super(WorkOSEvent, self).to_dict()
38+
39+
event_action_dict = self.action.to_dict()
40+
event_dict["action"] = event_action_dict
41+
42+
return event_dict

workos/resources/event_action.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from workos.resources.base import WorkOSBaseResource
2+
3+
4+
class WorkOSEventAction(WorkOSBaseResource):
5+
"""Representation of an Event Action as returned by WorkOS through the Audit Trail feature.
6+
7+
Attributes:
8+
OBJECT_FIELDS (list): List of fields a WorkOSEventAction is comprised of.
9+
"""
10+
11+
OBJECT_FIELDS = [
12+
"id",
13+
"name",
14+
"project_id",
15+
]

workos/resources/sso.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from workos.resources.base import WorkOSBaseResource
22

3+
34
class WorkOSProfile(WorkOSBaseResource):
45
"""Representation of a User Profile as returned by WorkOS through the SSO feature.
56
@@ -14,4 +15,4 @@ class WorkOSProfile(WorkOSBaseResource):
1415
"last_name",
1516
"connection_type",
1617
"idp_id",
17-
]
18+
]

0 commit comments

Comments
 (0)