Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_themes method #385

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions datawrapper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Datawrapper:
_PUBLISH_URL = _BASE_URL + "/charts"
_BASEMAPS_URL = _BASE_URL + "/v3/basemaps"
_FOLDERS_URL = _BASE_URL + "/v3/folders"
_THEMES_URL = _BASE_URL + "/v3/themes"

_ACCESS_TOKEN = os.getenv("DATAWRAPPER_ACCESS_TOKEN")

Expand Down Expand Up @@ -84,6 +85,50 @@ def account_info(self) -> dict[Any, Any] | None | Any:
logger.error(msg)
raise Exception(msg)

def get_themes(
self,
limit: str | int = 100,
offset: str | int = 0,
deleted: bool = False
) -> dict[str, Any]:
"""Get a list of themes in your Datawrapper account.

Parameters
----------
limit: str | int
Maximum items to fetch. Useful for pagination. Default 100.
offset: str | int
Number of items to skip. Useful for pagination. Default zero.
deleted: bool
Whether to include deleted themes

Returns
-------
dict
A dictionary containing the themes in your Datawrapper account.
"""
_header = self._auth_header
_header["accept"] = "*/*"

_query = {
"limit": limit,
"offset": offset,
"deleted": json.dumps(deleted),
}

response = r.get(
url=self._THEMES_URL,
headers=_header,
params=_query,
)

if response.ok:
return response.json()
else:
msg = "Couldn't retrieve themes in account."
logger.error(msg)
raise Exception(msg)

def add_data(self, chart_id: str, data: pd.DataFrame | str) -> r.Response:
"""Add data to a specified chart.

Expand Down
13 changes: 13 additions & 0 deletions tests/test_themes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Test themes related API enpoints."""
from datawrapper import Datawrapper


def test_get_themes():
"""Test the get_themes method."""
dw = Datawrapper()

one = dw.get_themes()
assert len(one['list']) > 0

two = dw.get_themes(offset=2, limit=1)
assert one['list'][0] != two['list'][0]