From 9bdaea0feaf2b2495d28a74413412bc6dda3cdfd Mon Sep 17 00:00:00 2001 From: Schalk Date: Mon, 3 Jun 2024 14:53:53 +0200 Subject: [PATCH] Update all endpoint wrappers. --- examples/turn/contacts.py | 8 +++++ examples/turn/content.py | 5 ++++ examples/turn/messages.py | 8 ++--- src/api/turn/__init__.py | 8 +++-- src/api/turn/requests/contacts.py | 50 ++++++++++++++++--------------- src/api/turn/requests/content.py | 33 ++++++++++++-------- src/api/turn/requests/messages.py | 2 ++ 7 files changed, 70 insertions(+), 44 deletions(-) create mode 100644 examples/turn/contacts.py create mode 100644 examples/turn/content.py diff --git a/examples/turn/contacts.py b/examples/turn/contacts.py new file mode 100644 index 0000000..9486553 --- /dev/null +++ b/examples/turn/contacts.py @@ -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)) diff --git a/examples/turn/content.py b/examples/turn/content.py new file mode 100644 index 0000000..5bf233a --- /dev/null +++ b/examples/turn/content.py @@ -0,0 +1,5 @@ +from api.turn import pyTurn + +content = pyTurn.content.get_content() + +print(content.head(5)) diff --git a/examples/turn/messages.py b/examples/turn/messages.py index 1f6f0aa..3edfbd5 100644 --- a/examples/turn/messages.py +++ b/examples/turn/messages.py @@ -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()) diff --git a/src/api/turn/__init__.py b/src/api/turn/__init__.py index 55a712c..2d7ee03 100644 --- a/src/api/turn/__init__.py +++ b/src/api/turn/__init__.py @@ -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 diff --git a/src/api/turn/requests/contacts.py b/src/api/turn/requests/contacts.py index 721672a..0726e23 100644 --- a/src/api/turn/requests/contacts.py +++ b/src/api/turn/requests/contacts.py @@ -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() diff --git a/src/api/turn/requests/content.py b/src/api/turn/requests/content.py index 5262e7f..638496f 100644 --- a/src/api/turn/requests/content.py +++ b/src/api/turn/requests/content.py @@ -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 diff --git a/src/api/turn/requests/messages.py b/src/api/turn/requests/messages.py index 0bbdebd..80edaab 100644 --- a/src/api/turn/requests/messages.py +++ b/src/api/turn/requests/messages.py @@ -24,6 +24,8 @@ def get_messages( end=end ) + See examples/turn/messages.py. + """ url = "data/messages/cursor"