GenerativeAIRequest.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  16. protocol GenerativeAIRequest: Sendable, Encodable {
  17. associatedtype Response: Decodable
  18. var url: URL { get }
  19. var options: RequestOptions { get }
  20. }
  21. /// Configuration parameters for sending requests to the backend.
  22. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  23. // TODO(#14405): Make the `apiVersion` constructor public in Firebase 12 with a default of `.v1`.
  24. public struct RequestOptions: Sendable {
  25. /// The request’s timeout interval in seconds; if not specified uses the default value for a
  26. /// `URLRequest`.
  27. let timeout: TimeInterval
  28. /// The API version to use in requests to the backend.
  29. let apiVersion: String
  30. /// Initializes a request options object.
  31. ///
  32. /// - Parameters:
  33. /// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
  34. /// - apiVersion: The API version to use in requests to the backend.
  35. init(timeout: TimeInterval = 180.0, apiVersion: APIVersion) {
  36. self.timeout = timeout
  37. self.apiVersion = apiVersion.versionIdentifier
  38. }
  39. /// Initializes a request options object.
  40. ///
  41. /// - Parameters:
  42. /// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
  43. public init(timeout: TimeInterval = 180.0) {
  44. self.init(timeout: timeout, apiVersion: .v1beta)
  45. }
  46. }
  47. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  48. extension RequestOptions: Equatable {}