Class: ElevenlabsClient::HttpClient
- Inherits:
-
Object
- Object
- ElevenlabsClient::HttpClient
- Defined in:
- lib/elevenlabs_client/http_client.rb
Overview
HTTP client wrapper that handles all HTTP communication Separates HTTP concerns from business logic
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.elevenlabs.io"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#delete(path) ⇒ Hash
Makes an authenticated DELETE request.
-
#delete_with_body(path, body = nil) ⇒ Hash
Makes an authenticated DELETE request with JSON body.
-
#file_part(file_io, filename) ⇒ Faraday::Multipart::FilePart
Helper method to create Faraday::Multipart::FilePart.
-
#get(path, params = {}) ⇒ Hash
Makes an authenticated GET request.
-
#get_binary(path) ⇒ String
Makes an authenticated GET request expecting binary response.
-
#get_streaming(path, &block) ⇒ Faraday::Response
Makes an authenticated GET request with streaming response.
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#patch(path, body = nil) ⇒ Hash
Makes an authenticated PATCH request.
-
#post(path, body = nil) ⇒ Hash
Makes an authenticated POST request.
-
#post_binary(path, body = nil) ⇒ String
Makes an authenticated POST request expecting binary response.
-
#post_multipart(path, payload) ⇒ Hash
Makes an authenticated multipart POST request.
-
#post_streaming(path, body = nil, &block) ⇒ Faraday::Response
Makes an authenticated POST request with streaming response.
-
#post_streaming_with_timestamps(path, body = nil, &block) ⇒ Faraday::Response
Makes an authenticated POST request with streaming response for timestamp data.
-
#post_with_custom_headers(path, body = nil, custom_headers = {}) ⇒ String
Makes an authenticated POST request with custom headers.
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL) ⇒ HttpClient
Returns a new instance of HttpClient.
15 16 17 18 19 |
# File 'lib/elevenlabs_client/http_client.rb', line 15 def initialize(api_key:, base_url: DEFAULT_BASE_URL) @api_key = api_key @base_url = base_url @conn = build_connection end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
13 14 15 |
# File 'lib/elevenlabs_client/http_client.rb', line 13 def api_key @api_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
13 14 15 |
# File 'lib/elevenlabs_client/http_client.rb', line 13 def base_url @base_url end |
Instance Method Details
#delete(path) ⇒ Hash
Makes an authenticated DELETE request
50 51 52 53 54 55 56 |
# File 'lib/elevenlabs_client/http_client.rb', line 50 def delete(path) response = @conn.delete(path) do |req| req.headers["xi-api-key"] = api_key end handle_response(response) end |
#delete_with_body(path, body = nil) ⇒ Hash
Makes an authenticated DELETE request with JSON body
62 63 64 65 66 67 68 69 70 |
# File 'lib/elevenlabs_client/http_client.rb', line 62 def delete_with_body(path, body = nil) response = @conn.delete(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.body = body.to_json if body end handle_response(response) end |
#file_part(file_io, filename) ⇒ Faraday::Multipart::FilePart
Helper method to create Faraday::Multipart::FilePart
226 227 228 |
# File 'lib/elevenlabs_client/http_client.rb', line 226 def file_part(file_io, filename) Faraday::Multipart::FilePart.new(file_io, mime_for(filename), filename) end |
#get(path, params = {}) ⇒ Hash
Makes an authenticated GET request
25 26 27 28 29 30 31 |
# File 'lib/elevenlabs_client/http_client.rb', line 25 def get(path, params = {}) response = @conn.get(path, params) do |req| req.headers["xi-api-key"] = api_key end handle_response(response) end |
#get_binary(path) ⇒ String
Makes an authenticated GET request expecting binary response
102 103 104 105 106 107 108 |
# File 'lib/elevenlabs_client/http_client.rb', line 102 def get_binary(path) response = @conn.get(path) do |req| req.headers["xi-api-key"] = api_key end handle_response(response) end |
#get_streaming(path, &block) ⇒ Faraday::Response
Makes an authenticated GET request with streaming response
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/elevenlabs_client/http_client.rb', line 170 def get_streaming(path, &block) response = @conn.get(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Accept"] = "audio/mpeg" # Set up streaming callback req..on_data = proc do |chunk, _| block.call(chunk) if block_given? end end handle_response(response) end |
#patch(path, body = nil) ⇒ Hash
Makes an authenticated PATCH request
76 77 78 79 80 81 82 83 84 |
# File 'lib/elevenlabs_client/http_client.rb', line 76 def patch(path, body = nil) response = @conn.patch(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.body = body.to_json if body end handle_response(response) end |
#post(path, body = nil) ⇒ Hash
Makes an authenticated POST request
37 38 39 40 41 42 43 44 45 |
# File 'lib/elevenlabs_client/http_client.rb', line 37 def post(path, body = nil) response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.body = body.to_json if body end handle_response(response) end |
#post_binary(path, body = nil) ⇒ String
Makes an authenticated POST request expecting binary response
114 115 116 117 118 119 120 121 122 |
# File 'lib/elevenlabs_client/http_client.rb', line 114 def post_binary(path, body = nil) response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.body = body.to_json if body end handle_response(response) end |
#post_multipart(path, payload) ⇒ Hash
Makes an authenticated multipart POST request
90 91 92 93 94 95 96 97 |
# File 'lib/elevenlabs_client/http_client.rb', line 90 def post_multipart(path, payload) response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.body = payload end handle_response(response) end |
#post_streaming(path, body = nil, &block) ⇒ Faraday::Response
Makes an authenticated POST request with streaming response
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/elevenlabs_client/http_client.rb', line 150 def post_streaming(path, body = nil, &block) response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.headers["Accept"] = "audio/mpeg" req.body = body.to_json if body # Set up streaming callback req..on_data = proc do |chunk, _| block.call(chunk) if block_given? end end handle_response(response) end |
#post_streaming_with_timestamps(path, body = nil, &block) ⇒ Faraday::Response
Makes an authenticated POST request with streaming response for timestamp data
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/elevenlabs_client/http_client.rb', line 189 def (path, body = nil, &block) buffer = "" response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" req.body = body.to_json if body # Set up streaming callback for JSON chunks req..on_data = proc do |chunk, _| if block_given? buffer += chunk # Process complete JSON objects while buffer.include?("\n") line, buffer = buffer.split("\n", 2) next if line.strip.empty? begin json_data = JSON.parse(line) block.call(json_data) rescue JSON::ParserError # Skip malformed JSON lines next end end end end end handle_response(response) end |
#post_with_custom_headers(path, body = nil, custom_headers = {}) ⇒ String
Makes an authenticated POST request with custom headers
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/elevenlabs_client/http_client.rb', line 129 def post_with_custom_headers(path, body = nil, custom_headers = {}) response = @conn.post(path) do |req| req.headers["xi-api-key"] = api_key req.headers["Content-Type"] = "application/json" custom_headers.each { |key, value| req.headers[key] = value } req.body = body.to_json if body end # For streaming/binary responses, return raw body if custom_headers["Accept"]&.include?("audio") || custom_headers["Transfer-Encoding"] == "chunked" handle_response(response) else handle_response(response) end end |