Skip to content

Conversation

@florian-s-code
Copy link
Contributor

@florian-s-code florian-s-code commented Jan 25, 2025

This update introduces the ability to pass a requests.Session object to the miniflux client. This change enables users to leverage session management features such as rate limiting and caching.

Changes:

  • Added session: requests.Session as an optional parameter to the Client class constructor.
  • Update the constructor to configure the session and trying to avoid to clash with any preset Session config
  • The session is used for every HTTP request
  • The default behavior creates a new session for each client instance.

Example (header configuration):

import requests
import miniflux

session = requests.Session()
session.headers.update({"User-Agent": "Custom User-Agent"})

client = miniflux.Client("https://reader.miniflux.app", session=session)

Example (client-side caching):

import miniflux
from cachecontrol import CacheControl
from cachecontrol.heuristics import ExpiresAfter

session = CacheControl(requests.Session(), heuristic=ExpiresAfter(days=1))

client = miniflux.Client("https://reader.miniflux.app", session=session)

Possible improvement:

  • The request.Session object is auto-closeable, would it make sense to make the miniflux.Client auto-closeable as well to close the session ?

Let me know if you have any questions or suggestions!

@florian-s-code florian-s-code changed the title **Description:** Allow to pass a requests.Session object to the miniflux client. Jan 25, 2025
This update introduces the ability to pass a `requests.Session` object to the `Miniflux` client. This change enables users to leverage session management features such as rate limiting and caching.

**Changes:**
- Added `session: requests.Session` as an optional parameter to the `Miniflux` class constructor.
- Update the constructor to configure the session and trying to avoid to clash with any preset Session config
- The session is used for every HTTP request
- The default behavior creates a new session for each client instance.

**Example (header configuration):**

```python
import requests
import miniflux

session = requests.Session()
session.headers.update({"User-Agent": "Custom User-Agent"})

client = miniflux.Client("https://reader.miniflux.app", session=session)
```

**Example (client-side caching):**
```python
import miniflux
from cachecontrol import CacheControl
from cachecontrol.heuristics import ExpiresAfter

session = CacheControl(requests.Session(), heuristic=ExpiresAfter(days=1))

client = miniflux.Client("https://reader.miniflux.app", session=session)
```

**Possible improvement:**

The `request.Session` object is auto-closeable, would it make sense to
make the `miniflux.Client` auto-closeable as well to close the session ?

Let me know if you have any questions or suggestions!
@fguillot
Copy link
Member

Your pull-request looks good. However, there are some minor issues detected by the linter if you don't mind fixing that (this project uses Ruff).

The request.Session object is auto-closeable, would it make sense to make the miniflux.Client auto-closeable as well to close the session ?

That sounds like a good idea. Feel free to improve that as well :)

@florian-s-code
Copy link
Contributor Author

Glad you liked it.
All comments addressed. Let me know if anything else needs change.

@fguillot fguillot merged commit 172399c into miniflux:main Jan 26, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants