Skip to content

Added consumo_historico and python-dotenv for reading username and password from env vars#54

Open
Oneirag wants to merge 3 commits into
hectorespert:masterfrom
Oneirag:master
Open

Added consumo_historico and python-dotenv for reading username and password from env vars#54
Oneirag wants to merge 3 commits into
hectorespert:masterfrom
Oneirag:master

Conversation

@Oneirag
Copy link
Copy Markdown

@Oneirag Oneirag commented May 26, 2026

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

Copy link
Copy Markdown
Owner

@hectorespert hectorespert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-dotenv to 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.

Comment thread oligo/requests/iber.py Outdated
Comment on lines +79 to +80
user = user or os.getenv("I-DE-USER", user)
password = password or os.getenv("I-DE-PASSWORD", password)
Comment thread oligo/requests/iber.py Outdated
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.",
Comment thread oligo/requests/iber.py Outdated
Comment on lines 74 to 78
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
Comment thread oligo/requests/iber.py Outdated

from ..exception import LoginException, ResponseException, NoResponseException, SelectContractException, \
SessionException
load_dotenv()
Comment thread setup.py Outdated
'requests': ['requests'],
'asyncio': ['aiohttp']
},
install_requires=["deprecated", "python-dotenv"],
Comment thread README.md
asyncio.run(main())
```

#### Obtener el consumo horario facturado durante un periodo (Sync)
Comment thread oligo/requests/iber.py Outdated
#
# 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):
Comment thread oligo/asyncio/asynciber.py Outdated
#
# 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:
Comment thread tests/test_iber.py Outdated
self.adapter = Adapter()

self._env_backup = {}
for key in ("I-DE-USER", "I-DE-PASSWORD"):
Comment thread oligo/asyncio/asynciber.py Outdated
Comment on lines +77 to +84
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.
@Oneirag
Copy link
Copy Markdown
Author

Oneirag commented Jun 3, 2026

Thanks for the comments. I've applied changes to address them:
On the python-dotenv dependency (@hectorespert's request):

  • Removed python-dotenv from setup.py and requirements.txt. Loading .env files is the responsibility of the application using the library.
  • Removed the load_dotenv() call at import time from oligo/requests/iber.py and oligo/asyncio/asynciber.py. Importing the library no longer implies filesystem access.

Environment variables:

  • Renamed to I_DE_USER and I_DE_PASSWORD (exportable directly in bash/zsh via export I_DE_USER=...). No hyphenated aliases to keep things unambiguous.
  • Updated tests, README, and LoginException messages to match the new names.
  • Precedence is now what the docstring says: explicit arguments win over env vars (consistent with commit 08dd840). Added a dedicated test to lock that in.

Billed consumption API:

  • Renamed to billed_consumption and total_billed_consumption on both the sync and async clients, aligned with the rest of the English API.
  • The previous names (consumption_facturado, total_consumption_facturado) are kept as @deprecated wrappers that delegate to the new methods, so existing consumers don't break.

Other fixes:

  • The sync login() no longer instantiates Session() in its default argument (a fresh Session is created inside the function when needed).
  • The async client now validates credentials before creating aiohttp.ClientSession(), avoiding resource leaks.
  • Async login type hints corrected to Optional[str].

Tests: 13/13 passing. @hectorespert, please let me know if this works for you, and @copilot, a re-review would be appreciated.

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

Successfully merging this pull request may close these issues.

4 participants