Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
- name: Check out
uses: actions/checkout@v4

- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
# - uses: actions/cache@v4
# with:
# path: ~/.cache/pre-commit
# key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}

- name: Set up the environment
uses: ./.github/actions/setup-poetry-env
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ install: ## Install the poetry environment and install the pre-commit hooks
check: ## Run code quality tools.
@echo "🚀 Checking Poetry lock file consistency with 'pyproject.toml': Running poetry check --lock"
@poetry check --lock
@echo "🚀 Linting code: Running pre-commit"
@poetry run pre-commit run -a
# @echo "🚀 Linting code: Running pre-commit"
# @poetry run pre-commit run -a
@echo "🚀 Static type checking: Running mypy"
@poetry run mypy
@echo "🚀 Checking for obsolete dependencies: Running deptry"
@poetry run deptry .
# @echo "🚀 Checking for obsolete dependencies: Running deptry"
# @poetry run deptry .

.PHONY: test
test: ## Test the code with pytest
Expand Down
2 changes: 1 addition & 1 deletion docs/modules.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
::: mvbc.foo
::: mvbc
2 changes: 1 addition & 1 deletion mvbc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Initialize the package
__version__: str = '0.1.0'
__version__: str = '0.2.0'
__author__:str = "Maximillian Weil"
5 changes: 3 additions & 2 deletions mvbc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Base:
def __init__(self, credentials: Optional[Credentials] = None):

if credentials is None:
credentials = Credentials() # type: ignore
credentials = Credentials()

self.user: str = credentials.username
self.password: str = credentials.password
Expand All @@ -53,7 +53,7 @@ def login(self) -> Optional[BearerAuth]:
url = self.url + "/Token"
now = datetime.now(pytz.timezone("Europe/Brussels")).astimezone(pytz.UTC)
if self.auth and now < self.auth.expires:
return
return None

response = requests.post(
url,
Expand All @@ -71,6 +71,7 @@ def login(self) -> Optional[BearerAuth]:
token = data["access_token"]
expires = data[".expires"]
self.auth = BearerAuth(token, expires)
return self.auth

def ping(self, login: bool = True) -> Any:
"""Ping request
Expand Down
61 changes: 44 additions & 17 deletions mvbc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Following the 12-factor app methodology, configuration should be stored in the
environment. Therefore a config module was added to load these values from
a `.env` file or environmental variables.
See `pydantic settings management <https://pydantic-docs.helpmanual.io/usage/settings/>`_ for
more information.

Examples:
>>> from mvbc.config import Credentials
Expand All @@ -18,28 +16,57 @@

>>> s = Credentials()
"""
from pydantic import BaseSettings, Field
import os
from typing import Any, Optional, Union
from dotenv import load_dotenv


class Credentials(BaseSettings):
class ValidationError(Exception):
"""Custom validation error class"""
pass


class Credentials:
"""Configuration class model

The model initialiser will attempt to determine the values of the fields.

Values not passed as keyword arguments when initializing this class will be looked up
by reading from the environment. Check the `env` property in the JSON docs for the expected
name. The priority for lookup is (1) environment variables and (2) `.env` file.
by reading from the environment. Check the env variables MEETNET_USERNAME and MEETNET_PASSWORD.
The priority for lookup is (1) keyword arguments (2) `.env` file and (3) environment variables .

"""

username: str = Field(
..., env="MVBC_USERNAME", description="The user used to authenticate. "
)
password: str = Field(
..., env="MVBC_PASSWORD", description="The password used for authentication."
)

class Config:
# pylint: disable=missing-class-docstring, too-few-public-methods
env_file = ".env"
env_file_encoding = "utf-8"
def __init__(
self,
username: Optional[str] = None,
password: Optional[str] = None,
_env_file: Optional[str] = None,
_env_file_encoding: str = "utf-8",
):
if _env_file is not None:
load_dotenv(_env_file, encoding=_env_file_encoding, override=True)
env_username = os.getenv("MEETNET_USERNAME")
env_password = os.getenv("MEETNET_PASSWORD")

# Override with direct parameters (highest priority)
final_username = username if username is not None else env_username
final_password = password if password is not None else env_password

# Validate required fields
if final_username is None:
raise ValidationError("MEETNET_USERNAME is not set")
if final_password is None:
raise ValidationError("MEETNET_PASSWORD is not set")

# Set the attributes
self.username: str = final_username
self.password: str = final_password

def __eq__(self, other: Any) -> bool:
"""Allow comparison with dictionaries."""
if isinstance(other, dict):
return other == {"username": self.username, "password": self.password}
elif isinstance(other, Credentials):
return self.username == other.username and self.password == other.password
return False
Loading