Skip to content
Draft
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
12 changes: 10 additions & 2 deletions pyoaev/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, Any, BinaryIO, Dict, List, Optional, Union
from urllib import parse
from uuid import UUID

import requests

Expand All @@ -23,6 +24,7 @@ def __init__(
pagination: Optional[str] = None,
order_by: Optional[str] = None,
ssl_verify: Union[bool, str] = True,
tenant_id: Optional[UUID] = None,
**kwargs: Any,
) -> None:

Expand All @@ -32,6 +34,7 @@ def __init__(
raise ValueError("A TOKEN must be set")

self.url = url
self.tenant_id = tenant_id
self.timeout = timeout
#: Headers that will be used in request to OpenAEV
self.headers = {
Expand Down Expand Up @@ -109,9 +112,14 @@ def _build_url(self, path: str) -> str:
Returns:
The full URL
"""
if path.startswith("http://") or path.startswith("https://"):
if parse.urlparse(path).scheme in ("http", "https"):
return path
return f"{self.url}/api{path}"
base_url = self.url.rstrip("/")
normalized_path = path.lstrip("/")
if self.tenant_id:
return f"{base_url}/api/tenants/{self.tenant_id}/{normalized_path}"
else:
return f"{base_url}/api/{normalized_path}"

def _get_session_opts(self) -> Dict[str, Any]:
return {
Expand Down
6 changes: 6 additions & 0 deletions pyoaev/configuration/settings_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import timedelta
from pathlib import Path
from typing import Annotated, Literal
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field, HttpUrl, PlainSerializer
from pydantic_settings import (
Expand Down Expand Up @@ -99,6 +100,11 @@ class ConfigLoaderOAEV(BaseConfigModel):
token: str = Field(
description="The token for the OpenAEV platform.",
)
tenant_id: UUID | None = Field(
default=None,
description="Identifier of the tenant within the OpenAEV platform. Used in multi-tenant environments to scope "
"API requests and ensure data isolation between different tenants.",
)


class ConfigLoaderCollector(BaseConfigModel):
Expand Down
6 changes: 4 additions & 2 deletions pyoaev/daemons/base_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC, abstractmethod
from inspect import signature
from types import FunctionType
from uuid import UUID

from pyoaev.client import OpenAEV
from pyoaev.configuration import Configuration
Expand Down Expand Up @@ -37,6 +38,7 @@ def __init__(
self.api = api_client or BaseDaemon.__get_default_api_client(
url=self._configuration.get("openaev_url"),
token=self._configuration.get("openaev_token"),
tenant_id=self._configuration.get("openaev_tenant_id"),
)

# logging
Expand Down Expand Up @@ -131,8 +133,8 @@ def get_id(self):
)

@classmethod
def __get_default_api_client(cls, url, token):
return OpenAEV(url=url, token=token)
def __get_default_api_client(cls, url, token, tenant_id: UUID | None):
return OpenAEV(url=url, token=token, tenant_id=tenant_id)

@classmethod
def __get_default_logger(cls, log_level, name):
Expand Down
1 change: 1 addition & 0 deletions pyoaev/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def __init__(self, config: OpenAEVConfigHelper, icon) -> None:
self.api = OpenAEV(
url=config.get_conf("openaev_url"),
token=config.get_conf("openaev_token"),
tenant_id=config.get_conf("openaev_tenant_id"),
)
# Get the mq configuration from api
self.config = {
Expand Down
11 changes: 9 additions & 2 deletions pyoaev/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def __init__(self, api, config, logger, ping_type) -> None:
threading.Thread.__init__(self)
self.ping_type = ping_type
self.api = api
self.tenant_id = getattr(self.api, "tenant_id", None)
self.config = config
self.logger = logger
self.in_error = False
Expand All @@ -203,9 +204,15 @@ def ping(self) -> None:
self.exit_event.wait(40)

def run(self) -> None:
self.logger.info("Starting PingAlive thread")
self.logger.info(
"Starting PingAlive thread",
{"tenant_id": str(self.tenant_id) if self.tenant_id else None},
)
self.ping()

def stop(self) -> None:
self.logger.info("Preparing PingAlive for clean shutdown")
self.logger.info(
"Preparing PingAlive for clean shutdown",
{"tenant_id": str(self.tenant_id) if self.tenant_id else None},
)
self.exit_event.set()
Loading