"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT."""

import requests as requests_http
from .analytics import Analytics
from .domains import Domains
from .links import Links
from .metatags import Metatags
from .qr_codes import QRCodes
from .sdkconfiguration import SDKConfiguration
from .tags import Tags
from .track import Track
from .utils.retries import RetryConfig
from .workspaces import Workspaces
from dub import utils
from dub._hooks import SDKHooks
from dub.models import components, internal
from typing import Callable, Dict, Optional, Union

class Dub:
    r"""Dub.co API: Dub is link management infrastructure for companies to create marketing campaigns, link sharing features, and referral programs."""
    links: Links
    qr_codes: QRCodes
    analytics: Analytics
    workspaces: Workspaces
    tags: Tags
    domains: Domains
    track: Track
    metatags: Metatags

    sdk_configuration: SDKConfiguration

    def __init__(self,
                 token: Union[Optional[str], Callable[[], Optional[str]]] = None,
                 workspace_id: str = None,
                 project_slug: str = None,
                 server_idx: Optional[int] = None,
                 server_url: Optional[str] = None,
                 url_params: Optional[Dict[str, str]] = None,
                 client: Optional[requests_http.Session] = None,
                 retry_config: Optional[RetryConfig] = None
                 ) -> None:
        """Instantiates the SDK configuring it with the provided parameters.

        :param token: The token required for authentication
        :type token: Union[Optional[str], Callable[[], Optional[str]]]
        :param workspace_id: Configures the workspace_id parameter for all supported operations
        :type workspace_id: str
        :param project_slug: Configures the project_slug parameter for all supported operations
        :type project_slug: str
        :param server_idx: The index of the server to use for all operations
        :type server_idx: int
        :param server_url: The server URL to use for all operations
        :type server_url: str
        :param url_params: Parameters to optionally template the server URL with
        :type url_params: Dict[str, str]
        :param client: The requests.Session HTTP client to use for all operations
        :type client: requests_http.Session
        :param retry_config: The utils.RetryConfig to use globally
        :type retry_config: RetryConfig
        """
        if client is None:
            client = requests_http.Session()

        if callable(token):
            def security():
                return components.Security(token = token())
        else:
            security = components.Security(token = token)

        if server_url is not None:
            if url_params is not None:
                server_url = utils.template_url(server_url, url_params)
    
        _globals = internal.Globals(
            workspace_id=workspace_id,
            project_slug=project_slug,
        )

        self.sdk_configuration = SDKConfiguration(
            client,
            _globals,
            security,
            server_url,
            server_idx,
            retry_config=retry_config
        )

        hooks = SDKHooks()

        current_server_url, *_ = self.sdk_configuration.get_server_details()
        server_url, self.sdk_configuration.client = hooks.sdk_init(current_server_url, self.sdk_configuration.client)
        if current_server_url != server_url:
            self.sdk_configuration.server_url = server_url

        # pylint: disable=protected-access
        self.sdk_configuration.__dict__['_hooks'] = hooks

        self._init_sdks()


    def _init_sdks(self):
        self.links = Links(self.sdk_configuration)
        self.qr_codes = QRCodes(self.sdk_configuration)
        self.analytics = Analytics(self.sdk_configuration)
        self.workspaces = Workspaces(self.sdk_configuration)
        self.tags = Tags(self.sdk_configuration)
        self.domains = Domains(self.sdk_configuration)
        self.track = Track(self.sdk_configuration)
        self.metatags = Metatags(self.sdk_configuration)
