This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Merge pull request #47 from seamapi/implement-events
fix #33: Implement `event.list()` and `Event`
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from seamapi.types import ( | ||
Event, | ||
AbstractEvents, | ||
AbstractSeam as Seam, | ||
) | ||
from typing import List, Optional | ||
import requests | ||
|
||
|
||
class Events(AbstractEvents): | ||
""" | ||
A class to interact with events through the Seam API | ||
... | ||
Attributes | ||
---------- | ||
seam : Seam | ||
Initial seam class | ||
Methods | ||
------- | ||
list(since, device_id=None, device_ids=None, event_type=None, event_types=None) | ||
Gets a list of events | ||
""" | ||
|
||
seam: Seam | ||
|
||
def __init__(self, seam: Seam): | ||
""" | ||
Parameters | ||
---------- | ||
seam : Seam | ||
Intial seam class | ||
""" | ||
self.seam = seam | ||
|
||
def list( | ||
self, | ||
since: str, | ||
device_id: Optional[str] = None, | ||
device_ids: Optional[list] = None, | ||
event_type: Optional[str] = None, | ||
event_types: Optional[list] = None | ||
) -> List[Event]: | ||
"""Gets a list of events. | ||
Parameters | ||
---------- | ||
since : str | ||
ISO 8601 timestamp of the earliest event to return | ||
device_id : Optional[str] | ||
Device ID to filter events by | ||
device_ids : Optional[list] | ||
Device IDs to filter events by | ||
event_type : Optional[str] | ||
Event type to filter events by | ||
event_types : Optional[list] | ||
Event types to filter events by | ||
Raises | ||
------ | ||
Exception | ||
If the API request wasn't successful. | ||
Returns | ||
------ | ||
A list of events. | ||
""" | ||
if not since: | ||
raise Exception("'since' is required") | ||
|
||
params = { | ||
"since": since, | ||
} | ||
|
||
arguments = { | ||
"device_id": device_id, | ||
"device_ids": device_ids, | ||
"event_type": event_type, | ||
"event_types": event_types | ||
} | ||
|
||
for name in arguments: | ||
if arguments[name]: | ||
params.update({name: arguments[name]}) | ||
|
||
res = requests.get( | ||
f"{self.seam.api_url}/events/list", | ||
params=params, | ||
headers={"Authorization": f"Bearer {self.seam.api_key}"}, | ||
) | ||
if not res.ok: | ||
raise Exception(res.text) | ||
|
||
events = res.json()["events"] | ||
return events |
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,11 @@ | ||
from seamapi import Seam | ||
from tests.fixtures.run_august_factory import run_august_factory | ||
|
||
SINCE="2021-01-01T00:00:00.000Z" | ||
EVENT_TYPE = "device.connected" | ||
|
||
def test_events(seam: Seam): | ||
run_august_factory(seam) | ||
|
||
events = seam.events.list(since=SINCE) | ||
assert events[0]['event_type'] == EVENT_TYPE |