# The Okta software accompanied by this notice is provided pursuant to the following terms:
# Copyright © 2025-Present, Okta, Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
# License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
# IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
# coding: utf-8

"""
Okta Admin Management

Allows customers to easily access the Okta Management APIs

The version of the OpenAPI document: 5.1.0
Contact: devex-public@okta.com
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""  # noqa: E501

import logging

# AUTO-GENERATED! DO NOT EDIT FILE DIRECTLY
# This client class is typically generated based on an API specification (e.g., OpenAPI).
# Changes to this file may be overwritten upon regeneration.
# SEE CONTRIBUTOR DOCUMENTATION
import aiohttp

from okta.api.agent_pools_api import AgentPoolsApi
from okta.api.api_service_integrations_api import ApiServiceIntegrationsApi
from okta.api.api_token_api import ApiTokenApi
from okta.api.application_api import ApplicationApi
from okta.api.application_connections_api import ApplicationConnectionsApi
from okta.api.application_cross_app_access_connections_api import (
    ApplicationCrossAppAccessConnectionsApi,
)
from okta.api.application_features_api import ApplicationFeaturesApi
from okta.api.application_grants_api import ApplicationGrantsApi
from okta.api.application_groups_api import ApplicationGroupsApi
from okta.api.application_logos_api import ApplicationLogosApi
from okta.api.application_policies_api import ApplicationPoliciesApi
from okta.api.application_sso_api import ApplicationSSOApi
from okta.api.application_sso_credential_key_api import ApplicationSSOCredentialKeyApi
from okta.api.application_sso_federated_claims_api import (
    ApplicationSSOFederatedClaimsApi,
)
from okta.api.application_sso_public_keys_api import ApplicationSSOPublicKeysApi
from okta.api.application_tokens_api import ApplicationTokensApi
from okta.api.application_users_api import ApplicationUsersApi
from okta.api.associated_domain_customizations_api import (
    AssociatedDomainCustomizationsApi,
)
from okta.api.attack_protection_api import AttackProtectionApi
from okta.api.authenticator_api import AuthenticatorApi
from okta.api.authorization_server_api import AuthorizationServerApi
from okta.api.authorization_server_assoc_api import AuthorizationServerAssocApi
from okta.api.authorization_server_claims_api import AuthorizationServerClaimsApi
from okta.api.authorization_server_clients_api import AuthorizationServerClientsApi
from okta.api.authorization_server_keys_api import AuthorizationServerKeysApi
from okta.api.authorization_server_policies_api import AuthorizationServerPoliciesApi
from okta.api.authorization_server_rules_api import AuthorizationServerRulesApi
from okta.api.authorization_server_scopes_api import AuthorizationServerScopesApi
from okta.api.behavior_api import BehaviorApi
from okta.api.brands_api import BrandsApi
from okta.api.captcha_api import CAPTCHAApi
from okta.api.custom_domain_api import CustomDomainApi
from okta.api.custom_pages_api import CustomPagesApi
from okta.api.custom_templates_api import CustomTemplatesApi
from okta.api.device_api import DeviceApi
from okta.api.device_assurance_api import DeviceAssuranceApi
from okta.api.device_integrations_api import DeviceIntegrationsApi
from okta.api.device_posture_check_api import DevicePostureCheckApi
from okta.api.directories_integration_api import DirectoriesIntegrationApi
from okta.api.email_customization_api import EmailCustomizationApi
from okta.api.email_domain_api import EmailDomainApi
from okta.api.email_server_api import EmailServerApi
from okta.api.event_hook_api import EventHookApi
from okta.api.feature_api import FeatureApi
from okta.api.governance_bundle_api import GovernanceBundleApi
from okta.api.group_api import GroupApi
from okta.api.group_owner_api import GroupOwnerApi
from okta.api.group_push_mapping_api import GroupPushMappingApi
from okta.api.group_rule_api import GroupRuleApi
from okta.api.hook_key_api import HookKeyApi
from okta.api.identity_provider_api import IdentityProviderApi
from okta.api.identity_provider_keys_api import IdentityProviderKeysApi
from okta.api.identity_provider_signing_keys_api import IdentityProviderSigningKeysApi
from okta.api.identity_provider_users_api import IdentityProviderUsersApi
from okta.api.identity_source_api import IdentitySourceApi
from okta.api.inline_hook_api import InlineHookApi
from okta.api.linked_object_api import LinkedObjectApi
from okta.api.log_stream_api import LogStreamApi
from okta.api.network_zone_api import NetworkZoneApi
from okta.api.o_auth2_resource_server_credentials_keys_api import (
    OAuth2ResourceServerCredentialsKeysApi,
)
from okta.api.okta_application_settings_api import OktaApplicationSettingsApi
from okta.api.okta_personal_settings_api import OktaPersonalSettingsApi
from okta.api.org_creator_api import OrgCreatorApi
from okta.api.org_setting_admin_api import OrgSettingAdminApi
from okta.api.org_setting_communication_api import OrgSettingCommunicationApi
from okta.api.org_setting_contact_api import OrgSettingContactApi
from okta.api.org_setting_customization_api import OrgSettingCustomizationApi
from okta.api.org_setting_general_api import OrgSettingGeneralApi
from okta.api.org_setting_metadata_api import OrgSettingMetadataApi
from okta.api.org_setting_support_api import OrgSettingSupportApi
from okta.api.policy_api import PolicyApi
from okta.api.principal_rate_limit_api import PrincipalRateLimitApi
from okta.api.profile_mapping_api import ProfileMappingApi
from okta.api.push_provider_api import PushProviderApi
from okta.api.rate_limit_settings_api import RateLimitSettingsApi
from okta.api.realm_api import RealmApi
from okta.api.realm_assignment_api import RealmAssignmentApi
from okta.api.role_assignment_a_user_api import RoleAssignmentAUserApi
from okta.api.role_assignment_b_group_api import RoleAssignmentBGroupApi
from okta.api.role_assignment_client_api import RoleAssignmentClientApi
from okta.api.role_b_target_admin_api import RoleBTargetAdminApi
from okta.api.role_b_target_b_group_api import RoleBTargetBGroupApi
from okta.api.role_b_target_client_api import RoleBTargetClientApi
from okta.api.role_c_resource_set_api import RoleCResourceSetApi
from okta.api.role_c_resource_set_resource_api import RoleCResourceSetResourceApi
from okta.api.role_d_resource_set_binding_api import RoleDResourceSetBindingApi
from okta.api.role_d_resource_set_binding_member_api import (
    RoleDResourceSetBindingMemberApi,
)
from okta.api.role_e_custom_api import RoleECustomApi
from okta.api.role_e_custom_permission_api import RoleECustomPermissionApi
from okta.api.schema_api import SchemaApi
from okta.api.service_account_api import ServiceAccountApi
from okta.api.session_api import SessionApi
from okta.api.ssf_receiver_api import SSFReceiverApi
from okta.api.ssf_security_event_token_api import SSFSecurityEventTokenApi
from okta.api.ssf_transmitter_api import SSFTransmitterApi
from okta.api.subscription_api import SubscriptionApi
from okta.api.system_log_api import SystemLogApi
from okta.api.template_api import TemplateApi
from okta.api.themes_api import ThemesApi
from okta.api.threat_insight_api import ThreatInsightApi
from okta.api.trusted_origin_api import TrustedOriginApi
from okta.api.ui_schema_api import UISchemaApi
from okta.api.user_api import UserApi
from okta.api.user_authenticator_enrollments_api import UserAuthenticatorEnrollmentsApi
from okta.api.user_classification_api import UserClassificationApi
from okta.api.user_cred_api import UserCredApi
from okta.api.user_factor_api import UserFactorApi
from okta.api.user_grant_api import UserGrantApi
from okta.api.user_lifecycle_api import UserLifecycleApi
from okta.api.user_linked_object_api import UserLinkedObjectApi
from okta.api.user_o_auth_api import UserOAuthApi
from okta.api.user_resources_api import UserResourcesApi
from okta.api.user_risk_api import UserRiskApi
from okta.api.user_sessions_api import UserSessionsApi
from okta.api.user_type_api import UserTypeApi
from okta.api.web_authn_preregistration_api import WebAuthnPreregistrationApi
from okta.cache.no_op_cache import NoOpCache
from okta.cache.okta_cache import OktaCache
from okta.config.config_setter import ConfigSetter
from okta.config.config_validator import ConfigValidator
from okta.logger import setup_logging
from okta.request_executor import RequestExecutor


class Client(
    AgentPoolsApi,
    ApiServiceIntegrationsApi,
    ApiTokenApi,
    ApplicationApi,
    ApplicationConnectionsApi,
    ApplicationCrossAppAccessConnectionsApi,
    ApplicationFeaturesApi,
    ApplicationGrantsApi,
    ApplicationGroupsApi,
    ApplicationLogosApi,
    ApplicationPoliciesApi,
    ApplicationSSOApi,
    ApplicationSSOCredentialKeyApi,
    ApplicationSSOFederatedClaimsApi,
    ApplicationSSOPublicKeysApi,
    ApplicationTokensApi,
    ApplicationUsersApi,
    AssociatedDomainCustomizationsApi,
    AttackProtectionApi,
    AuthenticatorApi,
    AuthorizationServerApi,
    AuthorizationServerAssocApi,
    AuthorizationServerClaimsApi,
    AuthorizationServerClientsApi,
    AuthorizationServerKeysApi,
    AuthorizationServerPoliciesApi,
    AuthorizationServerRulesApi,
    AuthorizationServerScopesApi,
    BehaviorApi,
    BrandsApi,
    CAPTCHAApi,
    CustomDomainApi,
    CustomPagesApi,
    CustomTemplatesApi,
    DeviceApi,
    DeviceAssuranceApi,
    DeviceIntegrationsApi,
    DevicePostureCheckApi,
    DirectoriesIntegrationApi,
    EmailCustomizationApi,
    EmailDomainApi,
    EmailServerApi,
    EventHookApi,
    FeatureApi,
    GovernanceBundleApi,
    GroupApi,
    GroupOwnerApi,
    GroupPushMappingApi,
    GroupRuleApi,
    HookKeyApi,
    IdentityProviderApi,
    IdentityProviderKeysApi,
    IdentityProviderSigningKeysApi,
    IdentityProviderUsersApi,
    IdentitySourceApi,
    InlineHookApi,
    LinkedObjectApi,
    LogStreamApi,
    NetworkZoneApi,
    OAuth2ResourceServerCredentialsKeysApi,
    OktaApplicationSettingsApi,
    OktaPersonalSettingsApi,
    OrgCreatorApi,
    OrgSettingAdminApi,
    OrgSettingCommunicationApi,
    OrgSettingContactApi,
    OrgSettingCustomizationApi,
    OrgSettingGeneralApi,
    OrgSettingMetadataApi,
    OrgSettingSupportApi,
    PolicyApi,
    PrincipalRateLimitApi,
    ProfileMappingApi,
    PushProviderApi,
    RateLimitSettingsApi,
    RealmApi,
    RealmAssignmentApi,
    RoleAssignmentAUserApi,
    RoleAssignmentBGroupApi,
    RoleAssignmentClientApi,
    RoleBTargetAdminApi,
    RoleBTargetBGroupApi,
    RoleBTargetClientApi,
    RoleCResourceSetApi,
    RoleCResourceSetResourceApi,
    RoleDResourceSetBindingApi,
    RoleDResourceSetBindingMemberApi,
    RoleECustomApi,
    RoleECustomPermissionApi,
    SSFReceiverApi,
    SSFSecurityEventTokenApi,
    SSFTransmitterApi,
    SchemaApi,
    ServiceAccountApi,
    SessionApi,
    SubscriptionApi,
    SystemLogApi,
    TemplateApi,
    ThemesApi,
    ThreatInsightApi,
    TrustedOriginApi,
    UISchemaApi,
    UserApi,
    UserAuthenticatorEnrollmentsApi,
    UserClassificationApi,
    UserCredApi,
    UserFactorApi,
    UserGrantApi,
    UserLifecycleApi,
    UserLinkedObjectApi,
    UserOAuthApi,
    UserResourcesApi,
    UserRiskApi,
    UserSessionsApi,
    UserTypeApi,
    WebAuthnPreregistrationApi,
):
    """An Okta client object, serving as an entry point to various Okta APIs."""

    def __init__(self, user_config: dict = {}):
        # Load configuration
        client_config_setter = ConfigSetter()
        client_config_setter._apply_config({"client": user_config})
        self._config = client_config_setter.get_config()
        # Prune configuration to remove unnecessary fields
        self._config = client_config_setter._prune_config(self._config)
        # Validate configuration
        ConfigValidator(self._config)
        # Initialize superclasses (the specific Okta API clients like UserApi, ApplicationApi)
        # These superclasses would typically contain the methods corresponding to API operations.
        super().__init__(configuration=self._config)

        # Set client instance variables from the validated configuration
        self._authorization_mode = self._config["client"]["authorizationMode"]
        self._base_url = self._config["client"][
            "orgUrl"
        ]  # Base URL for the Okta organization
        self._api_token = self._config["client"].get(
            "token", None
        )  # For SSWS token authentication
        self._client_id = None  # For OAuth 2.0 / OpenID Connect
        self._scopes = None  # For OAuth 2.0 / OpenID Connect
        self._private_key = None  # For OAuth 2.0 Private Key JWT
        self._oauth_token_renewal_offset = (
            None  # Offset for proactive OAuth token renewal
        )

        # Determine which cache to use (NoOpCache or OktaCache)
        cache = NoOpCache()
        if self._config["client"]["cache"]["enabled"] is True:
            if user_config.get("cacheManager", None) is None:
                time_to_idle = self._config["client"]["cache"]["defaultTti"]
                time_to_live = self._config["client"]["cache"]["defaultTtl"]
                cache = OktaCache(time_to_live, time_to_idle)
            else:
                cache = user_config.get("cacheManager")

        # Determine request executor to use
        # The RequestExecutor handles the actual HTTP calls.
        self._request_executor = user_config.get("requestExecutor", RequestExecutor)(
            self._config, cache, user_config.get("httpClient", None)
        )

        # Set private key variables if using PrivateKey authorization mode (for OAuth 2.0)
        if self._authorization_mode == "PrivateKey":
            self._client_id = self._config["client"]["clientId"]
            self._scopes = self._config["client"]["scopes"]
            self._private_key = self._config["client"]["privateKey"]
            self._oauth_token_renewal_offset = self._config["client"][
                "oauthTokenRenewalOffset"
            ]

        # Setup logging for the SDK
        setup_logging(log_level=self._config["client"]["logging"]["logLevel"])
        if self._config["client"]["logging"]["enabled"] is True:
            logger = logging.getLogger("okta-sdk-python")
            logger.disabled = False

    async def __aenter__(self):
        """Automatically create and set session within context manager for async operations."""
        self._session = aiohttp.ClientSession()
        self._request_executor.set_session(self._session)
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        """Automatically close session within context manager for async operations."""
        await self._session.close()

    """
    Getters for client properties
    """

    def get_config(self):
        """Returns the client's configuration."""
        return self._config

    def get_scopes(self):
        """Returns the OAuth scopes if configured."""
        return self._scopes

    def get_base_url(self):
        """Returns the base URL for the Okta organization."""
        return self._base_url

    def get_request_executor(self):
        """Returns the request executor instance."""
        return self._request_executor

    """
    Custom Header Management
    """

    def set_custom_headers(self, headers: dict):
        """Sets custom headers to be sent with every request."""
        self._request_executor.set_custom_headers(headers)

    def clear_custom_headers(self):
        """Clears any previously set custom headers."""
        self._request_executor.clear_custom_headers()

    def get_custom_headers(self) -> dict:
        """Gets the currently set custom headers."""
        return self._request_executor.get_custom_headers()

    def get_default_headers(self) -> dict:
        """Gets the default headers used by the request executor."""
        return self._request_executor.get_default_headers()
