File: host_configuration.py

package info (click to toggle)
python-aiopyarr 23.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,300 kB
  • sloc: python: 25,335; makefile: 22; javascript: 11
file content (45 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""PyArrHostConfiguration."""
from __future__ import annotations

from dataclasses import dataclass

from .. import ArrException


@dataclass
class PyArrHostConfiguration:  # pylint: disable=too-many-instance-attributes
    """PyArrHostConfiguration."""

    api_token: str | None = None
    hostname: str | None = None
    ipaddress: str | None = None
    port: int | None = None
    ssl: bool = False
    verify_ssl: bool = True
    base_api_path: str | None = None
    url: str | None = None
    api_ver: str | None = None

    def __post_init__(self) -> None:
        """Post init."""
        if self.api_token is None:
            raise ArrException(message="No api token to the server was provided")
        if self.hostname is None and self.ipaddress is None and self.url is None:
            raise ArrException(
                message="No url, hostname or ipaddress to the server was provided"
            )

    def api_url(self, command: str, initialize: bool = False) -> str:
        """Return the generated base URL based on host configuration."""
        if initialize:
            return f"{self.base_url}/initialize.js"
        return f"{self.base_url}/api/{self.api_ver}/{command}"

    @property
    def base_url(self) -> str:
        """Return the base URL for the configured service."""
        if self.url is not None:
            return self.url
        protocol = f"http{'s' if self.ssl else ''}"
        host = f"{self.hostname or self.ipaddress}:{self.port}"
        return f"{protocol}://{host}{self.base_api_path or ''}"