Added consumo_historico and python-dotenv for reading username and password from env vars#54
Added consumo_historico and python-dotenv for reading username and password from env vars#54Oneirag wants to merge 3 commits into
Conversation
hectorespert
left a comment
There was a problem hiding this comment.
Thanks for you collaboration.
I would rather not make the library depend on dotenv. If loading environment variables is necessary, it should be handled by the application that uses the library.
There was a problem hiding this comment.
Pull request overview
This PR extends the i-DE clients to support reading credentials from environment variables (including .env via python-dotenv) and adds new “billed consumption” retrieval methods, along with updates to tests and documentation.
Changes:
- Add
.env/environment-variable based authentication for both sync (Iber) and async (AsyncIber) clients. - Add new billed-consumption endpoints (
consumption_facturado/total_consumption_facturado) for sync and async clients. - Update unit tests and README examples; add
python-dotenvto dependencies.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 18 comments.
Show a summary per file
| File | Description |
|---|---|
oligo/requests/iber.py |
Adds dotenv/env-var login path and new billed-consumption methods; includes several refactors/formatting updates. |
oligo/asyncio/asynciber.py |
Adds dotenv/env-var login path and new billed-consumption methods for the async client. |
tests/test_iber.py |
Reworks HTTP mocking and adds coverage for new billed-consumption methods and env-var auth behavior. |
README.md |
Documents env-var / .env authentication and new billed-consumption examples (sync/async). |
setup.py |
Adds python-dotenv to install_requires and applies formatting updates. |
requirements.txt |
Adds python-dotenv dependency. |
oligo/exception.py |
Extends LoginException to accept a custom message; minor string formatting updates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| user = user or os.getenv("I-DE-USER", user) | ||
| password = password or os.getenv("I-DE-PASSWORD", password) |
| if not user or not password: | ||
| raise LoginException( | ||
| user or "unknown", | ||
| message="Login failed: user and password are required. Set I-DE-USER and I-DE-PASSWORD environment variables or pass them as arguments.", |
| def login(self, user=None, password=None, session=Session()): | ||
| """Creates session with your credentials. | ||
| Reads I-DE-USER and I-DE-PASSWORD from environment if available, | ||
| falling back to the provided parameters.""" | ||
| self.__session = session |
|
|
||
| from ..exception import LoginException, ResponseException, NoResponseException, SelectContractException, \ | ||
| SessionException | ||
| load_dotenv() |
| 'requests': ['requests'], | ||
| 'asyncio': ['aiohttp'] | ||
| }, | ||
| install_requires=["deprecated", "python-dotenv"], |
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| #### Obtener el consumo horario facturado durante un periodo (Sync) |
| # | ||
| # Returns a list of billed consumptions starting at midnight on the start day until 23:00 on the last day. | ||
| # Each value is the hourly billed consumption in Wh. | ||
| def consumption_facturado(self, start, end): |
| # | ||
| # Returns a list of billed consumptions starting at midnight on the start day until 23:00 on the last day. | ||
| # Each value is the hourly billed consumption in Wh. | ||
| async def consumption_facturado(self, start: datetime, end: datetime) -> list: |
| self.adapter = Adapter() | ||
|
|
||
| self._env_backup = {} | ||
| for key in ("I-DE-USER", "I-DE-PASSWORD"): |
| self.__session = aiohttp.ClientSession() | ||
| user = os.getenv("I-DE-USER", user) | ||
| password = os.getenv("I-DE-PASSWORD", password) | ||
| if not user or not password: | ||
| raise LoginException( | ||
| user or "unknown", | ||
| message="Login failed: user and password are required. Set I-DE-USER and I-DE-PASSWORD environment variables or pass them as arguments.", | ||
| ) |
…env vars - Remove python-dotenv dependency; env-var loading is the app's responsibility. - login() reads I_DE_USER / I_DE_PASSWORD; explicit args take precedence; the sync client no longer instantiates a shared Session at import time and the async client validates credentials before creating aiohttp.ClientSession. - Rename consumption_facturado / total_consumption_facturado to billed_consumption / total_billed_consumption for consistency with the rest of the English API; keep the old names as deprecated aliases. - Update README and tests accordingly; add a test that pins the explicit-args-over-env-vars precedence.
|
Thanks for the comments. I've applied changes to address them:
Environment variables:
Billed consumption API:
Other fixes:
Tests: 13/13 passing. @hectorespert, please let me know if this works for you, and @copilot, a re-review would be appreciated. |
Added a new endpoint for consumo_historico
Username and password can be read as an alternative of function parameters from I-DE-USER and I-DE-PASSWORD environmental variables using python-dotenv
Several fixes in tests