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

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

Open
xlemmingx opened this issue Jan 4, 2023 · 2 comments
Open

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

xlemmingx opened this issue Jan 4, 2023 · 2 comments

Comments

@xlemmingx
Copy link

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

Environment

  • listmonk v2.3.0 in docker behind a nginx-reverse proxy with a single list and a single subscriber
  • python script on remote computer using this api in version 0.1.2

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
Copy link

jicho commented Aug 8, 2023

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
Copy link

jicho commented Aug 8, 2023

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants