Knuckles-Team / listmonk-api

Listmonk Python API Wrapper
MIT License
5 stars 2 forks source link

X-Total-Pages header not (always) set #1

Open xlemmingx opened 1 year ago

xlemmingx commented 1 year ago

Hey, thanks for your work! Unfortunately I got an error while trying to use the api wrapper.

Environment

Code lm_client = listmonk_api.Api(url=listmonk_api_url, username=username, password=password) print(f"Subscribers: {lm_client.get_subscribers()}")

Error Traceback (most recent call last): File "/home/random/repositories/test/files.py", line 22, in <module> print(f"Subscribers: {lm_client.get_subscribers()}") File "/home/random/repositories/test/.venv/lib/python3.10/site-packages/listmonk_api/decorators.py", line 19, in wrapper return function(self, *args, **kwargs) File "/home/random/repositories/test/.venv/lib/python3.10/site-packages/listmonk_api/listmonk_api.py", line 65, in get_subscribers total_pages = int(response.headers['X-Total-Pages']) File "/home/random/repositories/test/.venv/lib/python3.10/site-packages/requests/structures.py", line 52, in __getitem__ return self._store[key.lower()][1] KeyError: 'x-total-pages'

At first I thought it may be caused by the nginx dropping an empty header, but even a local request from inside the container isn't containg an 'X-Total-Pages'-header.

A possible solution could be to fall back to 1 if the header doesn't exist (seems to be the case if the response isn't paged at all?).

jicho commented 1 year ago

Got the same issue with version 2.4.0 when I call:

client = listmonk_api.Api(url=listmonk_api_url, username=username, password=password)
print(client.get_lists())

When I change this:

total_pages = int(response.headers['X-Total-Pages'])

into (for testing)

total_pages = int(response.headers['X-Total-Pages'] if response.headers.get('X-Total-Pages') else 1)

The code continues, but breaks on: response = response + response_page

With the error : {TypeError}can only concatenate list (not "dict") to list

I'm using 0.12.0 of listmonk api

jicho commented 1 year ago

It looks like Listmonk made some API changes:

{
    "data": {
        "results": [
            {
                "id": 1,
                "created_at": "2020-02-10T23:07:16.194843+01:00",
                "updated_at": "2020-03-06T22:32:01.118327+01:00",
                "uuid": "ce13e971-c2ed-4069-bd0c-240e9a9f56f9",
                "name": "Default list",
                "type": "public",
                "optin": "double",
                "tags": [
                    "test"
                ],
                "subscriber_count": 2
            },
            {
                "id": 2,
                "created_at": "2020-03-04T21:12:09.555013+01:00",
                "updated_at": "2020-03-06T22:34:46.405031+01:00",
                "uuid": "f20a2308-dfb5-4420-a56d-ecf0618a102d",
                "name": "get",
                "type": "private",
                "optin": "single",
                "tags": [],
                "subscriber_count": 0
            }
        ],
        "total": 5,
        "per_page": 20,
        "page": 1
    }
}

The API returns now the number of pages:

        "total": 5,
        "per_page": 20,
        "page": 1

Source: https://listmonk.app/docs/apis/lists/