Class: Typecast::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/typecast/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.typecast.ai".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
# File 'lib/typecast/client.rb', line 16

def initialize(api_key: ENV["TYPECAST_API_KEY"], base_url: ENV["TYPECAST_API_HOST"] || DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30)
  @api_key = api_key.to_s.strip
  @base_url = normalize_base_url(base_url)
  validate_api_key!
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/typecast/client.rb', line 14

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



14
15
16
# File 'lib/typecast/client.rb', line 14

def base_url
  @base_url
end

Instance Method Details

#clone_voice(audio:, filename:, name:, model:) ⇒ Object



71
72
73
74
75
# File 'lib/typecast/client.rb', line 71

def clone_voice(audio:, filename:, name:, model:)
  validate_clone_inputs(audio, name)
  response = request_multipart("/v1/voices/clone", audio: audio, filename: filename, fields: { name: name, model: model })
  Models::CustomVoice.from_h(JSON.parse(response.body))
end

#delete_voice(voice_id) ⇒ Object



77
78
79
80
# File 'lib/typecast/client.rb', line 77

def delete_voice(voice_id)
  request_raw(:delete, "/v1/voices/#{path_segment(voice_id)}")
  nil
end

#get_my_subscriptionObject



51
52
53
# File 'lib/typecast/client.rb', line 51

def get_my_subscription
  Models::SubscriptionResponse.from_h(JSON.parse(request_json(:get, "/v1/users/me/subscription").body))
end

#get_voice_v2(voice_id) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
# File 'lib/typecast/client.rb', line 62

def get_voice_v2(voice_id)
  voices = JSON.parse(request_json(:get, "/v2/voices/#{path_segment(voice_id)}").body).map do |item|
    Models::VoiceV2.from_h(item)
  end
  raise NotFoundError, "Voice not found: #{voice_id}" if voices.empty?

  voices.first
end

#get_voices_v2(filter = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/typecast/client.rb', line 55

def get_voices_v2(filter = nil)
  query = filter.respond_to?(:to_h) ? filter.to_h : filter
  JSON.parse(request_json(:get, "/v2/voices", nil, query).body).map do |item|
    Models::VoiceV2.from_h(item)
  end
end

#text_to_speech(request) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/typecast/client.rb', line 24

def text_to_speech(request)
  response = request_json(:post, "/v1/text-to-speech", request.to_h)
  Models::TTSResponse.new(
    audio_data: response.body,
    duration: response["X-Audio-Duration"].to_f,
    format: response["Content-Type"].to_s.include?("mp3") || response["Content-Type"].to_s.include?("mpeg") ? Models::AUDIO_MP3 : Models::AUDIO_WAV
  )
end

#text_to_speech_stream(request) {|response.body| ... } ⇒ Object

Yields:

  • (response.body)


33
34
35
36
37
38
# File 'lib/typecast/client.rb', line 33

def text_to_speech_stream(request)
  response = request_json(:post, "/v1/text-to-speech/stream", request.to_h)
  return enum_for(:text_to_speech_stream, request) unless block_given?

  yield response.body
end

#text_to_speech_with_timestamps(request, granularity: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/typecast/client.rb', line 40

def text_to_speech_with_timestamps(request, granularity: nil)
  unless granularity.nil? || %w[word char].include?(granularity)
    raise ArgumentError, "granularity must be 'word' or 'char'"
  end

  path = "/v1/text-to-speech/with-timestamps"
  query = granularity.nil? ? nil : { granularity: granularity }
  response = request_json(:post, path, request.to_h, query)
  Models::TTSWithTimestampsResponse.from_h(JSON.parse(response.body))
end