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
15// Code generated by the Google Gen AI SDK generator DO NOT EDIT.
16
17package genai
18
19import (
20 "context"
21 "fmt"
22 "net/http"
23 "strings"
24)
25
26func getOperationParametersToMldev(ac *apiClient, fromObject map[string]any, parentObject map[string]any) (toObject map[string]any, err error) {
27 toObject = make(map[string]any)
28
29 fromOperationName := getValueByPath(fromObject, []string{"operationName"})
30 if fromOperationName != nil {
31 setValueByPath(toObject, []string{"_url", "operationName"}, fromOperationName)
32 }
33
34 fromConfig := getValueByPath(fromObject, []string{"config"})
35 if fromConfig != nil {
36 setValueByPath(toObject, []string{"config"}, fromConfig)
37 }
38
39 return toObject, nil
40}
41
42func getOperationParametersToVertex(ac *apiClient, fromObject map[string]any, parentObject map[string]any) (toObject map[string]any, err error) {
43 toObject = make(map[string]any)
44
45 fromOperationName := getValueByPath(fromObject, []string{"operationName"})
46 if fromOperationName != nil {
47 setValueByPath(toObject, []string{"_url", "operationName"}, fromOperationName)
48 }
49
50 fromConfig := getValueByPath(fromObject, []string{"config"})
51 if fromConfig != nil {
52 setValueByPath(toObject, []string{"config"}, fromConfig)
53 }
54
55 return toObject, nil
56}
57
58func fetchPredictOperationParametersToVertex(ac *apiClient, fromObject map[string]any, parentObject map[string]any) (toObject map[string]any, err error) {
59 toObject = make(map[string]any)
60
61 fromOperationName := getValueByPath(fromObject, []string{"operationName"})
62 if fromOperationName != nil {
63 setValueByPath(toObject, []string{"operationName"}, fromOperationName)
64 }
65
66 fromResourceName := getValueByPath(fromObject, []string{"resourceName"})
67 if fromResourceName != nil {
68 setValueByPath(toObject, []string{"_url", "resourceName"}, fromResourceName)
69 }
70
71 fromConfig := getValueByPath(fromObject, []string{"config"})
72 if fromConfig != nil {
73 setValueByPath(toObject, []string{"config"}, fromConfig)
74 }
75
76 return toObject, nil
77}
78
79// Operations provides methods for managing the long-running operations.
80// You don't need to initiate this struct. Create a client instance via NewClient, and
81// then access Operations through client.Operations field.
82type Operations struct {
83 apiClient *apiClient
84}
85
86func (m Operations) getVideosOperation(ctx context.Context, operationName string, config *GetOperationConfig) (*GenerateVideosOperation, error) {
87 parameterMap := make(map[string]any)
88
89 kwargs := map[string]any{"operationName": operationName, "config": config}
90 deepMarshal(kwargs, ¶meterMap)
91
92 var httpOptions *HTTPOptions
93 if config == nil {
94 httpOptions = mergeHTTPOptions(m.apiClient.clientConfig, nil)
95 } else {
96 httpOptions = mergeHTTPOptions(m.apiClient.clientConfig, config.HTTPOptions)
97 config.HTTPOptions = nil
98 }
99 var response = new(GenerateVideosOperation)
100 var responseMap map[string]any
101 var fromConverter func(*apiClient, map[string]any, map[string]any) (map[string]any, error)
102 var toConverter func(*apiClient, map[string]any, map[string]any) (map[string]any, error)
103 if m.apiClient.clientConfig.Backend == BackendVertexAI {
104 toConverter = getOperationParametersToVertex
105 fromConverter = generateVideosOperationFromVertex
106 } else {
107 toConverter = getOperationParametersToMldev
108 fromConverter = generateVideosOperationFromMldev
109 }
110
111 body, err := toConverter(m.apiClient, parameterMap, nil)
112 if err != nil {
113 return nil, err
114 }
115 var path string
116 var urlParams map[string]any
117 if _, ok := body["_url"]; ok {
118 urlParams = body["_url"].(map[string]any)
119 delete(body, "_url")
120 }
121 if m.apiClient.clientConfig.Backend == BackendVertexAI {
122 path, err = formatMap("{operationName}", urlParams)
123 } else {
124 path, err = formatMap("{operationName}", urlParams)
125 }
126 if err != nil {
127 return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
128 }
129 if _, ok := body["_query"]; ok {
130 query, err := createURLQuery(body["_query"].(map[string]any))
131 if err != nil {
132 return nil, err
133 }
134 path += "?" + query
135 delete(body, "_query")
136 }
137
138 if _, ok := body["config"]; ok {
139 delete(body, "config")
140 }
141 responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodGet, body, httpOptions)
142 if err != nil {
143 return nil, err
144 }
145 responseMap, err = fromConverter(m.apiClient, responseMap, nil)
146 if err != nil {
147 return nil, err
148 }
149 err = mapToStruct(responseMap, response)
150 if err != nil {
151 return nil, err
152 }
153 return response, nil
154}
155
156func (m Operations) fetchPredictVideosOperation(ctx context.Context, operationName string, resourceName string, config *FetchPredictOperationConfig) (*GenerateVideosOperation, error) {
157 parameterMap := make(map[string]any)
158
159 kwargs := map[string]any{"operationName": operationName, "resourceName": resourceName, "config": config}
160 deepMarshal(kwargs, ¶meterMap)
161
162 var httpOptions *HTTPOptions
163 if config == nil {
164 httpOptions = mergeHTTPOptions(m.apiClient.clientConfig, nil)
165 } else {
166 httpOptions = mergeHTTPOptions(m.apiClient.clientConfig, config.HTTPOptions)
167 config.HTTPOptions = nil
168 }
169 var response = new(GenerateVideosOperation)
170 var responseMap map[string]any
171 var fromConverter func(*apiClient, map[string]any, map[string]any) (map[string]any, error)
172 var toConverter func(*apiClient, map[string]any, map[string]any) (map[string]any, error)
173 if m.apiClient.clientConfig.Backend == BackendVertexAI {
174 toConverter = fetchPredictOperationParametersToVertex
175 fromConverter = generateVideosOperationFromVertex
176 } else {
177
178 return nil, fmt.Errorf("method FetchPredictVideosOperation is only supported in the Vertex AI client. You can choose to use Vertex AI by setting ClientConfig.Backend to BackendVertexAI.")
179
180 }
181
182 body, err := toConverter(m.apiClient, parameterMap, nil)
183 if err != nil {
184 return nil, err
185 }
186 var path string
187 var urlParams map[string]any
188 if _, ok := body["_url"]; ok {
189 urlParams = body["_url"].(map[string]any)
190 delete(body, "_url")
191 }
192 if m.apiClient.clientConfig.Backend == BackendVertexAI {
193 path, err = formatMap("{resourceName}:fetchPredictOperation", urlParams)
194 } else {
195 path, err = formatMap("None", urlParams)
196 }
197 if err != nil {
198 return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
199 }
200 if _, ok := body["_query"]; ok {
201 query, err := createURLQuery(body["_query"].(map[string]any))
202 if err != nil {
203 return nil, err
204 }
205 path += "?" + query
206 delete(body, "_query")
207 }
208
209 if _, ok := body["config"]; ok {
210 delete(body, "config")
211 }
212 responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPost, body, httpOptions)
213 if err != nil {
214 return nil, err
215 }
216 responseMap, err = fromConverter(m.apiClient, responseMap, nil)
217 if err != nil {
218 return nil, err
219 }
220 err = mapToStruct(responseMap, response)
221 if err != nil {
222 return nil, err
223 }
224 return response, nil
225}
226
227// GetVideosOperation retrieves the status and result of a long-running video generation operation.
228//
229// If the operation is still in progress, the returned GenerateVideosOperation
230// will have Done set to false. If the operation has completed successfully,
231// Done will be true, and the Result field will contain the result. If the
232// operation failed, Done will be true, and the Error field will be populated.
233func (m Operations) GetVideosOperation(ctx context.Context, operation *GenerateVideosOperation, config *GetOperationConfig) (*GenerateVideosOperation, error) {
234 operationName := operation.Name
235 if operationName == "" {
236 return nil, fmt.Errorf("Operation name is empty")
237 }
238 if m.apiClient.clientConfig.Backend == BackendVertexAI {
239 parts := strings.Split(operationName, "/operations/")
240 if len(parts) > 1 {
241 resourceName := parts[0]
242 fetchPredictOperationConfig := &FetchPredictOperationConfig{}
243 if config != nil {
244 fetchPredictOperationConfig.HTTPOptions = config.HTTPOptions
245 }
246 return m.fetchPredictVideosOperation(ctx, operationName, resourceName, fetchPredictOperationConfig)
247 } else {
248 return nil, fmt.Errorf("Invalid operation name")
249 }
250 }
251 return m.getVideosOperation(ctx, operationName, config)
252}