// Code generated by sidekick. DO NOT EDIT.
//
// 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
//
//     https://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.

import Foundation
#if canImport(FoundationNetworking)
  import FoundationNetworking
#endif
import GoogleCloudLocation
import GoogleCloudWkt
import GoogleIAMV1
import GoogleLongRunning
import GoogleRpc
import GoogleCloudGax

extension Clients {
  final class AutokeyRetry: AutokeyStub {
    let inner: any AutokeyStub
    let options: GoogleCloudGax.ClientOptions

    public init(_ inner: any AutokeyStub, options: GoogleCloudGax.ClientOptions) {
      self.inner = inner
      self.options = options
    }

    func _intercept<Input, Output>(
      request: Input,
      options: GoogleCloudGax.RequestOptions,
      idempotent: Swift.Bool,
      action: (Input, GoogleCloudGax.RequestOptions) async throws -> Output,
    ) async throws -> Output {
      let loop = GoogleCloudGax._RetryLoop(
        options: options, withDefault: self.options, idempotent: idempotent,
      )
      let attempt = { (attemptTimeout: Swift.Duration?) async throws -> Output in
        var attemptOptions = options
        attemptOptions.attemptTimeout = attemptTimeout
        return try await action(request, attemptOptions)
      }
      return try await loop.run(attempt: attempt)
    }

    public func createKeyHandle(
      request: CreateKeyHandleRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: false,
        action: {
          (r: CreateKeyHandleRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleLongRunning.Operation
          in
          return try await self.inner.createKeyHandle(request: r, options: o)
        })
    }

    public func getKeyHandle(
      request: GetKeyHandleRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudKMSV1.KeyHandle {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: GetKeyHandleRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleCloudKMSV1.KeyHandle
          in
          return try await self.inner.getKeyHandle(request: r, options: o)
        })
    }

    public func listKeyHandles(
      request: ListKeyHandlesRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudKMSV1.ListKeyHandlesResponse {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: ListKeyHandlesRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleCloudKMSV1.ListKeyHandlesResponse
          in
          return try await self.inner.listKeyHandles(request: r, options: o)
        })
    }

    public func listLocations(
      request: GoogleCloudLocation.ListLocationsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudLocation.ListLocationsResponse {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: GoogleCloudLocation.ListLocationsRequest, o: GoogleCloudGax.RequestOptions)
            async throws -> GoogleCloudLocation.ListLocationsResponse
          in
          return try await self.inner.listLocations(request: r, options: o)
        })
    }

    public func getLocation(
      request: GoogleCloudLocation.GetLocationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudLocation.Location {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: GoogleCloudLocation.GetLocationRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleCloudLocation.Location
          in
          return try await self.inner.getLocation(request: r, options: o)
        })
    }

    public func setIamPolicy(
      request: GoogleIAMV1.SetIamPolicyRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleIAMV1.Policy {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: false,
        action: {
          (r: GoogleIAMV1.SetIamPolicyRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleIAMV1.Policy
          in
          return try await self.inner.setIamPolicy(request: r, options: o)
        })
    }

    public func getIamPolicy(
      request: GoogleIAMV1.GetIamPolicyRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleIAMV1.Policy {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: GoogleIAMV1.GetIamPolicyRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleIAMV1.Policy
          in
          return try await self.inner.getIamPolicy(request: r, options: o)
        })
    }

    public func testIamPermissions(
      request: GoogleIAMV1.TestIamPermissionsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleIAMV1.TestIamPermissionsResponse {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: false,
        action: {
          (r: GoogleIAMV1.TestIamPermissionsRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleIAMV1.TestIamPermissionsResponse
          in
          return try await self.inner.testIamPermissions(request: r, options: o)
        })
    }

    public func getOperation(
      request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation {
      try await self._intercept(
        request: request,
        options: options,
        idempotent: true,
        action: {
          (r: GoogleLongRunning.GetOperationRequest, o: GoogleCloudGax.RequestOptions) async throws
            -> GoogleLongRunning.Operation
          in
          return try await self.inner.getOperation(request: r, options: o)
        })
    }
  }
}
