Skip to content

Commit

Permalink
Update all endpoint wrappers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Schalk1e committed Jun 3, 2024
1 parent 1c7aa36 commit 9bdaea0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 44 deletions.
8 changes: 8 additions & 0 deletions examples/turn/contacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from api.turn import pyTurn

start = "2024-01-01T00:00:00+00:00"
end = "2024-01-01T05:00:00+00:00"

contacts = pyTurn.contacts.get_contacts(start=start, end=end)

print(contacts.head(5))
5 changes: 5 additions & 0 deletions examples/turn/content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from api.turn import pyTurn

content = pyTurn.content.get_content()

print(content.head(5))
8 changes: 3 additions & 5 deletions examples/turn/messages.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from datetime import datetime, timedelta

from api.turn import pyTurn

end = datetime.now().isoformat()
start = (datetime.now() - timedelta(2)).isoformat()
start = "2024-01-01T00:00:00+00:00"
end = "2024-01-01T05:00:00+00:00"

messages = pyTurn.messages.get_messages(start=start, end=end)

messages.keys()
print(messages.keys())
8 changes: 5 additions & 3 deletions src/api/turn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ def get_paginated(
cursor_response = client.post(url, json=data, params={**kwargs})
cursor_response.raise_for_status()

cursor = cursor_response.json()["cursor"]

while True:
response = client.get(url + f"/{cursor_response}")
response = client.get(url + f"/{cursor}")
response.raise_for_status()

response_data: dict = response.json()["data"]
yield from response_data

try:
cursor_response = response.json()["paging"]["next"]
except AttributeError:
cursor = response.json()["paging"]["next"]
except TypeError:
break


Expand Down
50 changes: 26 additions & 24 deletions src/api/turn/requests/contacts.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
from attrs import define
from httpx import Client
from pandas import DataFrame, concat, json_normalize


def deep_get(obj, path):
if not path or not obj:
return obj
return deep_get(obj.get(path[0]), path[1:])
from .. import get_paginated


@define
class Contacts:
def __init__(self, session):
self._session = session
"""Dedicated to the contacts endpoint of the Turn Data Export API."""

def get_contacts(self, start, end) -> DataFrame:
url = "data/contacts/cursor"
client: Client

data = {"from": start, "until": end}
def get_contacts(
self, start: str, end: str, **kwargs: str | int
) -> DataFrame:
"""Returns a pandas DataFrame of Turn Contacts.
cursor_request = self._session.request("POST", url, json=data)
The endpoint supports time-base query parameters and
can be called as:
cursor_request.raise_for_status()
cursor = cursor_request.json()["cursor"]
pyTurn.contacts.get_contacts(
start=start,
end=end
)
response_list = []
i = 0
while cursor:
i += 1
print("Iteration #: ", i)
response = self._session.request("GET", url + f"/{cursor}")
response.raise_for_status()
for row in response.json()["data"]:
response_list.append(row)
See examples/turn/contacts.py for information on
timestamp formatting. (The Turn Data Export API is
pedantic on such issues).
"""
url = "data/contacts/cursor"

cursor = deep_get(response.json(), ["paging", "next"])
contacts_generator = get_paginated(
client=self.client, url=url, start=start, end=end, **kwargs
)

try:
contacts = concat(
[json_normalize(obj, sep="_") for obj in response_list]
[json_normalize(obj, sep="_") for obj in contacts_generator]
)
except ValueError:
contacts = DataFrame()
Expand Down
33 changes: 21 additions & 12 deletions src/api/turn/requests/content.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
from pandas import json_normalize
from attrs import define
from httpx import Client
from pandas import DataFrame, json_normalize


def deep_get(obj, path):
if not path or not obj:
return obj
return deep_get(obj.get(path[0]), path[1:])
@define
class Content:
"""Dedicated to the content export endpoint of the Turn Data Export API."""

client: Client

class Content:
def __init__(self, session) -> None:
self._session = session
def get_content(self, **kwargs: str | int) -> DataFrame:
"""Get a pandas DataFrame of content.
No time-based query parameters are supported by this endpoint.
def get_content(self, start, end):
pyTurn.content.get_content()
See examples/turn/content.py
"""
url = "export"

response = self._session.request("GET", url)
response.raise_for_status()
params = {**kwargs}

content_response = self.client.get(url=url, params=params)
content_response.raise_for_status()

content = json_normalize(response.json()["data"])
content = json_normalize(content_response.json()["data"])

return content
2 changes: 2 additions & 0 deletions src/api/turn/requests/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def get_messages(
end=end
)
See examples/turn/messages.py.
"""

url = "data/messages/cursor"
Expand Down

0 comments on commit 9bdaea0

Please sign in to comment.