Class: Google::Genai::ApiClient
- Inherits:
-
Object
- Object
- Google::Genai::ApiClient
- Defined in:
- lib/google/genai/api_client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#vertexai ⇒ Object
readonly
Returns the value of attribute vertexai.
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(api_key: nil, vertexai: nil, project: nil, location: nil, http_options: nil) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #request(http_method, path, body: nil) ⇒ Object
- #upload_file(file_path, file_size, mime_type, display_name: nil) ⇒ Object
Constructor Details
#initialize(api_key: nil, vertexai: nil, project: nil, location: nil, http_options: nil) ⇒ ApiClient
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/google/genai/api_client.rb', line 15 def initialize(api_key: nil, vertexai: nil, project: nil, location: nil, http_options: nil) @api_key = api_key @vertexai = vertexai = || {} base_url = if @vertexai # TODO: Handle global location "https://#{location}-aiplatform.googleapis.com/" else "https://generativelanguage.googleapis.com/" end @connection = Faraday.new(url: base_url) do |faraday| faraday.request :retry faraday.headers['Content-Type'] = 'application/json' if @api_key faraday.headers['x-goog-api-key'] = @api_key elsif @vertexai scopes = ['https://www.googleapis.com/auth/cloud-platform'] = Google::Auth.get_application_default(scopes) faraday.request :authorization, 'Bearer', -> { .fetch_access_token!['access_token'] } end end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
13 14 15 |
# File 'lib/google/genai/api_client.rb', line 13 def api_key @api_key end |
#vertexai ⇒ Object (readonly)
Returns the value of attribute vertexai.
13 14 15 |
# File 'lib/google/genai/api_client.rb', line 13 def vertexai @vertexai end |
Instance Method Details
#delete(path) ⇒ Object
87 88 89 |
# File 'lib/google/genai/api_client.rb', line 87 def delete(path) request(:delete, path) end |
#get(path) ⇒ Object
83 84 85 |
# File 'lib/google/genai/api_client.rb', line 83 def get(path) request(:get, path) end |
#request(http_method, path, body: nil) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/google/genai/api_client.rb', line 76 def request(http_method, path, body: nil) response = @connection.send(http_method, path) do |req| req.body = body.to_json if body end handle_response(response) end |
#upload_file(file_path, file_size, mime_type, display_name: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/google/genai/api_client.rb', line 40 def upload_file(file_path, file_size, mime_type, display_name: nil) # 1. Start resumable upload start_upload_headers = { "X-Goog-Upload-Protocol" => "resumable", "X-Goog-Upload-Command" => "start", "X-Goog-Upload-Header-Content-Length" => file_size.to_s, "X-Goog-Upload-Header-Content-Type" => mime_type, "Content-Type" => "application/json" } start_response = @connection.post("upload/v1beta/files") do |req| req.headers.merge!(start_upload_headers) req.body = { file: { display_name: display_name } }.to_json end handle_response(start_response) upload_url = start_response.headers["x-goog-upload-url"] # 2. Upload file content File.open(file_path, "rb") do |file| upload_connection = Faraday.new(url: upload_url) upload_response = upload_connection.post do |req| req.headers.merge!({ "X-Goog-Upload-Offset" => "0", "X-Goog-Upload-Command" => "upload, finalize", "Content-Length" => file_size.to_s, "Content-Type" => mime_type }) req.body = file.read end handle_response(upload_response) return JSON.parse(upload_response.body) end end |