| 1 |
// Copyright 2024 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 |
package genai |
| 16 |
|
| 17 |
import ( |
| 18 |
"google.golang.org/api/option" |
| 19 |
"google.golang.org/api/option/internaloption" |
| 20 |
) |
| 21 |
|
| 22 |
// WithREST is an option that enables REST transport for the client. |
| 23 |
// The default transport (if this option isn't provided) is gRPC. |
| 24 |
func WithREST() option.ClientOption { |
| 25 |
return &withREST{} |
| 26 |
} |
| 27 |
|
| 28 |
type withREST struct { |
| 29 |
internaloption.EmbeddableAdapter |
| 30 |
} |
| 31 |
|
| 32 |
func (w *withREST) applyVertexaiOpt(c *config) { |
| 33 |
c.withREST = true |
| 34 |
} |
| 35 |
|
| 36 |
// WithClientInfo is an option that sets request information |
| 37 |
// identifying the product that is calling this client. |
| 38 |
func WithClientInfo(key, value string) option.ClientOption { |
| 39 |
return &withClientInfo{key: key, value: value} |
| 40 |
} |
| 41 |
|
| 42 |
type withClientInfo struct { |
| 43 |
internaloption.EmbeddableAdapter |
| 44 |
key, value string |
| 45 |
} |
| 46 |
|
| 47 |
func (w *withClientInfo) applyVertexaiOpt(c *config) { |
| 48 |
c.ciKey = w.key |
| 49 |
c.ciValue = w.value |
| 50 |
} |
| 51 |
|
| 52 |
type config struct { |
| 53 |
// withREST tells the client to use REST as the underlying transport. |
| 54 |
withREST bool |
| 55 |
// key-value pair to add to the Google client info header. |
| 56 |
ciKey string |
| 57 |
ciValue string |
| 58 |
} |
| 59 |
|
| 60 |
// newConfig generates a new config with all the given |
| 61 |
// vertexaiClientOptions applied. |
| 62 |
func newConfig(opts ...option.ClientOption) config { |
| 63 |
var conf config |
| 64 |
for _, opt := range opts { |
| 65 |
if vOpt, ok := opt.(vertexaiClientOption); ok { |
| 66 |
vOpt.applyVertexaiOpt(&conf) |
| 67 |
} |
| 68 |
} |
| 69 |
return conf |
| 70 |
} |
| 71 |
|
| 72 |
// A vertexaiClientOption is an option for a vertexai client. |
| 73 |
type vertexaiClientOption interface { |
| 74 |
option.ClientOption |
| 75 |
applyVertexaiOpt(*config) |
| 76 |
} |