GenerateImagesRequest.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2025 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. public class GenerateImagesRequest: @unchecked Sendable, GenerativeAIRequest {
  17. public typealias Response = GenerateImagesResponse
  18. public var url: URL {
  19. var urlString =
  20. "\(apiConfig.service.endpoint.rawValue)/\(apiConfig.version.rawValue)/projects/\(projectID)"
  21. if case let .vertexAI(_, location) = apiConfig.service {
  22. urlString += "/locations/\(location)"
  23. }
  24. urlString += "/templates/\(template):\(ImageAPIMethod.generateImages.rawValue)"
  25. return URL(string: urlString)!
  26. }
  27. public let options: RequestOptions
  28. let apiConfig: APIConfig
  29. let template: String
  30. let variables: [String: TemplateVariable]
  31. let projectID: String
  32. init(template: String, variables: [String: TemplateVariable], projectID: String,
  33. apiConfig: APIConfig, options: RequestOptions) {
  34. self.apiConfig = apiConfig
  35. self.options = options
  36. self.template = template
  37. self.variables = variables
  38. self.projectID = projectID
  39. }
  40. enum CodingKeys: String, CodingKey {
  41. case variables = "inputs"
  42. }
  43. public func encode(to encoder: Encoder) throws {
  44. var container = encoder.container(keyedBy: CodingKeys.self)
  45. try container.encode(variables, forKey: .variables)
  46. }
  47. }