# Copyright 2026 Google LLC
#
# 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.
#
# pyformat: disable

"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

import httpx
from .asynctypes import (
    AsyncBeforeRequestHook,
    AsyncAfterSuccessHook,
    AsyncAfterErrorHook,
    AsyncAfterParseErrorHook,
    AsyncHooks,
)
from .types import (
    BeforeRequestContext,
    AfterSuccessContext,
    AfterErrorContext,
    AfterParseErrorContext,
)
from typing import List, Optional, Tuple


class AsyncSDKHooks(AsyncHooks):
    def __init__(self) -> None:
        self.before_request_hooks: List[AsyncBeforeRequestHook] = []
        self.after_success_hooks: List[AsyncAfterSuccessHook] = []
        self.after_error_hooks: List[AsyncAfterErrorHook] = []
        self.after_parse_error_hooks: List[AsyncAfterParseErrorHook] = []

    def register_before_request_hook(self, hook: AsyncBeforeRequestHook) -> None:
        self.before_request_hooks.append(hook)

    def register_after_success_hook(self, hook: AsyncAfterSuccessHook) -> None:
        self.after_success_hooks.append(hook)

    def register_after_error_hook(self, hook: AsyncAfterErrorHook) -> None:
        self.after_error_hooks.append(hook)

    def register_after_parse_error_hook(self, hook: AsyncAfterParseErrorHook) -> None:
        self.after_parse_error_hooks.append(hook)

    async def before_request(
        self, hook_ctx: BeforeRequestContext, request: httpx.Request
    ) -> httpx.Request:
        for hook in self.before_request_hooks:
            out = await hook.before_request(hook_ctx, request)
            if isinstance(out, Exception):
                raise out
            request = out

        return request

    async def after_success(
        self, hook_ctx: AfterSuccessContext, response: httpx.Response
    ) -> httpx.Response:
        for hook in self.after_success_hooks:
            out = await hook.after_success(hook_ctx, response)
            if isinstance(out, Exception):
                raise out
            response = out
        return response

    async def after_error(
        self,
        hook_ctx: AfterErrorContext,
        response: Optional[httpx.Response],
        error: Optional[Exception],
    ) -> Tuple[Optional[httpx.Response], Optional[Exception]]:
        for hook in self.after_error_hooks:
            result = await hook.after_error(hook_ctx, response, error)
            if isinstance(result, Exception):
                raise result
            response, error = result
        return response, error

    async def after_parse_error(
        self,
        hook_ctx: AfterParseErrorContext,
        response: httpx.Response,
        error: Exception,
    ) -> Exception:
        for hook in self.after_parse_error_hooks:
            error = await hook.after_parse_error(hook_ctx, response, error)
        return error
