Skip to content

Commit

Permalink
Add get_themes method
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Oct 16, 2023
1 parent b1fe4e9 commit 70ff8b6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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]

0 comments on commit 70ff8b6

Please sign in to comment.